Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript status bar


May 24, 2021 Vim


Table of contents


Vim allows you to customize the text displayed by the status bar at the bottom of each window, which you can customize by setting the statusline option. Execute the following command:

:set statusline=%f

You can see the path to the currently edited file (as opposed to the current path) on the status bar. Then execute this command:

:set statusline=%f\ -\ FileType:\ %y

Now you can see words like foo.markdown - FileType: [markdown] status bar.

If you are familiar with printf Python string interpolation in the C language, the format of this option will look familiar. I f you're not familiar with it, you just need % that strings that start with % are expanded into different text, depending on % that follow %. In the example above, %f replaced with the file name and %y replaced with the file type.

Note that spaces in the status bar need to be escaped by an anti-slash set can set multiple options at the same time, which are separated by spaces, as we said in Chapter 2.

State bar settings can quickly become very complex, but there is a better way to set them up to make them look clearer. Execute the following command:

:set statusline=%f         " 文件的路径
:set statusline+=\ -\      " 分隔符
:set statusline+=FileType: " 标签
:set statusline+=%y        " 文件的类型

The first command uses the = set the status bar to display only the file name, thus masking all previous values that would appear in the status bar. T hen gradually += to display, one at a time, using . . . Comments are also used to illustrate what each one means to make it easier for others to read our code (and for us to read it later).

Execute the following command:

:set statusline=%l    " 当前行号
:set statusline+=/    " 分隔符
:set statusline+=%L   " 总行数

The status bar now contains only the current row and the total number of rows 12/223

Width and margin

You % additional characters after % to change the display style of the information in the status bar. Execute the following command:

:set statusline=[%4l]

The number of file lines in the status bar now appears at least 4 characters wide (ex: [ 12] which can be used to prevent text in the status bar from always jumping around boringly.

By default, margins are added to the left of the value. Execute the following command:

:set statusline=Current:\ %4l\ Total:\ %4L

Your status bar looks like this:

Current:   12 Total:  223

You can - add margins to the right instead of the left. Execute the following command:

:set statusline=Current:\ %-4l\ Total:\ %-4L

Now your status bar looks like this:

Current: 12   Total: 223

This makes it much better because the number value is next to its label.

For code that will appear as a number, you can have Vim fill the margins with 0 instead of spaces. Execute the following command:

:set statusline=%04l

Now when the cursor is on line 12, your status bar 0012

Finally, you can set the maximum width of the value that your code wants to output. Follow these commands:

:set statusline=%F

%F shows the full path to the current file. Now perform the following command to change the maximum width:

:set statusline=%.20F

If necessary, the path will be deleted, as follows:

<hapters/17.markdown

This can be used to prevent the path or other long code from taking up the entire line.

Common format

Read: :help statusline common format of the code in the status bar:

%-0{minwid}.{maxwid}{item}

All % and item are optional.

Segmentation

We won't go into more details about the status bar (Vim's documentation is very detailed, and if you want to learn more, it's recommended to read them), but we'll introduce a simple code that brings immediate value. Execute the following command:

:set statusline=%f         " 文件的路径
:set statusline+=%=        " 切换到右边
:set statusline+=%l        " 当前行
:set statusline+=/         " 分隔符
:set statusline+=%L        " 总行数

The left side of the status bar now contains the path to the file, and the current row/total number of rows appears to the right of the status bar. %= tells Vim that all information to be displayed on the status bar after this should be aligned to the right (as a whole) rather than to the left.

Practice

Browse :help statusline Don't worry about code that you don't understand right now.

Edit your ~/.vimrc to create a custom status bar. Make sure set define += to be displayed one by one, using s, and that the settings for each line add comments to illustrate the meaning of each one.

Try using automatic commands setlocal to define different state bars for different files. Make sure that you use automatic command groups to prevent automatic commands from being created repeatedly (always remember).