18

Here's a link I find in stackoverflow about this question.

but I still cannot understand without a specific example about what's the difference between "change inner word" and "change word".

I test these two commands in my vim and finally find no differences ,Please give me an example to help me understand, thank you!

Community
  • 1
  • 1
Alec.Zhou
  • 417
  • 2
  • 4
  • 11
  • NB: there is an incipient SE site for [vi/vim knowledge](https://vi.stackexchange.com) – chb Oct 31 '17 at 07:29

4 Answers4

41

Here is an example:

foo bar baz
    ^

Typing cw and ciw will give the same result in this case (with the cursor positioned on the 'b' of 'bar'). Consider this:

foo bar baz
     ^

cw will yield

foo b baz
     ^

where ciw will yield

foo  baz
    ^

so it changes the whole word, regardless of the cursor position. Very useful, i love this command. Very useful is also the caw (or the aw) command:

foo bar baz
     ^
-> caw
foo baz
    ^

aw also contains the space. Try these commands with the v (visual) command, to see what they all do. Also, read motion.txt, part 6.

pschulz
  • 1,406
  • 13
  • 18
  • 1
    It's very kind of you to provide me so much extra information, thank you so much , I understand it now! It is like a magic! – Alec.Zhou Jan 20 '17 at 07:42
18

Above answers addressed HOW they behave different, i would like to share some idea on WHY they behave different.

First, in vim's world, w and iw have different semantics and can both be seen as target(see below):

  • w in cw refers to a Motion Object
  • while iw in ciw refers to a TextObject

Secondly, in Vim editing, Operator (c/change in your case, and y/yank, d/delete, =/indenting, >/shifting etc) and Target (w and iw in your case) helps you achieve what you want to do with less keystrokes compared to using visual selection + operator.

TextObject defines target range (start and end position).

Motion is essentially used to move cursor, so it only define final-target-position (destination to move).

Some good materials to read:

  1. :h motion.txt part 4, 5
  2. AdvancedTopicTutorial from atom-vim-mode-plus
Community
  • 1
  • 1
Allen
  • 4,431
  • 2
  • 27
  • 39
  • In `ciw` the textobject is `iw` not `w` which stands for **i**nner **w**ord – Meninx - メネンックス Jan 20 '17 at 19:48
  • 2
    your answer make me clearer about how these two commands work, and they seems easy to use, but I never had a thought to find the theory of these commands which seems easy to use before, with your kind help, I think I will learn more about whatever things in the future, not just work! Thank you for all! You inspired me. – Alec.Zhou Jan 21 '17 at 02:35
4
someword
    ^        (cursor)

cw

some_
    ^        (cursor)

Changes from cursor to end of word. Leaves the start of the word.


Compared to

someword
    ^        (cursor)

ciw

_
^            (cursor)

Changes the entire word from start to end.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
3

Mind you, cw has a bit 'unstandard' behavior by default in that it is behaves exactly like ce: It only works up until the end of the current word (like e, ce, de, ye) instead of including the whitespace up before the beginning of the next word (like w, dw, yw).

Therefore I recommend adding these mappings to your vimrc:

" Have `cw` adhere to its actual movement `w`, instead of duplicating `ce`.
nnoremap cw dwi
nnoremap cW dWi

and getting used to usually using ce. Typing ce is actually more efficient and less awkward than cw if you form the habit of pressing the e with your ring finger instead of middle finger when it comes right after c.


If you prefer to leave cw with its default assignment duplicating ce, then you can access its 'proper' functionality with an additional keystroke: dwi (or vwc).

(Mind you, I find dwi even more efficient to type than cw because of the positions of the keys, in spite of the additional key.)


Explanation of the second mapping: W (Shift+w) works on what vim calls "WORDS", which counts every non-blank character, e.g. punctuation, as part of a WORD; while w works on "words", which only consist of uninterrupted sequences of letters, digits and underscores (by default).

Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34