Quantcast
Channel: User Hubro - Stack Overflow
Viewing all articles
Browse latest Browse all 45

How do I save the current cursor position and load it later in Vimscript?

$
0
0

I've written a function for removing the excess white space in a file:

let g:trim_whitespace = 1function! TrimWhitespace()    if g:trim_whitespace        normal :%s/\s\+$//e    endifendfunction

The issue is that the cursor position is set to [1, 1] after the substitution command. I don't want the cursor to move at all, so I tried to save the cursor position and reset it after the substitute command:

let a:cursor_pos = getpos(".")normal :%s/\s\+$//eexec cursor(a:cursor_pos[1], a:cursor_pos[2])

But still the exact same thing happens, as if the call to cursor had no effect. Any ideas?


Viewing all articles
Browse latest Browse all 45

Trending Articles