← back to latest

Complete a file path while you type it

INSERT<C-x><C-f>Complete a file path while you type it

anatomy

<C-x>
In insert mode, enter the completion submode. Vim waits for a second key that says what kind of thing to complete: lines, keywords, tags, file names, and more.
<C-f>
Complete a file name. Vim takes the path-like text before the cursor, looks it up on disk relative to the current working directory, and inserts the first match. Further <C-f> or <C-n> presses move to the next match, <C-p> to the previous one.

In the buffer

You are writing a README and need the name of the deploy script. Vim was started from the project root, where scripts/ holds build.sh, deploy-staging.sh, and deploy.sh. You are in insert mode and have typed this much:

# Deploy

Run ./scripts/dep
                 ^

Press <C-x><C-f>. Vim inserts the first match in alphabetical order, which is deploy-staging.sh, and opens a menu with both matches. Press <C-n> to move to the second one, then <Esc>:

# Deploy

Run ./scripts/deploy.sh
                      ^

The ./ is optional. scr followed by <C-x><C-f> completes to scripts/, and pressing <C-x><C-f> again continues into the directory and inserts scripts/build.sh.

When you would reach for it

You are typing a path into a config file, a README, an import, or a shell script, and you do not remember whether the file is deploy.sh, deploy.bash, or deploy-prod.sh. The usual fix is to leave vim, run ls, come back, and type it out, with a fair chance of a typo. <C-x><C-f> reads the directory for you and puts the real name in the text, so the path is correct because it came from the disk.

Gotchas

  • Paths are resolved against vim’s current working directory, not the directory of the file you are editing. Start vim in your home directory with vim project/README.md and ./scripts/b<C-x><C-f> finds nothing, because vim looks for ~/scripts/, not ~/project/scripts/. Run :pwd to check, and :lcd %:h to make the current window’s directory match the file.
  • Hidden files only show up once you type the leading dot. ./scripts/<C-x><C-f> skips .env; ./scripts/.<C-x><C-f> includes it, along with the ./ and ../ entries.
  • Completing a directory stops at the slash. scripts/ is the only match for scr, so <C-n> does not go inside; it cycles back to the scr you typed. Press <C-x><C-f> again to list the directory’s contents.
  • If none of the matches is right, <C-e> cancels the completion and leaves the text exactly as you typed it, so ./scripts/dep comes back. <C-y> accepts the highlighted match and closes the menu without inserting anything else.

Variants

~/.con<C-x><C-f>

Completes from your home directory, so this finds ~/.config/. The ~ stays in the text instead of being expanded to the full path. Absolute paths like /etc/ng<C-x><C-f> work too.

bash<C-x><C-l>

Whole-line completion. Vim searches the open buffers for a line that starts with bash and inserts all of it, for example bash ./scripts/deploy.sh --env prod. Useful for repeating a command line that already appears elsewhere in the file.

gf

The opposite direction, in normal mode. With the cursor on ./scripts/deploy.sh, gf opens that file in the current window. Like <C-x><C-f>, it looks up relative paths from the working directory, though it also searches the directories in 'path'. In Vim it refuses to leave a buffer with unsaved changes unless 'hidden' is set; Neovim sets 'hidden' by default.

lineage

Vim's CTRL-X completion submode predates Vim 5.0, whose release notes in 1998 list it among the features that were improved rather than added. File name completion has been one of its submodes since then. Vim 7.0 in 2006 added the popup menu that lists every match at once. Before that, you cycled through matches one at a time and watched them replace each other in the text.

-- INSERT --vim-hacks.com033-033-complete-a-file-path-while-typing.html
utf-81,1All