Skip to main content

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 first sentence.

With most commands in vim, you can prepend a number.

2( told vim to move back two "sentences."

Are there more elegant ways to make this motion?

Yes. And with time, we'll learn them!

But in some cases, 2( may be the best option.

Switching sentence order

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

Type d) to delete the second sentence.

This pattern should be familiar by now!

It is a command followed by movement.

All that is new here is the particular movement, ).

Type 2( to go to the start of the first sentence.

Type P to put the second sentence before the first sentence.

Up until now, we've used lowercase p to put text.

Uppercase P puts text too, but before the cursor instead of after.

The order of our sentences is switched!

The sentences might not have a space between them.

Then again, then might. It depends on what you typed.

If they don't, it's because the last sentence did not have a trailing space.

If it did, vim would have deleted it and kept it with the sentence.

If you need a space, type l to move to the H of the next sentence.

Type i to go into insert mode, hit the spacebar, and <Esc> to normal mode.

Did you mess up the whole line?

Type U to fix it.

Uppercase U fixes all of whatever line we just messed up.

Remember, lowercase u undoes individual edits one by one.

Normal mode

Normal mode is called normal mode for a reason.

It's where you should spend most of your time in vim.

We like to think we're writers. We're not. We're editors.

Writing is fluid, spontaneous, and easy.

Editing is tough, necessary, and takes up most of our time.

We like to say, "Writing is hard!"

No. Writing is easy. Editing is hard!

We get lazy with language.

We say writing when we mean editing.

Vim will not make our writing easier, per se.

That's a function of our typing speed and thoughts.

I learned to type on a manual typewriter. I was the only boy in typing class. There were thirty girls. Those were the days when every boy in high school took wood shop and every girl took home economics. Taking typing was the smartest thing I ever did! Because I learned to touch type, I can write fast in any program.

So why even bother to learn vim?

I need to edit my writing! Vim edits better than any other program!

Editing in vim takes place primarily in normal mode.

If you type a character in vim, and the character appears before you on the screen, you're in insert mode. Leave! You don't want to be there! Of course, if you're doing actual writing, finish your thought. But as soon as you're done, escape back to normal mode!

Real work in vim gets done in normal mode.

Today we learned how vim sees a sentence.

We learned how to move by sentences through a document.

We learned how to exchange the order of sentences.

But vim understands how to divide up language in many other ways.

We will learn faster and more elegant movements.

We're getting there! Patience!

It will pay off when you're editing your next novel.


Support blog with $5 contribution

Popular posts from this blog

Movement and reordering paragraphs in vim

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…