How do I edit another file without changing the alternate-file (#) reference?
Answer
:keepalt edit path/to/file\<CR>
Explanation
Experienced Vim workflows often depend on the alternate file (#) for fast toggling with <C-^>, quick diffs, or two-file review loops. A normal :edit updates that alternate-file pointer, which can break your navigation rhythm. :keepalt solves this by running a command while preserving the current alternate-file reference.
How it works
:keepalt edit path/to/file
:keepaltis a command modifier- It applies to the Ex command that follows (
editin this case) - Vim opens the target file, but does not replace the current
#alternate-file value
Example
Assume you are toggling between app/main.go and app/main_test.go using <C-^>. Midway through debugging, you want to inspect docs/notes.md briefly.
:keepalt edit docs/notes.md
After checking notes, <C-^> still jumps back through your original code/test pair, not to docs/notes.md. This keeps established context-switch shortcuts intact while you take side trips.
Tips
- Combine with other commands:
:keepalt split file,:keepalt bnext, or:keepalt drop file - Use this in mappings where preserving alternate-file semantics is important for repeatable navigation
- If your workflow heavily relies on
#, this is a low-friction way to avoid subtle context loss during refactors