在 Vim 插入模式下,是否有添加文件路径自动完成的方法?

我编写了很多 shell 脚本,并且经常需要输入文件路径。我想知道是否有人知道在插入模式下让 Vim 自动完成文件路径的方法,就像在您最喜欢的 shell 中使用制表符来完成目录或文件的路径一样。

如果可以将 shell 的 CTRLD功能调入 Vim 插入模式(即查看当前路径中的文件/目录) ,则可以获得额外的好处。

35892 次浏览

There's ctrl-x ctrl-f

:he compl-filename

For file name omni completion, you can use:

Ctrl-XCtrl-F

To build on @CMS and @michael excellent answers

When using ctrl+X ctrl+f command sequence it will display a list of files in the current directory. I spend a minute looking for the right key to move up and down between the different filenames. The correct keys are Ctrl-n and Ctrl-p. You can use almost any other key (like Space) to select and continue typing.

In addition, if you are not already at the file/directory you would like to insert, you can go down a file tree structure as follows:

  1. Optionally enter some part of the directory. You can even use ../../ for example!
  2. Press ctrl+X ctrl+f and select the first item in the tree from the list.
  3. Next press ctrl+f again while the correct item is highlighted to display a list of the next level down the directory tree structure.

You can keep doing this until you find the directory/file you would like to insert in the document.

I experienced similar problem. I found solution like:

    sudo apt-get install realpath

And in VIM naviagte to file with in normal mode type:

    :r !realpath /path/to/file

When you are navigating in non-insert mode after !realpatch you are able to use our key button.

VOILA! TAB is working again!

edit: excuse me, I landed here from a google result for "vim insert file absolute path"

(first leave insert mode with esc or ctrl+c) ;)

from normal mode, on a blank line

!!readlink -f #

this will run a command, and substitute # with the current file name, readlink will resolve a canonical name, and !! will write the output where the cursor was

note, this needs to be done on a blank line, as the content of the line will be fed as stdin to the subcommand.