2

I use quite a lot cw to change a word. I put the cursor on the start of the word, then cw followed by the new word and then <esc>.

So far, so good. My problem is that after that the cursor is at the end of the (new) word. I know I can move back with b, but it's possible to define a modified version of cw so that the movement is automatic at the end of the insert?

1
  • By the way, I discovered that that was a case of XY problem --- what I really want here is to learn about ciw (change inner word).
    – Rmano
    Mar 16, 2017 at 10:11

1 Answer 1

4

You can use a :help :map-expression that defines a fire-once autocmd on InsertLeave (i.e. when you're done with the change) to move back the cursor. I've used `[; instead of the b motion, because that also handles multiple inserted words. Since I wouldn't want to override cw, I've chosen c,w:

function! ToStartOfChange()
    augroup ToStartOfChange
        autocmd ToStartOfChange InsertLeave * execute 'normal! g`[' | autocmd! ToStartOfChange
    augroup END
    return 'cw'
endfunction
nnoremap <expr> c,w ToStartOfChange()
2
  • +1 and thanks, it works; but it has a problem: if I repeat the change after the first one with . the "going back" is not repeated. I am accepting the answer because I really did not specify that thing...
    – Rmano
    Mar 17, 2015 at 13:50
  • 1
    You need to use the repeat.vim plugin to make repeat work. Unfortunately, that requires some more boilerplate code, like <Plug> mappings. Mar 17, 2015 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.