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

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…

Movement and reordering paragraphs in vim

Movement
Vim has many movement commands.

The four most important are h, j, k, and l.

They are consecutive keys under the right hand on the home row.

They function as the arrow keys do, but are easier to reach.

h and l move right and left respectively.

j moves down a line, and k moves up a line.

Reordering paragraphs
Download this file to your computer and open it in vim.

The five paragraphs in the document are out of order!

Before we fix the paragraphs, let's move around in the document to get used to the movement keys. Move to the bottom and top of the document by holding down j and k respectively. Go right and left within a line with l and h. Using h, j, k, and l to move will feel a little funny at first. Stick with it!

Now we're ready to go to work.

Move your cursor to the first line.

Your cursor can be anywhere within Paragraph 5.

Type dj. (No period! Just dj)

Paragraph 5 and the line below it are gone!

Use j to move your cursor to the empty line below Paragraph 4.

Type p…

Multiple line paragraphs in vim

Paragraphs in vim
Yesterday we moved around paragraphs of a single line.

Paragraphs are not always a single line.

Open this excerpt from War and Peace.

It's filled with multiple-line paragraphs.

We can move multiple-line paragraphs as they are.

A better solution is to make them single lines.

Before we do that, let's make sure we'll be able to see them!

Vim does not wrap text by default.

Type : to enter command mode.

Type set wrap <Enter> to make sure that long lines wrap.

Vim might break in the middle of words too.

Type : to enter command mode.

Type set linebreak <Enter> to make sure vim breaks at spaces.

To join lines, use J. (Just J, not the period!)

With your cursor on the first line of the first paragraph, type J to join the first and second lines. Type another J to join with the next line. When you reach the blank line, leave it. Always leave a space between paragraphs in vim. Move to the next paragraph with j, and join those lines with J. Continue until e…