Reputation: 38718
My vim plugin has a command to jump to a different location (just like tags).
I use the cursor
function for that.
How do I record the previous location in the jumplist, so that Ctrl+O works as expected?
Upvotes: 8
Views: 2554
Reputation: 156
I myself also wanted to this, and what works for me was mark ` before cursor move.
normal! m`
call cursor(l, c)
As help indicate, both setpos() and cursor() don't modify jumplist, so what's the difference of setpos() and cursor(), strange!
Upvotes: 13
Reputation: 8248
Simply mark that position before moving your cursor by setting the ' mark. This could be done by either using the normal mode m
command, or even a call to setpos()
, e.g. call setpos("''", getpos("."))
would add the current cursor position to the jump list.
Upvotes: 2
Reputation: 196556
:help cursor()
can't be clearer:
[…]
Does not change the jumplist.
[…]
(EDIT)
This means that cursor()
jumps are not recorded in the jumplist and therefore, that cursor()
is an inappropriate tool, here.
(ENDEDIT)
As an alternative, you could use something like
execute "normal " . target_line . "G" . target_col . "|"
which is perfectly compatible with <C-o>
and <C-i>
and just as idiomatic as
call cursor(target_line,target_col)
even if it gives the chills to JavaScripters ;-)
Upvotes: 5