Skip to main content

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 every paragraph in the document is a single line.

Now you can move the paragraphs with dj and p.

Moving multiple-line paragraphs

The commands { and } move to the beginning and end of paragraphs respectively.

Type u repeatedly to unjoin the paragraph lines we just joined.

Put the cursor on the first line of a multiple line paragraph.

Type d} to delete the paragraph.

Use j or k to move up or down in the document.

Use p to put the paragraph where you want.

Work with single line paragraphs in vim

Multiple line paragraphs result from typesetting.

The breaks are meaningless in terms of language.

They make the paragraphs tough to edit in vim.

Work with single line paragraphs in vim!

Format your document in a word processor after you write, not before.

Write -> Edit -> (Rewrite -> Reedit) [vim] -> Format [word processor]

If you have a work in progress you want to import to vim, use J to make single line paragraphs.

.vimrc file

You likely have a file called .vimrc on your computer.

If not, you can create it.

Where it is depends on your system.

On my system, Debian Linux, it's in my home directory.

Find .vimrc on your system.

Type vim .vimrc to open it.

If it's not there, find out where it should be.

Look at your system documentation.

If you can't figure out where it should be, create it in your home directory.

Type vim .vimrc to open an empty file called .vimrc.

.vimrc contains your personal settings for vim.

Hit i to enter insert mode.

Type three new lines:

set wrap
set linebreak
imap jj <Esc> 

Hit <Esc> to go to normal mode.

Hit : to go to command line mode, and type wq <Enter>.

:wq is the command for "write and quit."

The first two lines were the wrap and linebreak commands we used earlier today.

If they're in .vimrc, vim will wrap and break lines by default.

The third line sets jj as an alternative to <Esc>.

Many people prefer jj to <Esc> as jj is easier to reach.

If you type jj quickly, it works as <Esc>. Slowly, it is jj.

Unless your initials are jj, you probably don't use jj much!

If you do, change jj to any little used key sequence you like in .vimrc.

You can still use <Esc> even with jj mapped to it. 

People keep their .vimrc files for years and build up tons of personal customizations.

You can put macros in .vimrc, color preferences, and settings.


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…

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

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…