Skip to main content

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. (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!


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!)


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.
You created a file hello in vim with one line of text, "Hello, World!"
So much for vim's famous learning curve!
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…

Survival commands in Vim

Vim is powerful.

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

Let's learn how to recover from 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.

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…

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…