Learn the basics of Vim

0
(0)

Quite often there are times when you need to edit some files on a remote server and you find that your normal editor isn’t installed on the platform. This is particularly true if you normally use one of the modern GUI editors like Atom or Sublime and although you could copy the files to your desktop machine for edit and copy back, it will normally be quicker and easier if you can edit them in situ. In those circumstances it is useful if you know at least the basics of Vim.

Vim is a text editor which is available on the vast majority of Linux and Unix servers which makes it ideal for quick editing tasks since you can be sure it is installed. It fact, Vim is a modern re-write of an editor called vi but in nearly all cases these days even if you run the vi program you will probably find you are actually running a copy of Vim.

The thing which makes Vim frustrating initially, is that it is a modal editor. That mean it has a mode for inserting, appending and editing text, and a mode for running commands. This is completely at odds with the way most modern editors run and is normally the reason anyone who starts, unprepared, a Vim session, can spend several frustrating minutes trying to edit a file and then even longer trying to kill Vim itself.

So, in this article I’m going to just cover the basics of Vim – getting a file loaded, making simple edits and saving and exiting the file. For the majority of people who don’t want to move to using Vim full time, this will be a useful set of skills which could save you a frustrating time when you want to just make a quick change to a config file.

The basics of Vim

As with any unix command, to run vim you just type:

vim

at the command prompt and the program will start and you will be faced with the welcome screen. If you know the file you want to edit (and the premise of this post is that you will) than you can pass it on the command line and vim will open it.

vim myfile.conf

If, which is less likely, you know the actual line number you want to edit, you can also pass that to Vim and the file will open with that line as the current line. You do that like this:

vim +124 myfile.conf

Irrespective of how you have opened the file, Vim will start in command mode which is the mode from which you can run editing commands.

Perhaps a little bit of explanation is due here:

The reason Vim is so successful and is used by many developers as their editor of choice, is because the command mode allows very sophisticated and complex edits to be carried out very quickly.

Commands are made up of actions and movements. For example the key ‘d’ is the delete action and when it has a movement added the movement determines how much text is deleted.

There are many different movement commands; ‘w’ means ‘word’ or more accurately from the cursor to the end of the current word, ‘0’ means ‘beginning of line’, ‘$’ means end of line etc. So, in command mode, the key sequence ‘dw’ is used to delete from the cursor to the end of the current word, ‘d0’ is used to delete from the cursor to the beginning of a line, ‘d$’ is used to delete from the cursor to the end of the current line. As well as the basic commands and movement, it is also possible to precede the command with a number  which adds a repeater to the command so 4dw will delete the next 4 words.

The thing which confuses people when they first encounter Vim is the seemingly unending number of commands which need to be learnt, but in fact there are only a handful of commands which are needed to do the vast majority of editing. The beauty of Vim is that once you have learned the basics of Vim, you can add action and movements together to create very powerful editing commands to tailored exactly the task you are trying to do.

The other major mode which Vim can use is Insert mode, where text is inserted. In this mode, Vim behaves very like any other editor, although with only very basic editing commands. You can enter insert mode from the command mode using the ‘i’ key, and you can go back to command mode from insert mode with the ESC key. When you run a command from command mode, Vim actually moves into insert mode, carries out the command and then moves back to command mode.

Anyway, with that explanation done it’s back the the basics of Vim.

As I said at the start of this article, the aim of it is to go through the basics of vim, not to teach all the intricacies of how to use it (which, to be honest I’m not qualified to do). So the important points are:

  • Vim is modal. In command mode you can move the cursor about the file using the h,j,k & l keys or the arrow keys on the keyboard. Page Up and Page Down move through the file a page at a time.
  • To move from command mode to insert mode use the i key.
  • When you are in insert mode the arrow keys still move the cursor around and the backspace key erases and the delete key deletes etc.
  • To save a file you need to be in command mode and to get to command mode from insert mode use the ESC key. Then to save the file hit ESC again and a : will appear at the bottom of the screen. If you then type w! and enter the file will be saved (the ! means overwrite the existing file)
  • To exit Vim, press ESC to get the command prompt (the : ) and use q enter. If you have changed the file and it hasn’t been saved you will be prompted to save it or abandon the changes.

If you feel you want to learn more about how to use Vim, there is a tutorial built into the editor which you can access by running the command

vimtutor

from the unix command line. This will take you through more of the basics of vim and it’s worth doing for those occasions when you are working on a server which doesn’t have a GUI editor. You may even find you become hooked!


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Please consider sharing on social media!

Leave a Reply

Your email address will not be published. Required fields are marked *

The River Avon at Stratford on Avon Previous post The River Avon at Stratford-on-Avon
Cockapoo puppy in a wheatfield Next post Freddy in a corn field