Skip to main content


Showing posts from December, 2017

Little changes in Vim

One letter at a time Vim has commands to delete or change a single letter.
Delete a single letter with x. (Just x without the period!)
Change a single letter with r. (Just r without the period!)
Let's try the single letter commands out!
Small changes Open vim.
Type i to go into insert mode.
Type Didd you Fill out teh from?
Hit the Esc key or type jj to go to normal mode.
There are four errors in the sentence. Didd should be did, Fill should be fill, teh should be the, and from should be form. Let's fix them the vim way.
Type ?r<Enter>xp?e<Enter>xp3brf?d<Enter>x
Did you see how it worked?
Start from scratch.
Watch what happens as you type in the commands.
Figure out how the commands are grouped.
Figure out why they work.
Did it seem like a lot of work?
Time yourself. It's faster than using a mouse!
Write out what you would do to make these changes with a mouse. Spell out every click, picking up the mouse, hitting backspace, and so on. It's a lo…

Repeating commands in vim

One key shortcuts This post is about one key shortcuts in vim.

Vim has macros to record complex sequences of commands.

We'll cover macros in a later post.

The most useful single key shortcut to repeat a command is the period.

n is a useful single key shortcut to repeat searches.

Let's look at how they work.

Repeating last change Open vim.
Type 10i to go  into insert mode.
Type I love Lucy! and escape to normal mode with jj or the Esc key.
You should have ten lines that say I love Lucy!
Go to the top of the document with 1G.
Type dd to erase one line.
Type . (<- a period!) to erase another line.
The period repeats the last change.
You can quickly erase all the lines holding down the period key.
That was an artificial example, but here's a more practical one.
Type i to go into insert mode.
Type: This sentence would be all right, except that it goes on and on and on and on and on and on and on and on. That's kind of a problem with my sentences, that they are circular …

How to search in vim

Search commands A backslash / in vim searches forward. 
A question mark ? searches backward.
They can be combined with other commands to make precise changes to text.
Let's see how they work. Going exactly where you want to go Download this excerpt from War and Peace. It may already be on your computer.
Open it by typing vim practice or vim
Join the multiple line paragraphs into single line paragraphs using J.
Your first paragraph should look something like this:
“What would you have me do?” he said at last. “You know I did all a father could for their education, and they have both turned out fools.  Hippolyte is at least a quiet fool, but Anatole is an active one. That is the only difference between them.” He said this smiling in a way more natural and animated than usual, so that the wrinkles round his mouth very clearly revealed something unexpectedly coarse and unpleasant.
Let's say you want to delete the words in bold from the last se…

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 …

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…

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…

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…

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…

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…

Support blog with $5 contribution