J. R. Swab's Blog

Introduction To Vim

Categories: Technology
Tags: open-source programming apps

I began my VIM journey years ago after following a tutorial online for some obscure Linux function that said to open a configuration file in VIM. I then found out I had no idea how to do anything in this program. I could not type, only move the cursor.

It seemed much too odd for me as a text editor at the time when I was using programs like Notepad++ or Atom for programming and Office Libre to compose a blog post or prose. But it was at that moment, well after I figured out how to exit, that VIM got wedged in my brain.

Vim seems to do that to the nerd who types a lot and knows how slow and inefficient the mouse can be. Not moving our hand back and forth to the mouse and keyboard saves time that compounds as the day goes on, but it also is good for our health. Without the need to move from our keyboard we give our hands and wrists the chance to find their comfort zone.

When it comes to computer ergonomics, my hands are second only to my eyes. Keeping these two hands that type this post is important to me because if the day comes where the pain is too great to type, I'm not sure what I'll do. Probably use a dictation device, but I've heard only how buggy they can be.

What is VIM?

Vim is an acronym for "Vi Improved" and is just that, an improved version of the Vi text editor. Vi's first set of code was put into memory by Bill Joy in 1976. At that time the programmed wall was called "Ex." It was not until version 2.0 that the name changed to "Vi." Vim is a fork or clone of Vi created by Bram Moolenaar in 1991.

While Vi could only run in a terminal, Vim can run as a standalone application in a windowed environment. Vim is free (as in freedom) and open source software licensed under the GPL. The editor is free of charge as well, with an encouragement that the users donate to the children of Uganda.

Vim has no menu bar or ability to use a mouse when run in the terminal as most users I know do. However, these standard application features are present in the Graphical User Interface version know as gVim. To many users, gVim takes away the need to learn and can only slow down our productivity.

The Vim editor is customizable, as with most Unix-based programs, allowing it to fit any writing or programming style. We can change the color scheme, turn off and on syntax highlighting, and install plugins for additional use. Vim even allows us to remap keys specifically for the editor without affecting the rest of our system. Outside of the base install, no two Vim configuration files (.vimrc) are the same.

Modal Editing

The most apparent difference between Vim and other editors such as Atom or Notepad++ is its modal set up. Vim is a modal editor because there are three main modes that the user may work in while using Vim. The first and default mode is "Normal" mode.

It is in this mode we can do no typing, only editing. We can move around the document with key combinations, delete words and lines, and replace characters. The reason for setting this as the default mode is to write code, a novel, or a blog post; but is often met with more time reading the document to make changes than the actual writing process.

Then we have "Insert" mode. Here is where we type or insert text into the document. This mode works the same way any other text editor does. There is a cursor that moves as the characters we choose to add, enter into the document. Everything is as we'd expect a text editor to be, just without a mouse in most cases.

The final of the three main modes is "Visual" mode. This mode I use much less often than the other two since my need to manipulate a large section of text is minuscule compared to editing a word here and there. That said, the visual mode can do many amazing things such as change a column of characters at the same time.

Basic Vim Commands

All of these commands are used outside of "insert" mode and will make editing the text in "normal" or "visual" mode faster than any other editor we can use. These are not hard to remember, and over time our muscle memory will take over from our thinking brain. The key with these is to keep our hands on the keyboard to limit wasted time and in most cases, ease any pain.

Moving around the document

Every key you need to move around the document is on the home row of a QWERTY keyboard (don't worry if you use Dvorak, it's not bad).

Move Up k

Move Down j

Mover Right l

Move Left h

Manipulating the document

This is were Vim begins to really shine!

When we are in normal mode and want to change a word that our cursor is in the middle of, all we need to do is type:

caw

While this seems obscure, it's not! It's an acronym for "Change Around Word." This action will delete the word our cursor is in the middle of and place us into "insert" mode to type the change. If our cursor is on the first character of the word than all we need to enter is

cw for "change word."

What if we are editing a program and want to change all the text inside of a set of parentheses? Instead of going into "insert" mode and backspacing until they are all deleted we can place our cursor in inside the parentheses while in "normal" mode and type:

ci(

This combination stands for "Change Inside (" and will delete all the text inside the parentheses, then place us into "insert" mode to type our desired text.

vim-cheat-sheet.gif

Vim Cheat Sheet from viemu.com

Tips for the aspiring Vim user

The first tip is one that helped me on my journey to using Vim, and it is to disable the arrow keys. Removing the arrow keys will force you to learn how to move with h j k and l. This well makes your editing more efficient than using the arrow keys every time you need to move the cursor.

Second, don't add any plugins while you learn Vim. It's very tempting to want all the fancy bells and whistles that we can add to Vim with a plethora of plugins. However, we are much better off to learn Vim in its vanilla state to get the best understanding of what it can do.

Finally, change the color scheme. The colors that come out of the box with Vim are atrocious and hurt my eyes the more I looked at them. There are many color themes on vim.org to look through. My favorite is one call Molokai, and it was the most popular theme for a long time.


If you are an email kind of nerd you can sign up for mine here. You can always replay to any of my emails to start a conversation or ask me a question. You can donate to this site from my Liberapay account if you so choose.

« Previous Post Next Post »