This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!

Feature: Desktop Software

A more informative status line for Vim

By Kim Schulz on November 02, 2007 (8:00:00 AM)

Share    Print    Comments   

At the bottom of the Vim editor, you will find two things: the command-line buffer (where you can input commands) and the status line. In the default configuration, Vim has a simple and non-informative status line, but you can make the status line a lot more informative with simple methods.

This article is excerpted from the recently published book Hacking Vim.

To the right, the default status line shows the number of the current row and column and to the left it shows name of the file currently open (if any). Whenever you execute a Vim command, the status line disappears and the command buffer is shown in that line instead. If the command you execute writes any messages, then those are shown on the right of the status line.

For simple and fast file editing, this status line is adequate. But if you use Vim every day and for a lot of different file formats, it would be nice to have a more informative status line.

The command that sets how the status line should look is called:

:set statusline format

where format is a printf-like string that describes how the status line should look.

If you look in the Vim help system by typing :help 'statusline', you will see that the status line can contain a wide variety of information. Some pieces are more useful in your daily work than others.

My status line always contains information about:

  • Name of the file that I am editing
  • Format of the file that I am editing (DOS, Unix)
  • Filetype as recognized by Vim for the current file
  • ASCII and hex value of the character under the cursor
  • Position in the document as row and column number
  • Length of the file (line count)

The following command will turn your status line into a true information bar with all the above information:

:set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L] 

I have added a '[ ]' around each of the pieces of information so that it is easier to distinguish them from each other. This is purely to give a visual effect; you can leave them out.

Even after entering that command, the status line still shows the old non-informative status line, as in the default installation. This problem occurs because Vim, by default, does not show the status line at all. Instead, it just shows the command buffer with a little bit of information in it. To tell Vim that you would like to have a real status line shown, you have to add the following setting to your vimrc file. This command will make sure that your status line is always shown as the second last line in the editor window:

:set laststatus=2 

You will then see that the command buffer gets a place of its own in the last line of the editor window. This way there's always room for the status line, and you will always have information about the file right in front of you. The status line does of course take up some of the editing area. You can always remove it for the rest of the editing session by executing the following command from within Vim:

:set laststatus=0

Toggle menu and toolbar

If you are used to working with Vim in the console mode, you are used to having no menus and toolbars at the top of the window. Gvim, however, provides both a menu and toolbar by default in the GUI.

Many users believe that extra room for text is more important than the menu and the toolbar. However, some scripts add useful functionality in the menu, and it is therefore important to have the menus. The solution for this could be toggling whether the menu and toolbar is shown.

The following code maps the key combination Ctrl-F2 to toggle the menu and toolbar in Gvim. You can add it to your vimrc file if you want this functionality.

map <silent> <C-F2> :if &guioptions =~# 'T' <Bar>
                         \set guioptions-=T <Bar>
                         \set guioptions-=m <bar>

                    \else <Bar>
                         \set guioptions+=T <Bar>
                         \set guioptions+=m <Bar>


Now, whenever you don't need the menu and toolbar, you can just press Ctrl-F2 and you will get the full space for your text.

If you want either the menu or the toolbar to be hidden all the time, add one of the following lines to your vimrc file. To remove the menu completely:

:set guioptions-=m 

To remove the toolbar completely:

:set guioptions-=T 

Other parts of the GUI can be modified with the setguioptions command. To find out what you can modify, look in :help 'guioptions'.

Share    Print    Comments   


on A more informative status line for Vim

Note: Comments are owned by the poster. We are not responsible for their content.

Here's mine

Posted by: Anonymous [ip:] on November 02, 2007 09:45 AM
set statusline=%<[%02n]\ %F%(\ %m%h%w%y%r%)\ %a%=\ %8l,%c%V/%L\ (%P)\ [%08O:%02B]


A more informative status line for Vim

Posted by: Anonymous [ip:] on November 02, 2007 01:47 PM

just a minor improvement for everybody who is editing unicode files (japanese text files, and so on):

:set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%04.8b]\ [HEX=\%04.4B]\ [POS=%04l,%04v][%p%%]\ [LEN=%L]



Default is fine for me

Posted by: Anonymous [ip:] on November 02, 2007 02:52 PM
Configuring the status line is great, but I found the default to be more than what I need, despite of me using vim on a daily basis. The name of the file is shown in my xterm caption anyway and normally you just know which file you're editing, you don't need to see it all the time. And if you need to, you can hit Ctrl-G, how hard is that? Whether it's a DOS or Unix file, it displays it uppon load and save, that's more than enough. I don't need the ASCII/hex value of the character under the cursor all the time, do I? If I want that, I use the 'ga' command, or 'g8' command, which is really really quick enough to type. The length of file in lines and characters is displayed uppon loading and saving too, and if that's not enough using 'Ctrl-G' will tell you the number of lines too.

Yeah, what I want to say is that all those information are very very very easily accessible anyway, putting them all in the status line is just clutter that you'll rarely even look at anyway. I can of course imagine that there's useful information that can be displayed in the status line, but the examples in this article aren't convincing me at all. Just my two cents, though.


Re: Default is fine for me

Posted by: Anonymous [ip:] on November 05, 2007 03:55 PM
If you are editing 1 file at a time, you will probably know the name of that file. But if you edit multiple files (ie. vim file*.c) it's easy to lose track of where you are. Plus when you come back to a file (:#n = forward, :#N = backward), it can be helpful to see other status. I don't do this constantly, but I do it frequently enough that the extra status info is a nice option to have available. And still, Vim is developer friendly in allowing us to use almost all of the window for text when we want.


thanks, nice article albeit a bit too noisey/contrastey example offered

Posted by: Michael Shigorin on November 05, 2007 05:42 PM
I've went with this:

hi User1 term=underline cterm=bold ctermfg=Cyan ctermbg=Blue guifg=#40ffff guibg=#0000aa
set statusline=%1*%F%m%r%h%w%=%(%c%V\ %l/%L\ %P%)
set laststatus=2


A more informative status line for Vim

Posted by: Anonymous [ip:] on November 06, 2007 12:25 AM
... or you could use emacs


Mysetting: FullScreenmode

Posted by: Anonymous [ip:] on November 06, 2007 07:05 PM
Push F11 to get into full screen mode (Win32 only)
map <silent> <F11> :if &guioptions =~# 'T' <Bar>
\simalt ~X<CR> <Bar>
\set guioptions-=T <Bar>
\set guioptions-=m <Bar>
\set guioptions+=C <Bar>
\else <Bar>
\set guioptions+=T <Bar>
\set guioptions+=m <Bar>
\set guioptions-=C <Bar>
\simalt ~R<CR> <Bar>


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya