vimtricks.wiki Concise Vim tricks, one at a time.

How do I autocomplete file paths while typing in insert mode?

Answer

<C-x><C-f>

Explanation

The <C-x><C-f> command triggers filename completion in insert mode. Vim scans the filesystem relative to the current working directory and presents matching file and directory names in a popup menu. This is invaluable when typing import paths, require statements, configuration file references, or shell commands — you get tab-complete-style path resolution without leaving your editor.

How it works

  • While in insert mode, type the beginning of a file path
  • Press <C-x><C-f> to trigger filename completion
  • Vim reads the filesystem and shows matching files and directories in a popup menu
  • Press <C-n> or <C-p> to navigate through matches
  • Press <C-y> to accept the current selection, or <C-e> to dismiss the menu
  • Directories end with /, so you can press <C-x><C-f> again to continue completing deeper into the path

Example

You are writing a Python import and need to reference a config file:

config_path = "src/c"

With the cursor after c inside the quotes, press <C-x><C-f>. Vim shows:

src/config/
src/constants.py
src/cli.py

Select src/config/ with <C-n> and press <C-y>. Then press <C-x><C-f> again to complete files inside that directory:

src/config/database.yaml
src/config/logging.yaml
src/config/settings.py

Select the file you need and accept it.

Tips

  • Completion is relative to Vim's current working directory — check it with :pwd and change it with :cd
  • Absolute paths work too: type /etc/ and press <C-x><C-f> to browse system files
  • Home directory expansion with ~ works on most systems: type ~/ and trigger completion
  • If a path contains spaces, completion may break at the space — Vim treats spaces as word boundaries. Escape spaces with \ or avoid paths with spaces
  • Chain completions to drill into nested directories: accept a directory (ending in /), then immediately press <C-x><C-f> again
  • Set set wildignore=*.o,*.pyc,*.class,node_modules/** in your vimrc to exclude unwanted files from completion results
  • Combine with other completion types in the same insert session — you can use <C-x><C-f> for a path, then <C-n> for a keyword, all without leaving insert mode
  • If hidden files (dotfiles) don't appear, ensure the path prefix includes the dot: type ./ and complete from there

Next

How do I edit multiple lines at once using multiple cursors in Vim?