When in Vim insert mode, is there a way to add filepath autocompletion?
Asked Answered
S

5

91

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

Semple answered 17/12, 2009 at 4:36 Comment(3)
interesting question, I would love to know the answer too :)Gemina
@Semple What does Ctrl-D do in your shell? In my bash shell it does nothing when I enter some text, else close the terminal.Haug
@Haug - 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.Semple
B
181

For file name omni completion, you can use:

Ctrl-XCtrl-F

Bohunk answered 17/12, 2009 at 4:42 Comment(5)
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>Semple
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.Standing
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.Papillose
@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.Trig
Vim fails to autocomplete when spaces are there in the filename. Is there a way to fix that?Therron
O
21

There's ctrl-x ctrl-f

:he compl-filename

Ophthalmologist answered 17/12, 2009 at 4:41 Comment(0)
T
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.

Thievish answered 2/11, 2013 at 13:53 Comment(2)
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?Arriviste
Please edit this answer, due to the inaccuracy in step 3 (which causes some confusion). Please amend the answer per @Arriviste comment above, as it is otherwise helpful.Tobin
S
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!

Suburbanize answered 15/12, 2015 at 13:57 Comment(0)
Y
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.

Yogini answered 8/4, 2022 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.