Getting to Know Vim, Pt.1 The basics


I had mentioned in a previous blog post that I have recently taken to using a text editor called Vim for pretty much all of my text editing needs, from coding to writing this blog post. Quite a lot of people reading this post have probably never heard of Vim, so this post is going to explain what vim is and some of the basic functionality in its use.

Did I Just Open Vim? Help!!

For those people who have heard of Vim or even Vi, you may have gotten into the situation of opening a text file through the command line and the default $EDIT program for the system had been Vi. The program opens and you are shown the file to be edited, you start typing, nothing happens - suddenly everything is deleted and you are left with a blinking cursor taunting you at the top left of the window, daring you to start mashing the keyboard. You take on the challenge laid down, frantically mashing the keyboard hoping to stumble upon a key combo to knock-out this unforgiving program. Still nothing happens, or nothing that you want to happen does, in total disgust you decide to cut your losses and either log out and log back into the machine or do a reboot.

While the above may be a bit of an over dramatisation, Vim is well known to have a steep learning curve. While this is slightly true, once you get past the initial up-hill struggle, you realise it is all worth it, as there are so many benefits - it will increase your productivity, decrease your mouse usage (hurray for keyboards!), it's blazing fast, uses little resources and will probably be 'nicer' than any other text editor you have used. Oh and it's Free Open Source Software (FOSS), as the cherry on the proverbial cake.

The very basics

Vi comes installed in most Unix like distributions, Vim(proved) however, may not be. To install it use your relevant package manager. If you want to use a graphical interface instead of the terminal, there is a graphical version called gVim. If you're on MacOS you'll have to install it via the homebrew package manager. From there just type vim in a console window.

Commands in Vim must be prefixed with the colon : character, for example in the above situation where you just want to quite and get back to safety, press esc to make sure you're in Normal mode (more on this in a minute) and then :q(uit) to quit Vim. If you have made alterations to the file (known as a buffer) and don't want to save them :q! will quit without saving. Now we have the emergency commands down we can look at Vim's philosophy of editing files.

Modes

In Vim there are 3 widely used States or "Modes" that one can be in whilst editing a file these are:

  • Normal (The mode vim starts, and the one where you can navigate the document)
  • Insert (The mode which allows editing the file through typing)
  • Visual (A mode where one can visually select blocks of text)

In order to get from Normal mode to Insert mode, simply press i, A status message saying -- INSERT -- should appear in the bottom left corner. You can now insert text, type and add new characters to the file, there are other ways to get in to insert mode which will be discussed later. To get back to Normal mode from Insert mode simply press the escape key. The status message should disappear. Finally to get from Normal mode to Visual mode, simply type v, the status message should change to -- VISUAL -- and using the Motion keys (explained in a minute) you should be able to highlight blocks of text.

Moving the Cursor

Whilst it is very possible to navigate a file using the arrow keys, there is a better way, especially if you are familiar with the "home row" whilst typing. Vi and Vim utilise keys on the home row to move the cursor whilst in normal mode:

  • h - moves left
  • j - moves down
  • k - moves up
  • l - moves right

This may seem like an odd thing to have as a default setting, but it means that you are not having to move your hands back and forth between either the mouse/ trackpad or arrow keys and the alpha-numeric keys on the keyboard. This can help with reducing hand pain when holding a mouse, or developing a "claw" like Chandler from Friends.

chandler_claw

Moving on

For a more in-depth and interactive introduction to some of Vim's basics, type 'vimtutor' in to your console. This will open a Vim buffer with a text file containing instructions to follow which will teach you about the language of Vim and one of the real crux's of Vim. The notion of Operators, Motions and Counts, which I will cover in my next post on Vim. For now, try to get used to using only the h j k l keys to move around a file and go through the vimtutor a couple times to try and get your head around how vim works. As always, any comments or questions don't hesitate to post a comment down below.


Subscribe

If you want to get updated with the latest blog entries directly to your inbox, put your e-mail address in the form below!