如何在命令模式下使用Tab创建地图组合?

时间:2022-12-03 20:45:31

I want to create a key combination that would copy the text under the cursor, open the :find command, yank the word and then press Tab to autocomplete to the first filename in the list, so (in 99% of cases) I would have just to press enter to open the file.

我想创建一个组合键,复制光标下的文本,打开:find命令,拉出单词,然后按Tab键自动完成列表中的第一个文件名,所以(在99%的情况下)我会有只需按Enter键即可打开文件。

map <Leader>o yw:find <C-R>"<Tab>

However, when I press <Leader>o, I get :find FileName^I in the command line instead. How can I make it react the same way as if I pressed the key myself?

但是,当我按 o时,我得到:在命令行中找到FileName ^ I。我怎么能像我自己按键那样做出反应呢?

2 个解决方案

#1


You need the wildcharm options:

您需要wildcharm选项:

set wildcharm=<C-z>
map <Leader>o yw:find <C-R>"<C-z>

See :help 'wildcharm'.

请参阅:help'wildcharm'。

Here is a more solid, non-recursive, alternative that doesn't clobber the unnamed register for no reason:

这是一个更加可靠,非递归的替代方案,不会无缘无故地破坏未命名的寄存器:

nnoremap <leader>o :find <C-r><C-w><C-z>

#2


The gf (goto file) command should do the same thing as your mapping. This opens the file whose name is under or after the cursor.

gf(goto文件)命令应该与映射完成相同的操作。这将打开名称位于光标之下或之后的文件。

If not getting the expected behaviour, it's worth checking the contents of the isfname option (use set isfname? to check). This specifies a list of characters that are treated as valid characters for file path names.

如果没有得到预期的行为,那么值得检查isfname选项的内容(使用set isfname?来检查)。这指定了被视为文件路径名的有效字符的字符列表。

It’s also worth checking / setting the contents of Vim’s path option which lists the directories which are searched when using gf, :find and similar commands, e.g., the default setting on MS Windows is .,,:

还有值得检查/设置Vim路径选项的内容,该选项列出了使用gf时搜索的目录:find和类似命令,例如,MS Windows上的默认设置是。,,:

  • . searches relative to the directory of the file currently being edited
  • 。相对于当前正在编辑的文件的目录进行搜索

  • ,, (empty string) searches the current directory; use the cd command to check (or set) your current directory.
  • ,,(空字符串)搜索当前目录;使用cd命令检查(或设置)当前目录。

See

  • help gf
  • help isfname
  • help path

#1


You need the wildcharm options:

您需要wildcharm选项:

set wildcharm=<C-z>
map <Leader>o yw:find <C-R>"<C-z>

See :help 'wildcharm'.

请参阅:help'wildcharm'。

Here is a more solid, non-recursive, alternative that doesn't clobber the unnamed register for no reason:

这是一个更加可靠,非递归的替代方案,不会无缘无故地破坏未命名的寄存器:

nnoremap <leader>o :find <C-r><C-w><C-z>

#2


The gf (goto file) command should do the same thing as your mapping. This opens the file whose name is under or after the cursor.

gf(goto文件)命令应该与映射完成相同的操作。这将打开名称位于光标之下或之后的文件。

If not getting the expected behaviour, it's worth checking the contents of the isfname option (use set isfname? to check). This specifies a list of characters that are treated as valid characters for file path names.

如果没有得到预期的行为,那么值得检查isfname选项的内容(使用set isfname?来检查)。这指定了被视为文件路径名的有效字符的字符列表。

It’s also worth checking / setting the contents of Vim’s path option which lists the directories which are searched when using gf, :find and similar commands, e.g., the default setting on MS Windows is .,,:

还有值得检查/设置Vim路径选项的内容,该选项列出了使用gf时搜索的目录:find和类似命令,例如,MS Windows上的默认设置是。,,:

  • . searches relative to the directory of the file currently being edited
  • 。相对于当前正在编辑的文件的目录进行搜索

  • ,, (empty string) searches the current directory; use the cd command to check (or set) your current directory.
  • ,,(空字符串)搜索当前目录;使用cd命令检查(或设置)当前目录。

See

  • help gf
  • help isfname
  • help path