94

I write a lot of shell scripts and I am constantly having to enter in filepaths. I am wondering if anyone knows of a way to get Vim to autocomplete filepaths while in insert mode, just like when you are in your favorite shell you tab to complete the path of the directory or file.

Extra credit if you can get CTRLD functionality of the shell into Vim insert mode (i.e. to see what files/directories are in the current path).

3
  • interesting question, I would love to know the answer too :) Commented Dec 17, 2009 at 4:37
  • @stephenmm What does Ctrl-D do in your shell? In my bash shell it does nothing when I enter some text, else close the terminal. Commented Feb 15, 2018 at 6:19
  • @alhelal - It seems like CTRL-D would show the files and directories in some older shell of mine. I do not see any CTRL-D functionality anymore. Commented Jul 21, 2020 at 18:11

5 Answers 5

188

For file name omni completion, you can use:

Ctrl-XCtrl-F

Sign up to request clarification or add additional context in comments.

5 Comments

Another vim aha moment.... Thanks. I dont use tab in insert mode anyway so added this to my .vimrc: imap <Tab> <C-X><C-F>
Another good use for <tab> in insert mode is <c-n>. This will autocomplete the word you're typing based on other words in the file your working on. I use it all the time when typing variable or class names.
Is there any way to influence the search path? When adding #include entries in a C file the paths might not be relative to where you opened the file from.
@RobBradford try YouCompleteMe, if provided with right info (e.g. I use .ycm_extra_conf.py generated by YCM-Generator, which uses my cmake based buildsystem), it will nicely autocomplete any pathname in your include folders.
Vim fails to autocomplete when spaces are there in the filename. Is there a way to fix that?
21

There's ctrl-x ctrl-f

:he compl-filename

Comments

21

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.

2 Comments

In step 3 did you mean ctrl+x and ctrl+f? Because just ctrl+f isn't working for me, it does the same as ctrl+n. Btw it doesn't work here if a directory has spaces in it :/. do you know a workaround?
Please edit this answer, due to the inaccuracy in step 3 (which causes some confusion). Please amend the answer per @Rojo comment above, as it is otherwise helpful.
0

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.