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

How do I paste the name of the alternate (previously edited) file into the buffer?

Answer

"#p

Explanation

The # register holds the name of the alternate file — the file you were editing just before the current one. Using "#p in normal mode pastes this filename directly into the buffer, which is handy for constructing commands, inserting file references, or quickly noting paths.

How it works

  • "# specifies the # (alternate file) register
  • p pastes the register contents after the cursor
  • The alternate file is the one you most recently switched away from — the same target as <C-^> or :e #
  • In insert mode, you can use <C-r># to insert the same value while typing

Example

You're editing main.go and previously edited config.go. The alternate file is config.go.

"#p

Inserts config.go at the cursor position in normal mode.

In insert mode:

<C-r>#

Inserts config.go inline while typing.

Tips

  • The % register holds the current filename; # holds the alternate filename
  • Use "#P to paste before the cursor instead of after
  • The alternate file changes each time you switch files with <C-^>, :e #, :bn, etc.
  • Combine with :vs <C-r>#<CR> to open the alternate file in a vertical split from the command line

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?