简体   繁体   中英

What is difference between "i "and "t" tag? in VIM editor?

Let's say I have

'hello'

and my cursor is on h or ' .

When I command ci' or ct' , they're doing the same thing?

So what are those i and t ? What's the difference? Thanks

Both ci' and ct' can be decomposed as such:

<operator><motion>

where:

  • c is the operator , meaning "change" in this case,
  • i' and t' are two kinds of motions.

t' covers the text from the cursor to before the next ' , going in one direction only.

i' belongs to a special kind of motions called text objects. It covers the text between the first single quote at or before the cursor and the first single quote at or after the cursor, going in two directions.

This image, from Practical Vim , does a great job explaining the difference between the two kinds of motions ( t' on the left, i' on the right):

忍者

If your cursor happens to be positioned on one of the boundaries of i' , then both motions have the same effect because they cover the exact same text:

foo 'hello' bar
     <--->         i'
     ---->         t'

If your cursor doesn't happen to be positioned on one of the boundaries of i' , then the two motions won't have the same effect:

foo 'hello' bar
     <--->         i'
       -->         t'

This means that these motions can have the same effect given the correct conditions, despite not being functionally identical .

FWIW, there are a number of alternatives that would also work in your case:

foo 'hello' bar
     <--->         iw
     ---->         fo
     ---->         e

Vim's normal mode commands work like a language, with every "word" having a very specific meaning. This allows you to tell Vim exactly what you mean. In this very specific case , "change text in this quotes" ( ci' ), "change to next quote" ( ct' ), "change this word" ( ciw ), "change to and including o " ( cfo ), and "change to the end of the word" ( ce ) have the exact same effect but they are the result of different thinkings.

Pick the one that makes the most sense in the current context.

Reference:

:help motion.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM