Reputation: 5007
I am wondering about a improvement for vim that I can jump back for where I was the last time I stopped to move around for like 3 seconds. Then I can scroll around and have a kick shortcut to jump back where I was with the same old CTRL+o.
Vim put a lot of movements in the :jumps list and I wondering that half of that is useless in any moment for me, so how could I do that ?
In any another words I'm trying to make the jumplist more sensible.
ps: Netbeans and VS has a similar behavior.
Upvotes: 10
Views: 3018
Reputation: 2666
I have the following vimrc
setup. It only adds new jumps when CursorHold
triggers in a paragraph that is not either 1) the location of the most recent m'
set by CursorHold
jump or 2) the location of the currently selected location in the jump history (e.g. when scrolling with <C-o>
/<C-i>
).
function! s:reset_jumps() abort
let [line1, line2] = [line("'{"), line("'}")]
let [items, pos] = getjumplist()
let pos = min([pos, len(items) - 1]) " returns length if outside of list
let [prev, curr] = [line("''"), items[pos]['lnum']]
if (line1 > curr || line2 < curr) && (line1 > prev || line2 < prev)
call feedkeys("m'", 'n') " update mark to current position
endif
endfunction
augroup jumplist_setup
au!
au CursorHold * call s:reset_jumps()
augroup END
I also disable conventional jump setting behavior as follows:
noremap n <Cmd>keepjumps normal! n<CR>
noremap N <Cmd>keepjumps normal! N<CR>
noremap ( <Cmd>keepjumps normal! (<CR>
noremap ) <Cmd>keepjumps normal! )<CR>
noremap { <Cmd>keepjumps normal! {<CR>
noremap } <Cmd>keepjumps normal! }<CR>
Upvotes: 0
Reputation: 663
You can set a mark in the '
register. The keystrokes would be m'
I found this in vim's documentation :help jumplist
.
You can explicitly add a jump by setting the ' mark with "m'".
Upvotes: 2
Reputation: 172778
To have the current position automatically added to the jump list, you can utilize the CursorHold
event:
:autocmd CursorHold * normal! m'
Navigation is with the default <C-o>
/ <C-i>
.
Upvotes: 13
Reputation: 30181
It's common to use the m
and '
commands to jump around. For instance, you can type mx
, go off and do some things, and then do 'x
to jump back to where you were when you did mx
. This works for every letter of the alphabet (i.e. ma
, mb
,... mz
). Uppercase marks (mA
, mB
,...) will also remember the filename so you can jump between files. There are a number of other special marks you can set for various purposes.
The "previous context mark" is called '
. It is set automatically when you jump around, and can also be manually set with m'
. Jump to it with ''
. The m'
command also manually adds a jump to the jumplist. ''
is roughly equivalent to Ctrl+O, but doesn't take a count.
If you replace the apostrophes in these commands with backticks, Vim will jump to the specific column rather than just the line. In practice, apostrophe is easier to type and often close enough.
Upvotes: 12