Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

vim - Why ci" and ci(, ci{.... behave differently?

we all know what ci" ci' ci( ci[ ... does. Very handy in everyday's editing. I found something strange, and checked the help, didn't find out why.

say, I have a file:

foo "target"
foo 'target'
foo (target)
foo {target}
foo [target]
foo <target>

if my cursor at the beginning of each line, (on the 'f'), then I type ci", ci', ci(...

the cix works only with quotes (single or double), doesn't work for brackets. why do they behave differently?

(dix, vix the same)

tested with --noplugin, vim 7.3

thank you.

Update

thanks @romainl for the answer. I still have doubt regarding the "pair processing in vim"

check this example:

foo "targ"eti" some"thing else "

if I have a line like above, I type ci", no matter cursor is at beginning or between quotes, it works perfectly, it seems that vim does have the idea of "pair"?

and this maybe what you meant about the pairing ?

foo "target x some"thing else "
foo (target x some(thing else )

I have above two lines, if (cursor at x) I type ci" and ci(, nothing happened to 2nd line, but first line changed into:

foo "I"thing else " (I is cursor)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ci( is consistent with ci[, ci{ and cit and all the other <action>i<something>. Only ci' and ci" work like they do. The outliers are the quotes, here, not the brackets.

Vim doesn't consider that quotes come in pairs while brackets do. It has an internal logic for matching pairs that works with actual pairs but not with quotes hence the difference in behavior.

You are not the first to complain about that discrepancy: this is one solution, maybe you can find others.

edit

I don't have a deep knowledge of Vim's internals, unfortunately, so I can only make assumptions, here.

If you ask Vim to do ci" it does its best to find a pair of double quotes but double quotes don't go by pairs: there's no way to tell if a " is the closing one or the opening one contrary to brackets. Because of that, Vim must make some choices. IMO, the choice that would make the most sense considering how the other members of the family work, would be to assume that the cursor is between the quotes and select from the first one to the right to the first one to the left. I can only assume that this method proved wrong in some way or didn't work for some reason and that the other method (the current one) prevailed.

Another explanation could be that the i<something> mechanism is somehow tied to a specific subsystem (maybe the same as showmatch?) that is unable to deal correctly with quotes.

Anyway, just like you, I find this discrepancy weird and I've somehow internalized it and aligned my usage of <action>i" to how the others work. To the point of actually doing 2t"ci" or some variant instead of ci"!! Inefficient, I know.

Did you read :h a'? I completely forgot where I got my "limited understanding" of the issue but it was there! It says:

"Only works within one line. When the cursor starts on a quote, Vim will figure out which quote pairs form a string by searching from the start of the line."

What I get from that is this: for some reasons unknown to us, Vim uses another mechanism for matching quotes than for the other pairs and that is why ci" is different from ciband friends. The underlying cause is not clear at all but I'm fairly certain that the big picture looks a lot like what I imagine.

To me, it looks a lot like a bug or a limitation disguised as a feature.

If you are still curious, I'd suggest you ask any further question on vim-dev.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...