Skip to main content

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. (No period! Just p)

Paragraph 5 is below Paragraph 4 now!

Move your cursor to the P in Paragraph 3, and type dj.

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

Type p.

Everything is fixed!

Type u 4 times to go back to the messed up paragraph order.

Fix the paragraph order again.

Fix the paragraph order a few more times. Better yet, fix it many times.

Make a game out of it. See how fast you can get at fixing paragraph order.

You should be able to make these changes in just a few seconds.

If you write for a living, the payoff for that kind of speed is huge!

Of course, writing is not a race.

The goal is not to write poorly at breakneck speed.

But when vim commands like these are part of your muscle memory, you no longer have to think about them. You think, "This paragraph here, that paragraph there," and your fingers make the changes as if by magic. Your focus stays on your work, not text manipulation.

That's why it's important to learn vim! No other software is better at text manipulation!

Explanation


Yesterday we learned that d followed by a movement key deletes text.

Today we typed dj to delete the line we were on and the line below it.

Remember, when we type d, the computer still doesn't know what to delete.

We typed j, which told the computer what to delete with relation to the cursor.

In other words, we told the computer to delete from the cursor down one line.

p pastes text. (In vim p stands for put.)

We moved to where we wanted to paste the text and typed p.

It was as simple as that!

We undid changes with u, which is the undo command.

In the future we'll talk about registers.

Here's a short preview.

When we delete text, it goes into the default register. 

The default register is like a clipboard.

When we paste text, vim looks in the default register by, ahem, default.

The name default might tip you off that vim has other registers.

Vim does, but we only need the default register for now. (Maybe ever!)

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…