How do I open the previous file I was editing in a split window?
Answer
:split #
Explanation
In Vim, # is a special filename that always refers to the alternate file — the most recently active buffer before the current one. Passing # to :split (or :vsplit) opens that file in a new split without having to remember or type its name, letting you quickly set up side-by-side editing of two related files.
How it works
#— expands to the name of the alternate file (shown as#in:ls):split #— opens the alternate file in a horizontal split:vsplit #— opens it in a vertical split- The alternate file is set whenever you switch buffers with
<C-^>,:b,:e, or any other buffer command
Example
You are editing routes.py and switch to models.py. Now:
:vsplit #
Opens routes.py in a vertical split, so you can see both files at once without navigating away.
You can also use # in other Ex commands where a filename is expected:
:read # " insert alternate file contents into current buffer
:diff # " diff current file against the alternate file
Tips
<C-^>(or<C-6>) switches between current and alternate file without a split:echo expand('#')prints the alternate file's full path — useful in mappings- If no alternate file is set yet, Vim will report
E23: No alternate file - Use
<C-w>s #<CR>for the same effect as:split #without leaving normal mode