- Notifications
You must be signed in to change notification settings - Fork750
Fuzzy finder
You can define the following command to act as a simple fuzzy file picker from the command line:
def find -params1 -shell-script-candidates %{ find . -type f } %{edit %arg{1} }
Then:find
on the command line will find fuzzy matches to the first argument as you type.
From Kakoune 2022.10.31, the switches for providing command completion are discouraged in favor of thecomplete-command
command.The recommended way is now:
define-command find -docstring"find files" -params1 %{edit %arg{1} }complete-command find shell-script-candidates %{ find . -type f }
This command replicates Vim behavior whenpath
option contains**
item.find
will search for file recursively, and if several files found it will prompt you a list of files to choose. This is smart function, so it tries to find match as fast as possible before going full recursive.
define-command -docstring \"find <filename>: search for file recusively under path option: %opt{path}" \find -params1 %{evaluate-commands%sh{ file=$1eval"set --$kak_buflist"while [$#-gt 0 ];do# Check if buffer with thisif ["$file"="$1" ];then# file already exists. Basicallyprintf"%s\n""buffer$1"# emulating what edit command doesexitfishiftdoneif [-e"$file" ];then# Test if file exists underprintf"%s\n""edit -existing %{$file}"# servers' working directoryexit# this is last resort untilfi# we start recursive searchimg# if everthing above fails - search for file under patheval"set --$kak_opt_path"while [$#-gt 0 ];docase$1in# Since we want to check fewer places ./) path=${kak_buffile%/*} ;;# I've swapped ./ and %/ because %/) path=$PWD ;;# %/ usually has smaller scope. So*) path=$1 ;;# this trick is a speedi-up hack.esacif [-z"${file##*/*}" ];then# test if filename contains pathif [-e"$path/$file" ];thenprintf"%s\n""edit -existing %{$path/$file}"exitfielse# build list of candidates or automatically select if only one foundforcandidatein$(find -L$path -mount -type f -name"$file");doif [-n"$candidate" ];then candidates="$candidates %{$candidate} %{evaluate-commands %{edit -existing %{$candidate}}}"fidoneif [-n"$candidates" ];thenprintf"%s\n""menu -auto-single$candidates"exitfifishiftdoneprintf"%s\n""echo -markup %{{Error}unable to find file '$file'}"}}
This command can also be mapped togf keeping old behavior withgAlt+f:
map -docstring"file non-recursive"global goto'<a-f>''<esc>gf'map -docstring"file"global goto'f''<esc>: find %val{selection}<ret>'
def git-edit -params1 -shell-script-candidates %{ git ls-files } %{edit %arg{1} }
Again, the recommended way is now:
define-command git-edit -params1 %{edit %arg{1} }complete-command git-edit shell-script-candidates %{ git ls-files }
fzf is a basic fuzzy finder tool used to interactively filter large lists of data in a shell.
In the following snippets, a tmux pane is opened with fzf ready to filter a list of files and a list of current buffers.
Other kakoune lists could be filtered this way, takingfzf.vim as inspiration.
define-command -docstring'Invoke fzf to open a file' -params0 fzf-edit %{evaluate-commands%sh{if [-z"${kak_client_env_TMUX}" ];thenprintf'fail "client was not started under tmux"\n'else file="$(find. -type f|TMUX="${kak_client_env_TMUX}" fzf-tmux -d 15)"if [-n"$file" ];thenprintf'edit "%s"\n'"$file"fifi }}# the original version no longer works since kak_buflist is no longer ":" separated.# this one works even you have single quote or newline in file names.define-command -docstring'Invoke fzf to select a buffer' fzf-buffer %{evaluate-commands%sh{ BUFFER=$( (eval"set --$kak_buflist"while [$#-gt 0 ];doprintf"%s\0""$1"shiftdone )| fzf-tmux -d 15 --read0) BUFFER=${BUFFER/\'/\'\'}if [-n"$BUFFER" ];thenprintf"buffer '%s'""${BUFFER}"fi }}
There's also afzf.kak plugin, that includes such features:
- Supports both x11 and tmux
- Opening files with
find
,fd
,rg
,ag
- Previewing file contents with syntax highlighting, backed by
bat
,coderay
,highlight
,rogue
- Switching buffers
- Automatic detection of your VCS to list project files. Supports:
- Git
- GNU Bazaar
- Subversion
- Mercurial
- Searching tags withuniversal-ctags
- Filtering tags by
kind
s for all ctags supported languages
- Filtering tags by
- Searching current buffer contents
- Changing server's working directory
Useterminal
,fd
withskim to open a interactively fuzzy file finder in a new terminal window.
define-command find -docstring"Find a file to open" -params .. %{ terminal sk -c"fd -tf %arg{@}" --bind %exp{enter:execute(echoeval -verbatim -client %val{client}edit'"{}"' | kak -p %val{session})+abort}}
Select an open buffer usingRofi
define-command rofi-buffers \-docstring'Select an open buffer using Rofi' %{evaluate-commands%sh{ BUFFER=$(printf"%s\n""${kak_buflist}"| tr"""\n"| rofi -dmenu| tr -d\')if [-n"$BUFFER" ];thenprintf"%s\n""buffer${BUFFER}"fi} }
Using Ag + Rofi to select files in your project directory.
define-command rofi-files \-docstring'Select files in project using Ag and Rofi' %{nop%sh{ FILE=$(ag -g""| rofi -dmenu)if [-n"$FILE" ];thenprintf'eval -client %%{%s} edit %%{%s}\n'"${kak_client}""${FILE}"| kak -p"${kak_session}"fi} }
Fast file finder:fasd integration
Addfunction k () kak `fasd -f $1`
to your .zshrc to open frecent files withk filename
- Normal mode commands
- Avoid the escape key
- Implementing user mode (Leader key)
- Kakoune explain
- Kakoune TV