Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between b and B in Vim?

Tags:

vim

I googled and that's what it says:

To go back a word, b is used. Once again, B will include more characters in what Vim considers a word.

I didn't understand what B does different from b. Can you give me an example to make it clear for me to understand? Thanks.

[EDIT]Actually I wonder this because in the online vim game, I tried to go back to WORDS from ! with b, but it didn't work. However when I tried it with Vim installed in my computer, it worked with b. So is this only for to make player use B with the help of clue?

Here is the picture of game:

enter image description here

like image 810
Figen Güngör Avatar asked Jan 18 '13 00:01

Figen Güngör


People also ask

What does De do in Vim?

de means "cut from here to the end of the current word". Show activity on this post. The cursor deleted the letters from the current word, until end, but without space.

What is let G in Vimrc?

l: local to a function. g: global. :help internal-variables. Follow this answer to receive notifications.

What is the difference between Q and Q in Vim?

The key difference is the exclamation mark here. :q will warn you about unsaved changes and will not let you exit. :q! will not warn you.


2 Answers

Like most of the capitalized movement pairs, b moves by word, but B moves by WORD. The difference is that vim considers a "word" to be letters, numbers, and underscores (and you can configure this with the iskeyword setting), but a "WORD" is always anything that isn't whitespace.

So given this:

foo-bar-baz 

If your cursor is on the z and you press b, the cursor will move back to the start of baz, then to the hyphen, then back to the start of bar, and so on. Each of these is a different "word" to vim: foo, -, bar, -, baz.

But if you press B, the cursor will move all the way left to the f, because foo-bar-baz is all non-whitespace and thus a single WORD.

:help word inside vim explains this too.


Regarding the vim game: I think the game treats boulders as punctuation. Try typing it in vim like this:

not WORDS*! 

With the cursor on !, b will move you back to the *, because *! is all punctuation and thus one word. But that * is actually a boulder, so you can't move there, so nothing happens. B, on the other hand, will skip you back over everything that isn't a space.

like image 196
Eevee Avatar answered Sep 18 '22 09:09

Eevee


B treats punctuation as part of the word, using only whitespace as word delimiters; b does not treat punctuation as part of the word.

like image 22
Ted Hopp Avatar answered Sep 19 '22 09:09

Ted Hopp