mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-20 09:33:42 -05:00
Improved escaping, makes use of "dir" option, simplifies cases and catches more with regexes.
51
examples.md
51
examples.md
@@ -226,41 +226,50 @@ Fuzzy cmdline completion
|
|||||||
|
|
||||||
```vimL
|
```vimL
|
||||||
cnoremap <silent> <c-l> <c-\>eGetCompletions()<cr>
|
cnoremap <silent> <c-l> <c-\>eGetCompletions()<cr>
|
||||||
|
"add an extra <cr> at the end of this line to automatically accept the fzf-selected completions.
|
||||||
|
|
||||||
function! Lister()
|
function! Lister()
|
||||||
call extend(g:FZF_Cmd_Completion_Pre_List,split(getcmdline(),' '))
|
call extend(g:FZF_Cmd_Completion_Pre_List,split(getcmdline(),'\(\\\zs\)\@<!\& '))
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! CmdLineDirComplete(prefix, options, rawdir)
|
||||||
|
let l:dirprefix = matchstr(a:rawdir,"^.*/")
|
||||||
|
if isdirectory(expand(l:dirprefix))
|
||||||
|
return join(a:prefix + map(fzf#run({
|
||||||
|
\'options': a:options . ' --select-1 --query=' .
|
||||||
|
\ a:rawdir[matchend(a:rawdir,"^.*/"):len(a:rawdir)],
|
||||||
|
\'dir': expand(l:dirprefix)
|
||||||
|
\}),
|
||||||
|
\'"' . escape(l:dirprefix, " ") . '" . escape(v:val, " ")'))
|
||||||
|
else
|
||||||
|
return join(a:prefix + map(fzf#run({
|
||||||
|
\'options': a:options . '--query='. a:rawdir }),
|
||||||
|
\'escape(v:val, " ")'))
|
||||||
|
"dropped --select-1 to speed things up on a long query
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! GetCompletions()
|
function! GetCompletions()
|
||||||
let g:FZF_Cmd_Completion_Pre_List = []
|
let g:FZF_Cmd_Completion_Pre_List = []
|
||||||
let l:cmdline_list = split(getcmdline(), ' ', 1)
|
let l:cmdline_list = split(getcmdline(), '\(\\\zs\)\@<!\& ', 1)
|
||||||
let l:Prefix = l:cmdline_list[0:-2]
|
let l:Prefix = l:cmdline_list[0:-2]
|
||||||
execute "silent normal! :" . getcmdline() . "\<c-a>\<c-\>eLister()\<cr>\<c-c>"
|
execute "silent normal! :" . getcmdline() . "\<c-a>\<c-\>eLister()\<cr>\<c-c>"
|
||||||
let l:FZF_Cmd_Completion_List = g:FZF_Cmd_Completion_Pre_List[len(l:Prefix):-1]
|
let l:FZF_Cmd_Completion_List = g:FZF_Cmd_Completion_Pre_List[len(l:Prefix):-1]
|
||||||
unlet g:FZF_Cmd_Completion_Pre_List
|
unlet g:FZF_Cmd_Completion_Pre_List
|
||||||
if len(l:Prefix) > 0 && (
|
if len(l:Prefix) > 0 && l:Prefix[0] =~
|
||||||
\ (l:Prefix[0] == 'e') ||
|
\ '^ed\=i\=t\=$\|^spl\=i\=t\=$\|^tabed\=i\=t\=$\|^arged\=i\=t\=$\|^vsp\=l\=i\=t\=$'
|
||||||
\ (l:Prefix[0] == 'tabe') ||
|
"single-argument file commands
|
||||||
\ (l:Prefix[0] == 'vs') ||
|
return CmdLineDirComplete(l:Prefix, "",l:cmdline_list[-1])
|
||||||
\ (l:Prefix[0] == 'sp')
|
elseif len(l:Prefix) > 0 && l:Prefix[0] =~
|
||||||
\)
|
\ '^arg\=s\=$\|^ne\=x\=t\=$\|^sne\=x\=t\=$\|^argad\=d\=$'
|
||||||
return join(l:Prefix + map(fzf#run({'options': '--select-1 --query='.l:cmdline_list[-1] }),
|
"multi-argument file commands
|
||||||
\'escape(v:val, " ")'))
|
return CmdLineDirComplete(l:Prefix, '--multi', l:cmdline_list[-1])
|
||||||
elseif len(l:Prefix) > 0 && (
|
|
||||||
\ (l:Prefix[0] == 'ar') ||
|
|
||||||
\ (l:Prefix[0] == 'n') ||
|
|
||||||
\ (l:Prefix[0] == 'sn') ||
|
|
||||||
\ (l:Prefix[0] == 'arga')
|
|
||||||
\)
|
|
||||||
return join(l:Prefix + map(fzf#run({'options': '-m --select-1 --query='.l:cmdline_list[-1] }),
|
|
||||||
\'escape(v:val, " ")'))
|
|
||||||
else
|
else
|
||||||
return join(l:Prefix + fzf#run({
|
return join(l:Prefix + fzf#run({
|
||||||
\'source':l:FZF_Cmd_Completion_List,
|
\'source':l:FZF_Cmd_Completion_List,
|
||||||
\'options': '--select-1 --query='.l:cmdline_list[-1]
|
\'options': '--select-1 --query='.shellescape(l:cmdline_list[-1])
|
||||||
\}))
|
\}))
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
```
|
```
|
||||||
|
|
||||||
hit `<c-l>` while in the ex commandline (i.e. after pressing `:`) to have fzf filter a list of vim's commandline auto-completions. Try `:colo␣<c-l>` (be sure to include the space) or `:b␣<c-l>`. There are special cases for handling file-searches, so that you can go deeper into the directory than just one layer, and so that you can add multiple files to the arglist at once. More special cases could be added. Some limitations: the auto-complete for `:help` and `:tag` are limited to 300 entries, so you may need to narrow it a bit.
|
hit `<c-l>` while in the ex commandline (i.e. after pressing `:`) to have fzf filter a list of vim's commandline auto-completions. Try `:colo␣<c-l>` (be sure to include the space) or `:b␣<c-l>`. There are special cases for handling file-searches, so that you can go deeper into the path than just one directory at a time, and so that you can add multiple files to the arglist at once. More special cases could be added. Some limitations: the auto-complete for `:help` and `:tag` are limited to 300 entries, so you may need to narrow it a bit.
|
||||||
Reference in New Issue
Block a user