1

One of the most basic editing commands is c<motion> to change text, e.g. cw - change word, c$ - change to end of line, cc - change entire line, c3l - change 3 characters to the right.

My question is, why then does cb at the end of a word prepare for replacement, excluding the last character of the word (i.e. the character that the cursor was over when typing the command)?

vb at the end of a word will select the entire word, including the last character. Should cb not be expected to have the same behavior?

In short: The behavior of vbc is expected, but behavior of hvbc is equivalent to cb.

I'm using Vim 8.0.1350.

It seems this behavior follows more the idea that the cursor is "between" characters (insert mode) rather than "over" a character (normal mode).

2 Answers 2

9

The b motion is an exclusive motion. Visual mode is inclusive by default (See :h 'selection').

From :h exclusive:

A character motion is either inclusive or exclusive.  When inclusive, the
start and end position of the motion are included in the operation.  When
exclusive, the last character towards the end of the buffer is not included.
Linewise motions always include the start and end position.

You are using b when you probably want to use the iw text object instead, e.g. cb vs ciw. See :h iw and :h text-objects.

You can also force inclusivity on a motion by using v after an operator, e.g. cvb will do a change over motion b but inclusively. See :h o_v.

2
  • 1
    visual mode by definition is inclusive when set selection=inclusive (default) or set selection=old
    – Mass
    Jan 24, 2018 at 19:42
  • I appreciate the very informative answer, I was unaware of the existence of exclusive and inclusive categories for motions
    – Ivan G.
    Jan 24, 2018 at 20:16
1

The other answer doesn’t give the full command (note the equals sign):

:set selection=inclusive

even though the default is supposed to be inclusive. On my debian box I had to add it to .vimrc because it was exclusive.

2
  • 1
    Welcome to Vi and Vim! This answer could benefit from more information explaining how it solves the problem. Use the edit button to add some!
    – D. Ben Knoble
    Oct 8, 2019 at 22:49
  • The previous answer doesn't give the full command. I sat there typing <s>set selection inclusive</s> (no equals) before realizing the syntax
    – dansch
    Oct 10, 2019 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.