vimvim-macros

Vim method to jump to number under the cursor


In vim, is there a more elegant way to jump to a line number under the cursor than

map gn "nyw@nG

"gn" or some other key combo? This necessitates ensuring the cursor is at the beginning of the string of numbers. Also, this macro COULD be a bit hazardous depending on what's in register "n".

Not entirely sure even why this works: @n means to execute what's in register n (a number), not concatenate it with the "G".

Functions with something like

call cursor(expand("<cword>"),1)

don't seem to work either.

Thoughts?


Solution

  • I would recommend this:

    nnoremap gn yiw:exec (@" =~ '^\d\+$' ? 'norm @"G' : '')<cr>
    

    It fixes a lot of the problems you have mentioned, for example:

    There are also some other advantages:

    not entirely sure even why this works: @n means to execute what's in register n (a number), not concatenate it with the "G".

    Typing @n inserts the text inside the register n into the typeahead buffer, exactly as if you had typed it. So if a command is unfinished, it will sit there (as if you had typed it) waiting for the command to finish. You could even run a macro like

    3d
    

    which will sit there waiting for a motion, before deleting three of those motions.