Vim tip - showing line numbers

I covered an ultra quick start on how to get into Vim recently, and now it's time for a related Vim tip.

Being a bit of a geeky text editor, Vim is often used for programming. And in programming, having line numbers easily visible can mean the difference between hunting for a bug for half an hour or finding and correcting it within a few seconds.

Now, it is possible to jump straight to a line of your choice in Vim, but sometimes it can certainly be beneficial to see the line numbers at the side for quick and easy reference. Thankfully, that is very easy to do.

In command mode, simply run the command:

:set number

To turn it back off, run its opposite number:

:set nonumber

You can also set either of these settings as your default by adding the same command to the bottom of the file .vimrc in your home directory (if it doesn't exist, simply create it).

$ cd
$ vim .vimrc

And there you go - line numbers!

Ultra quick start in Vim

Vim is a very powerful text editor. It is also quite an intimidating text editor to those that haven't used it before and if you're interested in just getting started with Vim and learning how to use it as a basic text editor, it can be quite daunting.

What I am going to attempt to give you today is an ultra quick start guide to editing files in Vim. Let me reiterate that - I'm going to go over the most basic concepts really quickly. There is plenty more that Vim can do and this is only really designed to whet your appetite for the editor.

So, let's begin. You can open Vim by typing vim and then a filename at your command line. That filename can be a file that already exists, or a new file you want to create.

$ vim newfile

Once you hit enter, the command line goes away and you enter the Vim interface. Vim has a concept of different modes for editing, which means in order to do some tasks you might need to be in a certain mode. The two modes we need to worry about here are Normal mode and Insert mode.

By default, you start in normal mode. In most modern versions of Vim, you should be able to browse the document if it exists with the arrow keys (or use Ctrl-D to scroll down more quickly).

To insert, or edit text, we need to switch from Normal mode into Insert mode. You do that by pressing the letter i. You should see this near the bottom of the screen:

-- INSERT --

Now, you can use the keyboard as you would normally to type text into the document. Once you're done making the edit and want to switch back to Normal mode, press Esc.

The INSERT message at the bottom should go away, and anything you type will be interpreted as a command, not as text to type into the document.

That is basically the bare minimum you need to know to get started editing in Vim, or if you just want to use it quickly. However, it would also be quite useful to know how to save and quit the editor.

While in Normal mode (remember - Esc will get you back to it at any time), typing these commands in and pressing Enter will do the following:

:w - save the file (the 'w' stands for write).
:q - quit the file. If you haven't saved the file since you last changed it, you will be warned and it will not quit. If you want to delete the changes...
:q! - quit the file, not saving your changes.
:wq - save and quit the file in one fell swoop!

I'm going to say this yet again - this is only scratching the surface of what Vim can do, but if you want to get up and running and editing files in it, this is the bare minimum of the knowledge you need!

  1. 1
  2. 2
  3. 3
  4. 4

Sign In

    Enjoy FOSSwire's content? Have it delivered! Subscribe