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

How do I open or edit a file in the same directory as the file I am currently editing?

Answer

:e %:h/

Explanation

Vim expands % to the current file's path in Ex commands, and the :h modifier strips the last filename component to give you just the directory. Typing :e %:h/ followed by Tab opens a completion menu rooted at the current file's directory — letting you navigate and open sibling files without typing the full path.

How it works

  • % — expands to the current file's path
  • :h — the "head" modifier: strips the filename, leaving the directory
  • :e %:h/ — opens the Ex command prompt to edit, with the path pre-filled to the current directory
  • Press <Tab> after the / to trigger filename completion within that directory

Example

If you are editing /home/user/project/src/main.go and want to open utils.go in the same directory:

:e %:h/<Tab>
" Vim completes to: :e /home/user/project/src/
" then shows completions: main.go  utils.go  parser.go  ...

Select utils.go and press Enter to open it.

Tips

  • Other useful path modifiers for %:
    • %:t — tail (filename only, no directory)
    • %:r — root (filename without extension)
    • %:e — extension only
    • %:p — full absolute path
    • %:p:h — directory of the full absolute path
  • Chain modifiers: %:p:h:h gives the parent of the parent directory
  • Use :sp %:h/ or :vs %:h/ to open the sibling file in a split instead

Next

How do I make Vim automatically reformat paragraphs as I type so lines stay within the textwidth?