Sunday, March 17, 2019

The Basics of Using the Vim Text Editor

Many of my tutorials or projects talk about creating a file or editing a file, and I rarely go into details on how to do this, so I thought I would write a short tutorial on the basics of using my command-line text editor of choice: Vim.

As a disclaimer, this is a very basic tutorial.  Vim has many powerful features, and I fully intend to gloss over most of them.  In fact, if you are a Vim power user then your face will likely start turning a lovely shade of pink when you see that I tell people that they can navigate around with the arrow keys, but this tutorial is not for you.  It is for the novice that just wants to be able to edit a few config files from the command line or create a short script.

If you would like to know how to use Vim in a more advanced way, simply google "vim tutorial" and you will be presented with a myriad of instructional pages, most of which will go into much more detail than you will find here.  If you are just looking for the basics, then you are in the right place.

Installing Vim


Vim comes preinstalled on many Linux distributions, but not all, and specifically not on Raspbian.  So, if you try to use Vim, and your system informs you that it is not installed, don't worry, installation is quick and painless.

sudo apt-get update
sudo apt-get -y install vim

That's it, if you didn't have Vim before, you have it now.

Using Vim


In order to edit a file the command is simple:

vim <filename>

If the file is write-protected you will need to precede the vim command with sudo.  If the filename indicated does not exist, then Vim will open a blank file with the specified name.

Once the file is open, your terminal will be replaced by the Vim editor in Normal mode (sometimes referred to as Command mode).  Normal mode is mainly used for navigation, and if you opened a file that already contains text you will see that you can navigate around using the arrow keys.

I would be remiss if I didn't point out that you can also use the keys h(left), j(down), k(up), and l(right) to navigate as well, and there are advantages to using them, but if you have a hard time remembering that you can always revert to using the arrows.

There are many other powerful navigation options in Vim such as using w to go the beginning of the next word, or $ to go to the end of the line, but I promised to gloss over all of that, so let's move on.  If you would like to see a summary of the commands available here is a link to a Vim cheat sheet I found by searching for "vim cheat sheet".

The one thing you can't do in Normal mode is to actually edit text, which is the main reason for using a text editor, so let's move on.

Insert Mode


Once you have positioned your cursor where you would like it to be, if you would like to begin entering or deleting text you need to enter Insert Mode.  This can be accomplished by simply pressing i.  When you press i, you will notice that -- INSERT -- appears at the bottom of the terminal.  You will also notice that you can create text by typing, and delete text by using backspace.

When you are finished using Insert mode, you can revert back to Normal mode by pressing [Esc].

The general concept of Vim is that once you finish editing in a particular location you would press [Esc], return to Normal mode, and then use some of Normal mode's more powerful shortcut keys to navigate to the next place you need to edit before returning to Insert mode.  However, in current versions of Vim you can still navigate with the arrow keys while you are in Insert mode.  Purists will tell you this is a bad habit, but don't worry, I won't tell anyone.

Saving and Quitting


If you are doing more than a few quick edits, follow the age-old wisdom and save often.  Anytime you are in Normal mode you can type :w[Enter] to save your progress.  When you are finished working with the file, make sure you have pressed [Esc] to return to Normal mode, and type :wq[Enter] in order to save and exit out of Vim.  If you want to quit without saving your changes you can type :q![Enter] (if you forget the exclamation mark don't worry, Vim will remind you).

Once you have quit you will be returned to your terminal shell in the same condition as when you started the Vim editor.

And that's the quick and dirty rundown of Vim.  As I mentioned before, there is a lot more to learn, but this is enough to allow you to edit config files or write short scripts.  Just remember: i to insert text, [Esc] to get out of Insert mode, arrow keys to navigate around, and :wq to save and quit.

If you enjoy using Vim I encourage you to continue learning and becoming more proficient with the more powerful features available.  Who knows, maybe I'll write a more advanced tutorial myself.  If I haven't placed a link to it here, that hasn't happened yet.

Until next time, Have Fun!

No comments:

Post a Comment