DavidR
DavidR

Reputation: 369

Make Vim treat forward search as "up to and including"

As a newcomer to Vim, I'm a big fan of the combination c/[some_character]. For example, I use c/; to change a line up to the final ; in JavaScript.

However, I find it more intuitive to think of such a change us "change up to a certain character, including that character. This is particularly relevant given that something like c$ changes all the way to the end of the line.

Is there a straightforward way to make this command behave as "up to and including" and to apply this to d and y as well?

I've thought about a custom keymap, but I can't figure out how to map a command that combines as c does.

Upvotes: 1

Views: 332

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172778

/ is a motion that takes you to the beginning of the match. In combination with a change command, it does not include the first character. However, you can specify search offsets. To include the first matching character, you can append /s+1 (start plus one character), or in case of a single character match, also /e (end of match). That, when combined with the change command, will include the character.

But I have a big surprise for you: This kind of search is so frequent, there's a special shortcut in vi and Vim: the f{char} command is the same as /{char}/e<Enter>! (And t{char} corresponds to /{char<Enter>.) Have a look at :help f; you'll also find uppercase variants that go in the opposite direction!

Learn how to look up commands and navigate the built-in :help; it is comprehensive and offers many tips. You won't learn Vim as fast as other editors, but if you commit to continuous learning, it'll prove a very powerful and efficient editor.

Upvotes: 2

Related Questions