简体   繁体   English

将输入传递给vimrc中的fzf快捷方式

[英]Passing input to a fzf shortcut in vimrc

I have a mapping in my vimrc to call fzf for a specific directory. 我在vimrc中有一个映射,可以为特定目录调用fzf。

: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. 我正在寻找一种将输入(当前日期为mmdd格式)传递到上面的映射的方法。

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: 如果它只是发出简单命令,然后进入插入模式(例如,通过:startinsert ),则只需将密钥附加到原始映射即可:

: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. 我不使用fzf(仅使用较旧的FuzzyFinder),但如果该插件执行更复杂的操作,则恐怕无法与该插件一起使用。 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; 在Vim中读取输入的整体实现非常简单:用户可以填充一个键缓冲区; 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. 如果该简单方法行不通,请使用:help feedkeys()这个低级函数,可以让您在任何未决键之后直接写入输入缓冲区。

: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. 除了难以避免的映射转义( \\<lt>而不是< )外,这只是将输入缓冲区的填充放在命令之前,但是这些键仅在命令之后执行。

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() ). 不幸的是,至少对于我的FuzzyFinder来说,它仍然无法正常工作,因为该插件自行使用feedkeys()来构建其UI(并且它需要启动插入模式并触发完成弹出窗口;如果没有,则无法完成此操作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. 这样,我们用完了Vim中的抽象,因此似乎没有办法,除了直接扩展插件,以便它以某种方式将“输入预设”作为可选参数。 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. 因此,如果确实遇到了fzf的相同问题,但认为这是一个重要功能,请建议对fzf的作者进行增强,或者使用单独的辅助映射来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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