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

How do I open another file without changing the alternate-file mark (#)?

Answer

:keepalt edit {file}<CR>

Explanation

The alternate-file mark (#) powers fast toggles like <C-^> and commands that depend on the previous file context. In larger workflows, opening helper files can accidentally overwrite that context and break your jump rhythm. :keepalt solves this by running a command while preserving the current alternate-file value.

How it works

  • :keepalt modifies the behavior of the next Ex command.
  • edit {file} opens a different file in the current window.
  • With keepalt, Vim does not update the alternate-file mark for this operation.
  • Result: temporary file visits do not disturb your primary two-file toggle workflow.

Example

Assume you are bouncing between handler.go and handler_test.go via <C-^>. You briefly need to inspect go.mod but want your alternate-file pairing untouched. Run:

:keepalt edit go.mod

After checking go.mod, your alternate-file relationship remains intact, so <C-^> still toggles between handler.go and handler_test.go instead of being hijacked by go.mod.

Tips

  • Use with other commands too, such as :keepalt split {file} or :keepalt b {buf} when context preservation matters.
  • This pairs well with quickfix-driven edits, where transient jumps can otherwise pollute alternate-file state.
  • If your mappings rely on #, adding keepalt in scripted hops makes those mappings much more predictable.

Next

How do I open another file without saving the current modified buffer first?