resgh
resgh

Reputation: 984

In vim, why does c_ behave as if cc?

As the title.

Since _ is the movement to the first non-whitespace character in the line, and c + movement generally means to change (aka delete + go into insert mode) the buffer from the cursor to the movement, why does this not work? It seems to delete the entire line, instead of from the cursor to the beginning of line (aka cc or C). Is there an alternative for this?

Upvotes: 2

Views: 73

Answers (3)

asdf
asdf

Reputation: 121

Amadan's answer give's the reason why c_ doesn't work.


Answering the second part of your question

Is there an alternative for this?

Yes, there is. Use c^!

Check out :help left-right-motions and you'll find:

^                   To the first non-blank character of the line.
                    exclusive motion.

Upvotes: 1

Amadan
Amadan

Reputation: 198324

Because _ is defined as an up/down motion. From :help up-down-motions:

_  <underscore>     [count] - 1 lines downward, on the first non-blank
                    character |linewise|.

It just defines the position where the cursor will end up once the up/down motion is finished. Thus, c_ operates on lines, just like cj does.

Upvotes: 2

user9143630
user9143630

Reputation: 183

This is a known issue in vim. See https://github.com/vim/vim/issues/2189#issuecomment-334441965:

_ is a linewise movement (I guess because of VI compatibility). All commands (like d or c) that are combined with a linewise movement affect whole lines. You can make the movement (in combination with a command) characterwise by prefixing it with v. So cv_ and dv_ should do what you want.

Upvotes: 5

Related Questions