It's no secret that I like using Vim. Vim is characterized by its extensibility. There are also wonderful plugins and modules that make the status bar look nice. That is, the bar at the bottom of Vim that contains all the important information. Sure, I could use
Airline or
Lightline ... or build the status line myself. So I'm building one myself.
0x01 - STATUSLINE
The status line in Vim contains a lot of information that helps you keep track of things. For example, which mode is currently active, the file name of the open file, are there any changes in the file, file format, encoding, position of the cursor in the file and so on. If you want to keep it simple, use the plugins mentioned above, install them using the Plugin Manager and have fun with it. However, if you are not completely happy with it, you can build your own status line in the
.vimrc file. At first, that sounds incredibly complicated and like "open-heart surgery", but I can reassure you. It's easier than you think.
Like so many things, the status line can also be configured in the
.vimrc. The
.vimrc is located in your $HOME directory by default. The
.vimrc is written in VimScript, but is still relatively easy to read.
0x02 - ACTIVATE STATUSLINE
In the first step we activate the status line (if not already done) with the following entry:
.VIMRC
This means that you have activated the status line after restarting Vim. Now it's time for the content.
Because after the simple activation you only have a white status line with the file name. This is boring in the long run and not very informative.
To add modules or information to the status line, you need the following entry in the
.vimrc for each element that is to be added:
.VIMRC
Behind this are our modules and elements that we want to insert. My status line starts with a small alien. This looks like this in the
.vimrc:
.VIMRC
set statusline=
set statusline+=\
To display several elements one after the other in the status line, a "+" is added to
set statusline=, i.e.
set statusline+=.
This was just a gimmick and is not really essential for productivity. What is more important, however, is the current mode that Vim is in, and preferably in color. A little more is needed here.
0x03 - VIM MODE
To display the mode that vim is in, a little more scripting is needed. So before we can display the mode in the status line, we need the following script snippet in the
.vimrc:
.VIMRC
let g:currentmode={
\ 'n' : 'NORMAL ',
\ 'v' : 'VISUAL ',
\ 'V' : 'V-LINE ',
\ 'i' : 'INSERT ',
\ 'R' : 'R ',
\ 'Rv' : 'V-REPLACE ',
\ 'c' : 'COMMAND ',
\}
With
let g: the variables are created globally so that they can be accessed and called from anywhere. Now the modes must be passed on to the status line:
.VIMRC
statusline+=\ %#NormalC#%{(mode()=='n')?'\ NORMAL\ ':''}
statusline+=\ %#InsertC#%{(mode()=='i')?'\ INSERT\ ':''}
statusline+=\ %#VisualC#%{(mode()=='v')?'\ VISUAL\ ':''}
The variables
%#NormalC#%,
%#InsertC'% and
%#Visual#% define the colors that are used here. These are taken directly from the colorscheme used. This is where a big advantage of building your own status line becomes apparent. We have the option of taking the colors directly from the Vim theme and don't need a separate status line colorscheme. This can be quite tricky under Lightline, for example (creating your own Vim colorscheme is already on the list of things I need to write down).
Alienhead + Mode are pretty neat, but the status line can do even more.
0x04 - MODULES
As I wrote above, there are various modules that we can integrate into the status line.
The Vim Help provides a good overview of all the options.
Here you can choose modules that you can integrate into your status line. You can find the help file for the status line in Vim with
:help statusline. In my status line, in addition to the alien head and mode, you can also find the file name, changes, file format, encoding and the line position and total number of lines.
The next part of my
.vimrc looks like this:
.VIMRC
set statusline+=%#Filename#
set statusline+=\ %f
set statusline+=%#ReadOnly#
set statusline+=\ %r
set statusline+=%m
set statusline+=%=
set statusline+=%#Fileformat#
set statusline+=\ %y
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
set statusline+=\ [%{&fileformat}\]
set statusline+=%#Position#
set statusline+=\ [%l/%L]
The clearly readable variables are again the color codes. The rest are variables that are also listed in the help file:
.VIMRC
%f -- Filename
%r -- Readonly
%m -- Modified (y/n)
%= -- Alles was folgt ist rechts in der statusline
%y --
%{&fileencoding?&fileencoding:&encoding} -- Encoding
[%{&fileformat}\] -- Fileformat
[%l/%L] -- Cursorline / Lines Total
And now you have a useful status line that doesn't look quite so boring and you've saved yourself a plugin.
The
.vimrc is extremely expandable. Most plugins do nothing other than outsource code that would fit into the config. Don't get me wrong - plugins are cool. I'll take a look at it too - I already have a funny idea, but more on that when the time comes. Finally, I'll put the complete status line here for you.
Have fun hacking your status line.
0x05 - THE COMPLETE STATUSLINE
.VIMRC
"STATUSLINE
set laststatus=2
set noshowmode
"STATUSLINE MODE
let g:currentmode={
\ 'n' : 'NORMAL ',
\ 'v' : 'VISUAL ',
\ 'V' : 'V-LINE ',
\ "\" : 'V-BLOCK' ,
\ 'i' : 'INSERT ',
\ 'R' : 'R ',
\ 'Rv' : 'V-REPLACE ',
\ 'c' : 'COMMAND ',
\}
set statusline=
set statusline+=%#Icon#
set statusline+=\
set statusline+=\ %#NormalC#%{(mode()=='n')?'\ NORMAL\ ':''}
set statusline+=%#InsertC#%{(mode()=='i')?'\ INSERT\ ':''}
set statusline+=%#VisualC#%{(mode()=='v')?'\ VISUAL\ ':''}
set statusline+=%#Filename#
set statusline+=\ %f
set statusline+=%#ReadOnly#
set statusline+=\ %r
set statusline+=%m
set statusline+=%=
set statusline+=%#Fileformat#
set statusline+=\ %y
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
set statusline+=\ [%{&fileformat}\]
set statusline+=%#Position#
set statusline+=\ [%l/%L]