Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The way the jump list works is very often misunderstood. A jump, as stored in the jump list, only concerns itself with 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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading