'how to set vim map not change jumplist?

[first line]

int test() {
    a   
    b
    c
    d
    {
        e
        f   
        g   
    }
}
  1. The cursor in currently in line with f.
  2. If I press [[, the cursor will go to [first line].
  3. I want cursor goto int test() {. So I set a mapping: nnoremap [[ ][%.
  4. The map command works good, with one problem: if I press CTRL+O, cursor will go to the last line }.
  5. What I want is the cursor to go back to the line with f.

I tried nnoremap [[ :keepjumps normal ][%<CR>, but it does not work.

How can I implement this?

  1. press [[ make cursor goto line with int test(){.
  2. press CTRL+O make cursor jump back.

My vim version is Vi IMproved 7.4

vim


Solution 1:[1]

The way the jump list works is very often misunderstood. A jump, as stored in the jump list, only records the origin of the "jump" you made, not its destination. This means that :help :keepjumps is supposed to be used to prevent a new origin to be added to the jump list.

In order for <C-o> to jump back to the origin f, that origin must be present in the jump list so it's only the origin } that must be left out.

In doing:

nnoremap [[ :keepjumps normal ][%<CR>

you are effectively preventing both the origin f and the origin } to be added to the jump list.

You can fix your mapping by moving ][ out of the :keepjumps command:

nnoremap [[ ][:keepjumps normal %<CR>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1