90

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).

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
stephenmm
  • 2,640
  • 3
  • 30
  • 48
  • interesting question, I would love to know the answer too :) – Jay Zeng Dec 17 '09 at 04: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. – alhelal Feb 15 '18 at 06: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. – stephenmm Jul 21 '20 at 18:11

5 Answers5

179

For file name omni completion, you can use:

Ctrl-XCtrl-F

Conner
  • 30,144
  • 8
  • 52
  • 73
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 12
    Another vim aha moment.... Thanks. I dont use tab in insert mode anyway so added this to my .vimrc: imap – stephenmm Dec 17 '09 at 05:19
  • 5
    Another good use for in insert mode is . 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. – Derek Dec 18 '09 at 15:51
  • 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. – Rob Bradford Jan 09 '14 at 11:46
  • @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. – Emile Vrijdags Nov 01 '17 at 19:03
  • Vim fails to autocomplete when spaces are there in the filename. Is there a way to fix that? – Jean Oct 21 '18 at 16:53
21

There's ctrl-x ctrl-f

:he compl-filename

andyg0808
  • 1,367
  • 8
  • 18
michael
  • 11,667
  • 2
  • 26
  • 25
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.

SnapShot
  • 5,464
  • 5
  • 42
  • 40
  • 7
    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? – Rojo Dec 23 '14 at 00:58
  • 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. – Chris Jun 21 '21 at 08:34
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!

amonowy
  • 188
  • 12
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.

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147