Omar Abid
Omar Abid

Reputation: 15976

Assign two actions to a VIM shortcut

I have the following keyboard shortcut in VIM

nnoremap C vi"

This enable me to select text between "" by clicking C.

Now I want this same shortcut, to do the same but for ''.

nnoremap C vi'

Putting these two rules doesn't work, as the last replaces the one before it.

Is there a way to make both of them work?

Upvotes: 0

Views: 103

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172510

Yes, but you need to build the intelligence into the mapping. This can be done via a :help :map-expr:

nnoremap <expr> C 'vi' . (getline('.') =~ '"' ? '"'  : "'")

This simplistic example will check whether the current line contains a double quote, and then select those, else single quotes. For a useful mapping, you probably need to ensure surrounding quotes on both sides (using search()), and if both types match select the "closer" one. With a :function, you can make that as complex as you like...

Upvotes: 2

Related Questions