Im very new to vim scripting. I have the following script in my .vimrc:
- If there is no user input aka I press
<CR>
=> go to recent buffer - If input is 'd' => delete all buffers except for the current one
- Else go to inputted buffer.
"Fast buffer switch
function Warp()
ls
call inputsave()
let bufname = input('warp: ')
call inputrestore()
if bufname == ""
b#
elseif bufname == "d"
%bd|e#|bd#
else
exe "b " . bufname
endif
endfunction
nnoremap <silent> <C-f> :call Warp()<CR>
The :b
command works so that I dont have to write the full filepath but only a substring of the file name. For example I can navigate to bar/foo/helloworld.js
with :b hello
.
However, sometimes file names can overlap which causes :b
to error out. An intuitive solution to this is to use tab-completion to select the correct file when using :b
on the command line. However, I dont know how this could be integrated into my function or even if it is possible.
Somethings I noticed:
nnoremap <C-f> :b
allows me to use tab-completiongetchar() == 13
allows me to detect<CR>
- Maybe something with feedkeys but I dont know if that works with cmdline. Some further ideas I have but dont think is possible in simple vim scripting is catching the error so that the user can revise or use tab-completion the file name inputted clashes.
Thank you in advance. Any advice is greatly appreciated.
:h :command-complete
: The idea would be to create a new command (:h user-commands
)Warp
which will have-complete=buffer
and which will call yourWarp()
function. This way you can use:Warp hel<tab>
and you will get the same completion as for:b hel<tab>
:Warp
working: Imgur. The problem is:ls
no longer works. When I map using|
,:Warp
is not invoked as shown in the picture.