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

How do I access the alternate file register?

Answer

"# register

Explanation

The # register contains the name of the alternate file — the previously edited file in the current window. You can use it in commands and paste it.

How it works

  • "# stores the alternate filename
  • "#p pastes the alternate filename
  • <C-r># inserts it in insert/command mode
  • <C-^> switches to the alternate file

Example

While editing main.go after previously editing utils.go, "#p inserts utils.go.

Tips

  • :e # opens the alternate file
  • :ls shows # next to the alternate buffer
  • Useful in scripts and mappings that reference the previous file
  • <C-^> is the fastest way to toggle between two files
  • The alternate file is set when you switch buffers

Next

How do you yank a single word into a named register?