bricoletc
bricoletc

Reputation: 554

In Vim, open a new window at a specified jump position

In vim, my code navigation adds jumps to the jumplist (viewed with :jumps).

I can open a new window at previous jump number x by doing CTRL-W s followed by x CTRL-O; is there an existing command to achieve this faster, and if not what user-defined key mapping could achieve this?

Upvotes: 1

Views: 244

Answers (1)

romainl
romainl

Reputation: 196916

There is no existing command specifically designed for that.

In command-line mode, what you are doing can be expressed relatively simply with:

:split +normal\ 4^O<CR>

with ^O obtained with Ctrl+VCtrl+O.

But how about creating a custom mapping?

Let's start with a simple mapping:

nnoremap <key> :split +normal\ 4^O<CR>

that only jumps to item #4 in the jump list. Lame.

What we would really like is to use :help v:count. For that, we need to make that dumb :split +normal\ 4^O smarter. This is done with :help :execute:

nnoremap <key> :execute 'split +normal\ ' . v:count . '<C-o>'<CR>

Our last issue is that, when we use a count before :, Vim automatically assumes it is a number of lines and inserts a range corresponding to that number. This is a very useful feature in general but we don't want it here because a) it doesn't represent a number of lines and b) :execute doesn't accept a range anyway. This is done with :help c_ctrl-u:

nnoremap <key> :<C-u>execute 'split +normal\ ' . v:count . '<C-o>'<CR>

which you would use like so:

5<key>

Upvotes: 1

Related Questions