countunique
countunique

Reputation: 4296

How to add total line count of file to vim status bar

How can I edit my status bar to always display the total number of lines in the open file?

Looked at vim-airline, but didn't see an easy way to do it.

Upvotes: 3

Views: 3781

Answers (3)

user8425970
user8425970

Reputation:

Using the airline plugin, I solved this problem by adding the following lines of code to my init.vim file. If you are using Vim instead of Neovim, put them in your vimrc file instead.

let g:airline_symbols.linenr = '␤'
let g:airline_section_z = airline#section#create(['%3p%%: ', 'linenr', '/%3L', ':%3v'])

The result of using the above code

Upvotes: 0

Troy Fletcher
Troy Fletcher

Reputation: 58

Example of statusline results

If you wanted to make this small addition to the default airline statusline instead of writing a new statusline, you can update the appropriate airline section.

In my case I added the total line count after the current line number like 3/45.

Airline's section with the current line number is z, which you can check by running echom g:airline_section_z which should returns something like:

%3p%% %{g:airline_symbols.linenr}%#__accent_bold#%4l:%#__restore__#%3v

You can update it to include the vim statusline code for total line number %L

%3p%% %{g:airline_symbols.linenr}%#__accent_bold#%4l/%L:%#__restore__#%3v

I also moved the %#__restore__# back a little to make the current line number the only thing bolded.

%3p%% %{g:airline_symbols.linenr}%#__accent_bold#%4l%#__restore__#/%L:%3v

Test with let g:airline_section_z = 'test string here' followed by :AirlineRefresh until you get it right, then make it stick in the vimrc with:

function! AirlineInit() let g:airline_section_z = 'your string' endfunction autocmd VimEnter * call AirlineInit()

Which is apparently the canonical way of doing this...

Upvotes: 2

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

set statusline =%1*\ %n\ %*            "buffer number
set statusline +=%5*%{&ff}%*            "file format
set statusline +=%3*%y%*                "file type
set statusline +=%4*\ %<%F%*            "full path
set statusline +=%2*%m%*                "modified flag
set statusline +=%1*%=%5l%*             "current line
set statusline +=%2*/%L%*               "total lines
set statusline +=%1*%4v\ %*             "virtual column number
set statusline +=%2*0x%04B\ %*          "character under cursor

vim statusline

And here's the colors I used:

hi User1 guifg=#eea040 guibg=#222222
hi User2 guifg=#dd3333 guibg=#222222
hi User3 guifg=#ff66ff guibg=#222222
hi User4 guifg=#a0ee40 guibg=#222222
hi User5 guifg=#eeee40 guibg=#222222

Upvotes: 9

Related Questions