Is there any way to bind cI<char> to forward to next ocurrence and change inside <char>?



, pressing cI( would result in


, [] represents cursor in normal mode and | represents cursor in insert mode.

So in summary, cI( would represent a shortcut to a key sequence like this f(ci(.

1 Answer 1


How about cI<char><CR>?

The LHS of a mapping is a static string. You can't have a "dynamic" mapping with, say, a placeholder character that gets replaced with whatever character the user types. I suppose you could brute-force it and enter mappings for all the relevant chars: cIw, cIW, cI(, etc. I'm guessing you're hoping for something better than that.

I can think of one way to get close to what you want. Create a function and mapping like these (you can put them in your vimrc file):

function! ChangeInsideChar()
    let l:char = input("Enter a character: ")
    exec 'normal! f' . l:char . 'di' . l:char

nnoremap cI :call ChangeInsideChar()<CR>

When you hit cI in Normal mode a prompt will appear. Enter the appropriate character then hit Enter and the magic will happen. Using your example, if at the prompt you entered ( and Enter that will delete the text inside the next set of parens and leave you in Insert mode.

You can enter these sequences pretty quickly so in effect this solution is like just adding one extra key press to your hoped for binding. Heck, I've already thought of a few ideas for using this general technique myself.

Update: Thanks to a pointer from Rich, here, we can eliminate Enter because Vim has a single char fetch function. Here's the new and improved function:

function! ChangeInsideChar()
    echomsg 'Enter a character '
    let l:mychar = nr2char(getchar())
    exec 'normal! f' . l:mychar . 'di' . l:mychar
  • 1
    Using getchar() instead of input(...) gets even closer. (You also have to use nr2char(l:char) instead of l:char in the execute line.)
    – Rich
    Dec 4, 2017 at 10:11
  • @Rich Oh, awesome. I wondered if there was a single char input function but hadn't gotten around to looking for it. I'll update my answer to incorporate that if you don't mind.
    – B Layer
    Dec 4, 2017 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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