質問

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?

役に立ちましたか?

解決

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...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top