How does one keep the cursor in its place after a file-replace using :%s/find/replace/g
? I looked at restoring cursor position, but not quite what I was looking for, and I could jump back to previous position using ''
.
2 Answers
The anwolib plugin has a :KeepView
command:
:KeepView %s/find/replace/g
-
Is there a way to "enable"
KeepView
for all find-replace operations? I'm trying to minimize typing (_:– KM.Commented Mar 16, 2015 at 17:57 -
1You can create a shorter
:KV
alias command, or even:kv
using cmdalias.vim. To be even more extreme::nnoremap : :KeepView<Space>
Commented Mar 16, 2015 at 19:59 -
1
I would suggest something like:
fun! SubstiTute()
let l = 1
let limit = line('$')
while l < limit
call setline(l, substitute(getline(l), 'this', 'that' , 'gc'))
let l = l + 1
endwhile
endfun
Using setline() in conjunction with substitute() your jumplist and changelist will not be messed up.
''
or<C-o>
is sort of the answer :-) Do you want it to happen automatically?