How do I quickly switch between the current file and the last edited file?
Answer
<C-^>
Explanation
Pressing <C-^> (Ctrl-6 on most keyboards) instantly toggles between the current buffer and the alternate file — the last file you were editing. This is Vim's fastest file-switching mechanism, requiring zero thought about buffer numbers or file names.
How it works
- Vim tracks the "alternate file" in the
#register — it's the buffer you were in immediately before the current one <C-^>swaps the current buffer with the alternate buffer- Pressing it again swaps back, creating a rapid toggle between two files
- The Ex command equivalent is
:e #or:b #
Example
You're editing main.go and you open utils.go with :e utils.go. Now:
- Current file:
utils.go - Alternate file:
main.go
Press <C-^> to jump back to main.go. Press <C-^> again to return to utils.go. You can bounce between them endlessly.
This is shown in :ls output — the % marks the current buffer, # marks the alternate:
1 # "main.go" line 42
2 %a "utils.go" line 7
Tips
- Use
{count}<C-^>to switch to a specific buffer number:3<C-^>switches to buffer 3 - If you get
E23: No alternate file, it means you haven't visited a second file yet in this session - The alternate file is per-window, so each split maintains its own alternate
- Combine with splits:
<C-w><C-^>opens the alternate file in a horizontal split - This is especially powerful during development when you're bouncing between a source file and its test file, or between a header and implementation
- Map it to something easier if
<C-^>feels awkward:nnoremap <leader><leader> <C-^>