beerbajay
beerbajay

Reputation: 20270

Combined history for vim sp and vsp?

Is there a way to easily open a file previously opened as a horizontal split as a vertical split in vim? Perhaps some method to combine the history for these commands?

When using vim, I often open multiple files with :sp or :vsp. Later, when I want to reopen a file previously opened with :sp as a vertical split using :vsp, the path doesn't exist in the history for :vsp so I have to look in the history for :sp as well and then change sp to vsp...

Use case:

:sp ../../some/very/long/annoying/to/type/path/to/a/file.cpp

:wq

... time passes ...

:vsp <up arrow> (not found, argh)

:sp <up arrow> (found, edit sp to vsp)

Upvotes: 3

Views: 364

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172520

This :autocmd will add to each :sp / :vsp command the corresponding :vsp / :sp command to the command-line history, so that you can recall with any other command:

:autocmd! BufWinEnter * if histget(':', -1) =~# '^v\?sp\s' |
\   call histadd(':', substitute(histget(':', -1), '^v\?', '\=empty(submatch(0)) ? "v": ""', '')) |
\   endif

Alternative

There are several plugins that offer a most-recently opened file list, e.g.

This also offer different mappings to open in (vertical) splits. With this, you avoid the need to decide on the split type before recalling, and you have a separate history just for the files.

Upvotes: 2

romainl
romainl

Reputation: 196476

You are doing all that in the same session

Buffers created with :e, :sp, :vs and various other commands are listed in the "buffer list". If you want to re-edit one of those buffers in a vertical window the original command used to edit the file doesn't matter. You simply do:

:vert sb <Tab>

You are doing that in two different sessions

Vim keeps a list of edited files between sessions. To re-edit a previously edited file in a vertical window, do:

:oldfiles
(decide you want the number 15)
:vs#<15

Upvotes: 3

Related Questions