Skip to main content

Deleting words in vim

How vim sees words

Some words have only letters.

Some words include punctuation.

To move forward by words in vim, use w or W.

w moves across combinations of letters.

W moves across combinations of letters and punctuation.

b moves backwards across combinations of letters.

B moves backwards across combinations of letters and punctuation.

Moving by words

Open vim. Type i to go in insert mode.

Type: I shouldn't help but love her.

Type jj or <Esc> to go to normal mode.

Move backwards with b or B.

Move forwards with w or W.

Go back and forth a bunch of times. Use all of w, W, b, and B.

Notice w and b take three presses to go through "shouldn't."

That's useful!

Didn't we mean to say "couldn't" instead of "shouldn't?"

Move your cursor to the s at the start of shouldn't.

Type dw. You erased the part of the word you wanted to change!

Type i go to into insert mode. Type couldn. Type jj or <Esc> for normal mode.

Are there ways to be more precise than this? Could we have changed "sh" to "c?"

Yes! We're getting to that. Next week some time. I promise.

Explanation

w, W, b, and B allow us to move through documents very quickly.

Imagine a page full of text. You can navigate faster with w, W, b, and B faster than with the arrow keys. Remember, ) and ( move by sentences, and j and k move by lines. We're way ahead of word processors already with just these few commands, and we haven't even got to the cool stuff. Vim is all about fast.

If you dig into the documentation of your word processor, you'll find you can move with keystrokes. But the capabilities are limited compared to vim. Also, the keystrokes are tough. This is the same issue with emacs. You can't work fast if everything you type is Ctrl- or Alt- something or other.

Vim wins on speed of text manipulation, every time.

Comments

Support blog with $5 contribution

Popular posts from this blog

Getting started with Vim

First vim file
Let's get something done in vim!

Open vim.
Do the following without changes.

Press i to go into insert mode. 
Type: "Hello, Dolly!" (with quotation marks)
Hit the Esc key to go into normal mode.
The cursor should be over the second quotation mark.
Type b to go back to the exclamation point.
Type cb to erase Dolly and go into insert mode.
Type World.
Hit the Esc key to go into normal mode.
Type : to go into command line mode.
Type w hello and hit the Enter key to save (write) the file as hello.
Type : to go into command line mode again. Type q and hit the Enter key to leave vim.
Congratulations!
You created a file hello in vim with one line of text, "Hello, World!"
So much for vim's famous learning curve!
Explanation
Vim is a modal text editor. We used three modes, normal, insert, and command line. 
Vim opened in normal mode. We couldn't insert text in normal mode. We could only give commands. Since we wanted to insert text, we gave the i…

Moving and deleting sentences in vim

What is a sentence?  Vim looks for punctuation to define a sentence.

Vim usually knows what a sentence is, but not always!

Here are two useful commands.

To move to the start of the next sentence, type ).

To move to the start of the current or previous sentence, type (.

Now let's see how these commands are used.

Open vim.

Hit i to go into insert mode.

Type or copy and paste this text:

Hello, Mr. Wilson. I was wondering if you could stop by this afternoon.

Hit <Esc> (or better yet, jj) to go back to normal mode.

Your cursor should be on the period of the second sentence.

Type ( to go back to the start of the current sentence.

Your cursor should be on the I at the start of the second sentence.

Type ( to go to the first letter of the previous sentence.

Oops! We ended up on the W of Wilson. Why?

Vim saw two sentences, "Hello, Mr." and "Wilson."

Two periods, two sentences.

Type ) to return to the start of the second sentence.

Type 2( to go to the start of the f…

Survival commands in Vim

Power
Vim is powerful.

When that power turns on you, it can be a disaster!

Let's learn how to recover from disaster.

Disaster!
Download War and Peace to your computer.

Open the file: vim 2600-0.txt (or vim <FILENAME.YOU.CHOSE>)

Imagine you're Tolstoy at your desk.

You just wrote War and Peace! It's a good day.

You type g followed by <Ctl>-g to see if your book meets the publisher's minimum 60,000 word requirement. Wow! 566,308 words! You're good.

But you can't remember if you wrote, "The End."

You type dG to go to the end of your document and check.

Whoops! You erased your whole book!

Maybe dG was the wrong command.

Salvation
Not to worry!

You type u (undo) and your whole document is restored.

Good thing you learned a few vim survival commands!

Erase War and Peace again. Feels powerful, doesn't it? dG was the command.

Type i to go into insert mode.

Type: Not only did I erase my masterpiece, but now I've typed over it!

Hit the Esc k…