VIM - How can I put my current position in jumplist?
Asked Answered
W

4

10

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.

Waspish answered 28/11, 2014 at 17:58 Comment(0)
C
12

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>.

Chartist answered 28/11, 2014 at 19:37 Comment(2)
it will append or just replace the last one ?Waspish
They will be appended; you can't replace those.Chartist
W
12

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.

Wordy answered 28/11, 2014 at 18:57 Comment(4)
I know about marks but its just not enough when you are analyzing logs, you have scroll too much. The standard jumps are not useful for this scenario too because I need more than one automatic stored jump.Waspish
@MaikoID: Why not set alphabetic marks manually when you need them?Wordy
Because I need to set them before I starting scrolling, and I simple can't remember to do that. I use marks to remember important things I think is wrong to use it to help scrolling but is my opinion.Waspish
You've got 26 of 'em (52 counting file marks). You can and should use them for whatever you want.Wordy
C
12

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>.

Chartist answered 28/11, 2014 at 19:37 Comment(2)
it will append or just replace the last one ?Waspish
They will be appended; you can't replace those.Chartist
S
2

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'".

Soldierly answered 28/2, 2019 at 14:50 Comment(0)
L
0

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>
Laurencelaurene answered 18/2 at 2:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.