Vim Basics: Part 1

Vim Basics: Part 1

Getting to know vim

If you are looking for a way to get more efficient at editing configs and text then vim is what you are wanting. Even at its most basic interface with no added plugins, vim can help you manipulate a file with its efficient modes and commands.

vim is not the easiest to learn, especially for beginners where often newbies trying it out for the first time find they are not even able to exit the program. But, after some little quirks have been understood, you may soon ask yourself how you lived without it.

Running vim

When running vim, like with most other text editors, you will be presented with an empty file and cursor or line. The name of the file we are editing can be seen at the bottom of the terminal with a few numbers and additional information towards the bottom right which show your current lines and columns. Most people will often be unsure what to do here as when you begin typing like you would with other text editors, you may find that nothing gets entered or that you have entered into a different mode like –INSERT–, –VISUAL–, or –MACRO–.

Let’s create a new test directory and start working on a new file called greetings.txt.

$ cd ~
$ mkdir vim_testing_grounds
$ cd vim_testing_grounds
$ vim greetings.txt

Tip: You can hit the tab button in the middle of the directory name where bash and zsh will fill in the rest. Type in cd vim_ then hit [tab] and the prompt will be filled in with cd vim_testing_grounds

Your First Commands

When you first open vim, you’re in –NORMAL– mode. This might seem confusing at first, but it’s actually where the magic happens. Here are three basic commands to start writing:

  • Type i (insert) - begins typing exactly where the cursor is
  • Type a (append) - jumps one space ahead then allows you to type
  • Type o (open line) - creates a new line below and lets you start typing

After typing any of these keys, you’ll enter –INSERT– mode, which you’ll see at the bottom of your terminal. This is where you can type normally like any other text editor.

To return back to –NORMAL– mode, simply press the Esc key.

Let’s try it:

  1. With your greetings.txt file open, press i
  2. Type “Hello from vim!”
  3. Press Esc to return to normal mode

Why Vim Uses Modes

Vim is unique from other editors as it can allow a user to manipulate a file using keyboard keys and combinations all without touching the mouse. Other editors can do some great things but I often find vim to be faster and after learning the various commands, has allowed me to easily edit and create text files with very little to almost no hassle at all.

One of the things I immediately do when I perform a new GNU/Linux install is install vim and set the default editor to vim for root user in Debian/Ubuntu based distros. This can be done by running the following command:

$ sudo update-alternatives --config editor
There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed              -100      manual mode
  2            /usr/bin/vim.basic   30        manual mode
  3            /usr/bin/vim.tiny    15        manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

I will enter in the number 2 in my case. If you have additional editors installed your numbers and options will be different. The choice you want to choose is /usr/bin/vim.basic.

Normal Mode

vim will always start in –NORMAL– mode which is used to traverse around the file you are editing. This mode is where you’ll spend most of your time once you get comfortable with vim.

As mentioned before, i is for insert which is mainly used since where you have the cursor is usually where you will want to start editing the file. But to move the cursor around efficiently, you have two options: the arrow keys, or better yet, use the h, j, k, l keys.

How to Move Around in Your Document

The hjkl keys replace the arrow keys and allow you to keep your hands in the standard typing position without having to reach for the arrow keys or mouse. This might feel strange at first, but after a day or two of practice, it becomes second nature. Here’s how they map:

  • h - move left ←
  • j - move down ↓
  • k - move up ↑
  • l - move right →

Think of it this way: j looks like a down arrow, and k is right above it so it goes up. Once you get the feel for where your fingers need to be, you’ll find yourself flying around text files.

A Quick Practice Exercise

Let’s put what we’ve learned together:

  1. Open your greetings.txt file if it’s not already open: vim greetings.txt
  2. Press i to enter insert mode
  3. Type a few lines:
    Hello from vim!
    This is line 2.
    And this is line 3.
    
  4. Press Esc to return to normal mode
  5. Use h, j, k, l to move your cursor around
  6. Press o to open a new line and add more text
  7. Press Esc again

Don’t worry about saving yet - we’ll cover that in part 2.

What’s Next?

In this first part, we’ve covered the absolute basics: opening vim, understanding the difference between normal and insert mode, and moving around your document. These are the foundations that everything else builds on.

Stay tuned for part 2 where I will show you how to properly edit text using various commands, save and exit files using –COMMAND– mode, and introduce some time-saving shortcuts that will make you wonder how you ever edited files without vim.

For now, I recommend spending some time just practicing moving around with hjkl and entering/exiting insert mode with i and Esc. Muscle memory is key with vim, and the more you practice these basic movements, the more natural it will feel.

Until next time, happy vimming!