Getting started with Vim

Home / RSS / Contact me

01.05.2019

Vim Picture by Laura Chouette.

Vim can be quite confusing for beginners (just look at the upvotes).
But it’s actually not that hard and if you’re getting used to it, you might just x10 your efficiency.

If you’re typing for a few hours a day, it is definitely a good idea to spend some time becoming more efficient at what you’re doing. Typing somewhat near your speed of thought is also way more fun than always having to wait for your fingers to type what you thought about 10 seconds ago…

Getting used to the vim key bindings (or any uniform key binding scheme) doesn’t only enable you to edit text much faster and efficient, but is also essential for the use of any tiling window manager or keyboard controlled program in general.

I’ll only explain the basic concepts here, there’s already someone who gets you started with the actual use of the editor. I especially won’t list all available commands here. However, in order to get better at editing, it’s essential to have enough tools at hand, so go ahead and launch vimtutor!

The most important thing at the beginning is gaining a good overview of what the editor offers.
That’s what I’ll try to do in the following text.

Hint: Try :help <keyword> for finding offline help in the editor itself.
Hint: The commands shown here are to be executed in Normal mode. Press Esc before entering a command if you’re not sure which mode you’re currently in.

Three modes to rule them all

There are 3 main modes in vim:

Vim modes overview

  1. Normal mode

    This is the mode in which you should spend most of your time in.
    It’s used for moving around, bulk editing text; this is basically where most of the magic happens.

  2. Insert mode

    This is the mode which new users would probably call normal mode.
    The only things you have to remember while using insert mode are:

  3. Command mode

    This mode is used for inserting special commands, most times not directly related to editing text.
    For example, you’d use this mode to toggle settings, use find&replace or execute commands in the shell without having to leave vim.
    It’s also used for quitting vim (:q, :wq, :q!).

Where the magic happens…

Editing text could be done by moving to the position you want to edit, pressing i for entering insert mode and Esc for going back to normal mode after editing.
But if we wanted to be slow and boring, we’d just use nano, right?

Instead of just using h, j, k, l and counting, you can (and should) use movement keys like word and back, the find functionality (searching in a line) or searching globally (/) if you have a bigger distance to move.

The actual inserting happens, of course, in insert mode. However, you’re specifying where the text will be inserted and what text will be replaced in normal mode.
I won’t list all the possiblities here, there are already many resources doing just that (e.g. :help movement refers to this file).

A typical normal-mode vim command exists of a command, sometimes a quantity and a direction/selection. Examples are:

This allows you to become way more effective, compared to just using single keystrokes in insert mode.

These commands shouldn’t be remembered; Ideally you shouldn’t have to think about how you’re doing something, but only about what you’re doing.
Vim commands are built in such a way, that they can be used as a kind of language: daw (delete around word), cit (change in tags), dw (delete word), …
With some experience, you won’t have to think about which command you’re using to complete a task, it will feel natural to just do it.

Macros for bulk editing

There will come a time where you’re editing a bunch of file names in vim and visual mode just isn’t enough, not even block mode (^V).
This is where you should start using macros.

Record a macro by pressing q and then any key to save the macro as. Do any kind of editing and press q again to stop recording and save the macro. After that, press @ and the key you chose for saving your macro under to execute the macro. Of course you can also use the macro in more complex commands too.

Let’s say you have a bunch of lines with a variable amount of words in each line. All the words only consist of lowercase letters. You want to change all the words to start with an uppercase letter. No amount of visual mode is going to help you here, but try the following:

After that you can use your macro as 50@1 to change the first 50 words.

Configuration or how to spend the next 50 years of your life

Vim configuration in managed in a file called vimrc. It’s typically located at ~/.vimrc.
There’s a lot to explore here. Look up what other people put into their .vimrcs, you’ll probably find something you’ll like.

A general tip for improvement

Whenever you’re editing some text and you wish there was a feature which would help you save some time and work:
Just grab your favorite search engine and look up if it exists. Chances are it actually does!

That’s it

… for the overview. There are a lot of things I didn’t even mention in here. If you find it hard to stick to the good practices, try disabling the arrow keys or use a plugin like hardmode. Now go ahead and type vimtutor into your favorite shell if you haven’t done so already.

Update 25.07.20: Also check out this nice little talk by Sandy Maguire.

:exit
:quit
q
:q
:!pkill vim
:!halt


Want to leave a comment?

If you want to give me some feedback or share your opinion, please contact me via email.


© Niklas B├╝hler, 2020 RSS / Contact me