How do I use the alternate file register to quickly reference the previous file?
Answer
"#p or <C-r># in insert mode
Explanation
The # register always contains the name of the alternate file — typically the file you were editing just before the current one. You can paste it, use it in commands, or switch to it instantly.
What is the alternate file?
- It's the file you most recently edited before the current one
- Displayed in
:lswith a#marker - Accessible via
<C-^>to switch to it
Using the # register
" Paste the alternate filename
"#p
" In insert mode
<C-r>#
" In command mode
:echo @#
" Open the alternate file in a split
:split #
" Diff current file with alternate
:diffthis | split # | diffthis
Related filename registers
| Register | Contains |
|---|---|
"% |
Current filename |
"# |
Alternate filename |
<C-r>% |
Current filename (insert/command mode) |
<C-r># |
Alternate filename (insert/command mode) |
Practical uses
- Insert a filename reference in your code:
// See also: <C-r># - Compare two files:
:split #then:windo diffthis - Copy the alternate filename for shell commands:
:!diff % #
Tips
<C-^>is the fastest way to toggle between two files — it uses the alternate file- The alternate file is per-window
:e #opens the alternate file (same as<C-^>)- If no alternate file exists,
@#is empty - Documented under
:help quote#and:help alternate-file