Novell Home

VIM: Console Text Editor

Novell Cool Solutions: Feature
By Scott M. Morris

Digg This - Slashdot This

Posted: 29 Aug 2005
 

Applies to:

  • SUSE Linux Professional
  • Novell Linux Desktop
  • SUSE Linux Enterprise Server

Part of learning how to use Linux will, for most people, include learning how to use a text editor. I personally prefer Kate. However, Kate works best for local files. What do you do when you need to edit a file on a remote server? You'll probably want a console-based text editor. So, which one do we use? Many people will offer you widely differing opinions. For me, I say to go with what you know best. In my case, that is a program called vim. It is this console-based text editor that I would like to introduce and discuss.

First, let's make sure you have it installed. Open up YAST and search for the package called vim. When it comes up in the right pane, tick the checkbox next to it. Then, click ACCEPT:

YAST then installs the program. Now you have it installed; let's get started using it.

As vim is a console-based application, open a console window. To invoke vim, type vim at the prompt and press ENTER. You'll see something that resembles this:

~
~
~
~
~
~                       VIM - Vi IMproved
~
~                        version 6.3.58
~                   by Bram Moolenaar et al.
~          Vim is open source and freely distributable
~
~                   Sponsor Vim development!
~        type  :help sponsor<Enter>    for information
~
~        type  :q<Enter>               to exit
~        type  :help<Enter>  or  <F1>  for on-line help
~        type  :help version6<Enter>   for version info
~
~
~
~
~
                                               0,0-1         All

The very first thing to learn about vim is that there are two modes. They are referred to as normal mode and edit mode. In edit mode, as you are typing, what you type will appear on the screen. This is what you are used to with other text editors. You open the text file and start typing. What you type is what you see. This mode is called edit mode in vim.

The other mode, normal mode, is quite different. Let's say that you are using Kate or gedit or some other window-based text editor. If you press CTRL+S, usually the editor will save the file. If you press ALT+F4, usually the editor will exit. These keyboard combinations are referred to as keyboard shortcuts. In vim, normal mode resembles these keyboard shortcuts. Each key that you press in normal mode generally performs a function. There is a key for copying text to the clipboard. There is a key for pasting. There is a key for deleting a line. There is a key for jumping to the first line of the file, and a key for jumping to the last line of the file. Each key that you press in normal mode is like a command to vim.

So, we have our two modes, edit mode and normal mode. Edit mode is where what you type appears on the screen. Normal mode is where each key that you press sends a command to vim.

Another thing to know about normal mode is that it has a type of command prompt where you are able to type in commands that you want vim to execute. You exit the program this way. You save your file this way. You can save the file and then immediately exit the program this way by combining these two commands together. The basic thought here is that you have two ways to enter commands in normal mode. The first is with individual keys. The second is by typing them into the command prompt.

So, how do we switch between edit mode and normal mode? When you very first start vim, you are in normal mode. So, you need to tell vim that you wish to "insert" text. To do this, you just press the "i" key for "insert". Keep in mind that these commands are case-sensitive. Pressing "i" and "I" are not the same thing. So, press "i" to insert text. vim tells you that you have entered INSERT mode down in the status bar. You will see -- INSERT -- at the bottom (after entering "insert" mode):

~
~
~
~
-- INSERT --                                   0,1           All

This means that you can now start typing, and what you type will appear in the vim window. So type some deep, dark secret into vim, there. When you're done, let's save it out.

To save it, we have to be in normal mode. How do we get into normal mode? Quite simply, just hit the ESCAPE key. You will see the -- INSERT -- disappear from the bottom of your window. You are now in normal mode. Quite easy.

But, now, we need to save it. To save it, type a ":" (colon). You will see that appear at the bottom. This is the command prompt that I was discussing earlier. Now, we just need to tell vim what to do. We are going to save it. So we need to tell vim to save the file, and what filename to save it as. So, immediately after the ":" type a "w". This means "write" or "save". Now, put a space, and then put the filename you wish to save this text into, such as "mytextfile.txt" or something. You should now see something like this:

~
~
~
~
:w mytextfile.txt                                               

Press ENTER, and vim will report back with:

~
~
~
~
"mytextfile.txt" [New] 1L, 12C written         1,11          All

Excellent. We've written the file. We'll probably want to exit the program. To do this, type a ":" followed by a "q", so it looks like this:

~
~
~
~
:q                                                              

Press ENTER, and vim quits.

There are quite a huge number of commands that vim has. The basics, though, are that there are two modes. In edit mode, you are directly editing the text of the file. In normal mode, you are entering in commands, telling vim what to do and how to do it.

The best way to learn vim is by spending some time in vimtutor. At a command prompt, type in vimtutor and press ENTER. vimtutor pops up:

===============================================================================
=    W e l c o m e   t o   t h e   V I M   T u t o r    -    Version 1.5      =
===============================================================================

     Vim is a very powerful editor that has many commands, too many to
     explain in a tutor such as this.  This tutor is designed to describe
     enough of the commands that you will be able to easily use Vim as
     an all-purpose editor.

     The approximate time required to complete the tutor is 25-30 minutes,
     depending upon how much time is spent with experimentation.

     The commands in the lessons will modify the text.  Make a copy of this
     file to practise on (if you started "vimtutor" this is already a copy).

     It is important to remember that this tutor is set up to teach by
     use.  That means that you need to execute the commands to learn them
     properly.  If you only read the text, you will forget the commands!

     Now, make sure that your Shift-Lock key is NOT depressed and press
     the   j   key enough times to move the cursor so that Lesson 1.1
     completely fills the screen.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

One of the best teachers is experience. Take some time to play around with vimtutor to familiarize yourself with vim. Once you are used to it, you will get faster and faster while editing text files or source code. vim is especially nice for editing files via a secure shell connection to a remote machine.

For a little more on secure shell, take a look at this article that I wrote a while back.

If you'd like more help learning the many vim commands, check out this great reference card.


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell