The Very Beginning

"I've been using Vim for about 2 years now, mostly because I can't figure out how to exit it."

@iamdevloperarrow-up-right, a famous Twitter post about Vim.

Introduction

Welcome! Whether you are just getting started with Vim or have been using it for years, this book is for you. You may have seen some of the tips in this book in other places or websites — and that is perfectly fine. The more we cover, the more likely it is that you have encountered some of them before. Our goal is to share these solutions in a way that is, in our view, more direct and practical. It is up to you, reader, to evaluate how useful they are. If you find this book helpful in any way, we would be honored if you shared your reading experience with others.

The main idea is to present useful commands and ideas, much like a cookbook — but with one key difference: we are going to break down each command, because there is no use in just copying commands blindly. In the learning process, understanding is the key. Due to Vim's composable nature, this philosophy makes even more sense. Longer code snippets will be provided as complete, ready-to-use examples.

What you'll learn in this chapter:

  • Understand what Vim is and how its conventions are used throughout this book

  • Navigate the built-in help system to find answers quickly

  • Use basic motions such as f{char}, line-position jumps, and the dot command

  • Perform fundamental file operations including the Ex put command and linewise vs characterwise copy/paste

Issues and Conventions

If you notice any issue, feel free to send an e-mail to the vim-daily teamenvelope. See also vim-bootstraparrow-up-right (a project maintained by editor-bootstraparrow-up-right).

In this book you will see key combinations like Ctrl + r, colon commands in the middle of the text like :echo "Hello World", and examples of code like this:

fun! UndoUndoSearch()
    call histdel("search", -1)
    let @/ = histget("search", -1)
endfun

Note: You will also see many boxes like this with additional information!

Warning: Some warnings will also appear, but rarely.

Throughout this book we will explore daily Vim solutions, like the ones in the section Squeezing Blank Lines or in the chapter Normal Commands That Stand Out.

Requirements

Knowledge of the basic modes of Vim is necessary to understand the contents of this book, but we will show you the essentials (see Modes, Navigation & Basic Editing for a deeper exploration):

Mode
What it does
How to get there

Normal

Move around and change text

Esc

Insert

Type text

a, A, i, I, o, O

Command

Run some commands

Esc:

Visual

Select text

Esc v V or Ctrl + v

Shortcut
What it does

a

Start Insert Mode after current character

A

Start Insert Mode at the end of current line

i

Start Insert Mode before current character

I

Start Insert Mode at start of the line

o

Start Insert Mode one line below

O

Start Insert Mode one line above

cc

Delete current line and start Insert Mode

S

Delete current line and start Insert Mode

gi

Restart Insert Mode from the last insert point

The $MYVIMRC Environment Variable

The environment variable $MYVIMRC points to the main config file of Vim/Neovim. Its value changes depending on your system and whether you are using Vim or Neovim. For example, if you are using Vim on Windows it will be $HOME/vimfiles/vimrc, whereas if you are using Neovim on Linux it will be $HOME/.config/nvim/init.vim. That is why many people prefer to use $MYVIMRC instead of ~/.vimrc or init.vim directly — it is a more portable, generic reference.

How to Get Vim Help

Command
What it does

:help gR

Help for gR in normal mode

:help v_r

Help for r in visual mode

:help i_CTRL-W

Help for CTRL-W in insert mode

:help :quit

Help for :quit in command mode

:help c_CTRL-R

Help for CTRL-R in command-line editing

:help togg<TAB>

Help for all the topics that have the string 'togg'

Tip: You can abbreviate the word help as :h — in the Vim help you will see h[elp], where the part between brackets is optional.

Tip: After opening the Vim help you will see some "links" highlighted in different colors. When your cursor is over any of these links, you can type Ctrl + ] to jump to that topic. To get back, type Ctrl + o or Ctrl + t.

The Ex Put Command

When you type the command :0put = 'Hi vim!', the put command places the text of the string 'Hi vim!' after line zero (the default is after the current line). We can specify other lines and other sources for the put command, as you will see later. What would be the advantage of such a thing? We can combine put with Vim's internal scripting language to programmatically insert content into the text — for example, inserting a large range of IP addresses into a file:

Note: We use | to separate commands.

Step
Description

for i in range(1,256)

Start a for loop, creating a variable i that goes from 1 to 256.

silent! put = '192.168.0.' . i

Silently insert the string '192.168.0.' concatenated with the current value of i below the cursor.

endfor

End the for loop.

In the chapter Types of Registers in Vim, section The Clipboard Register "+, we will see how to paste the content of the clipboard multiple times.

If you want to add a blank line at the end of your document, you can type :$put _. The dollar sign $ represents the last line, and the _ is the black hole register, which we are going to see later.

F{chars}

The f{char} motions are one of those Normal Commands that make many people who try Vim for a while keep coming back to it afterwards. Along with the Text Objects we are going to explore, they are one of Vim's fundamental building blocks. Let's see how they work:

Tip: The uppercase versions of these motions (F{char}, T{char}) jump in the opposite direction.

Shortcut
Example
What it does

f{char}

f,

Jump to comma

df{char}

df.

Delete until dot (inclusive)

t{char}

t)

Jump until before close parenthesis

vt{char}

vt"

Select until before quotes

In the section The Missing Motion of Vim you can read about the plugin vim-sneak, which allows us to jump to other lines just as we do with f{char}.

Moving on the Line

Vim has four native keybindings to jump to specific positions in the line, and you can use them as motions with operators like d, y, and c.

Moving on the line

Tip: If you are in the middle of a line and want to delete until the first non-blank character, just type: d _

For more movement tips, see the chapter Normal Commands That Stand Out.

Copy And Paste Linewise Or Characterwise

If you don't know the difference between linewise and characterwise operations, you may have been annoyed at least once: you copy a line with yy and try to paste it in the middle of another line — what happened? When you use the yy command, you are copying the entire line including the newline character (the "line break"). That is a linewise copy.

Sometimes we want to copy a line without its newline character. To do so, try this in normal mode:

Explaining a characterwise copy of the line:

In the chapter Mappings for Line Text-Objects we have mappings that allow us to copy the line without the newline character — "inner line" and "a line".

The Dot Command

In normal mode, the dot command repeats your last action. If you have...

Target file:

...and want to add --> between the last two words, you can do:

Steps:

Literal Inserting

The site vimgolf.comarrow-up-right has Vim challenges. Among them you can see this onearrow-up-right[^3], where you have the original text (before) and the final result (after). Your objective is to solve the problem with as few keystrokes as possible.

VimGolf

If you try using a text-object — something like ciw, opening the parenthesis, then pasting the default register with Ctrl + r ", and closing the parenthesis — it works for the first word. But when you jump to the second word and repeat the action with the dot command, it will repeat the last insertion literally, pasting the first word instead. To fix this, you need to paste your default register literally with Ctrl + r Ctrl + o ". Now, if you try to repeat your actions with the dot command, it works correctly.

Summary

This chapter introduced the foundations you will build on throughout the rest of the book: Vim's conventions, its built-in help system, essential motions like f{char} and line-position jumps, the dot command for repeating actions, and fundamental operations such as the Ex put command and linewise vs characterwise copy/paste.

Key takeaways:

  • Vim's help system (:help) is comprehensive and should be your first stop when learning a new command

  • The f{char} and t{char} motions let you jump to any character on the current line, and they compose with operators like d and c

  • The dot command (.) repeats your last change, making it one of Vim's most powerful editing tools

  • Understanding the difference between linewise and characterwise operations prevents unexpected paste behavior

  • The Ex put command can insert programmatically generated content directly into your buffer

Exercises

  1. Open a file and navigate the help system — Open any file in Vim, then use :help to look up the f motion. Follow at least two hyperlinks inside the help buffer using Ctrl+], and return with Ctrl+o.

  2. Practice f{char} motions — Given the line const result = calculateTotal(prices, taxRate);, place your cursor at the beginning and use f motions to jump to the opening parenthesis, then delete everything inside the parentheses.

  3. Use the dot command — Create a file with five lines that each say TODO: fix this. Use the dot command to change TODO to DONE on every line with as few keystrokes as possible.

  4. Generate content with :put — Use the Ex put command to insert the numbers 1 through 10 into an empty buffer, each on its own line.

  5. Characterwise copy — Copy a line without its newline character using 0yg_, then paste it in the middle of another line to see the difference from yy + p.

Last updated

Was this helpful?