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.mdand./scripts/b<C-x><C-f>finds nothing, because vim looks for~/scripts/, not~/project/scripts/. Run:pwdto check, and:lcd %:hto 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 forscr, so<C-n>does not go inside; it cycles back to thescryou 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/depcomes 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.