简体   繁体   中英

Passing input to a fzf shortcut in vimrc

I have a mapping in my vimrc to call fzf for a specific directory.

:map <F4> :Files my_directory_path_here <CR>

This works fine. This brings up a popup window and I can search a part of the file to see all possible files.

I am looking for a way to pass an input (current date in mmdd format) to my mapping above.

I looked up and I was able to find a way to map this to a different key.

imap <F5> <C-R>=strftime("%m%d")<CR>

But how do I append this as an input to my key mapping?

Thank you.

That depends on how the plugin is implemented. If it just issues straightforward commands and then goes into insert mode (eg via :startinsert ), this is a simple matter of appending the keys to the original mapping:

:map <F4> :Files my_directory_path_here <CR><C-R>=strftime("%m%d")<CR>

I don't use fzf (only the older FuzzyFinder), but I'm afraid that won't work with the plugin, if it does more complex things. Here's a demo that works for me (so that you believe me):

:map <F4> :startinsert<CR><C-R>=strftime("%m%d")<CR>

The overall implementation of reading input in Vim is quite simple: There's a key buffer that gets filled by the user; whenever a complete command is detected, reading from the buffer is suspended, the command executed, and then reading continues (even if the command has changed the current mode in the meantime).

If that simple approach doesn't work, there's :help feedkeys() , a low-level function that lets you directly write to the input buffer, after any pending keys.

:map <F4> :call feedkeys("\<lt>C-R>=strftime('%m%d')\<lt>CR>", 't')<Bar>startinsert<CR>

Apart from the ugly escaping for the mapping ( \\<lt> instead of < ), this just puts the filling of the input buffer before the command, but those keys are only executed after it.

Unfortunately, at least for my FuzzyFinder, this still doesn't work, because the plugin uses feedkeys() on its own to build up its UI (and it needs to start insert mode and trigger the completion popup; stuff that cannot be done without feedkeys() ). With that, we're running out of abstractions in Vim, so there doesn't seem to be a way, except for directly extending the plugin so that it takes a "input preset" as an optional argument somehow. So, if you indeed run into the same problems with fzf but think that this is an important feature, please suggest an enhancement to fzf's author, or live with the workaround of a separate secondary mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM