1

I'm using vim-airline and would like to add a count of all characters on the current line to it. It should be displayed like 1:75 where 1 is the column the cursor is currently on and 75 the total character count.

0

You can customize the section that includes line and column information in vim-airline by setting the airline_section_z variable.

There's not really a direct escape sequence for the total number of columns in the current line, but you can use an expression %{...} which gets evaluated by 'statusline' (see the documentation on the { NF item.)

For an expression, you can use the col() function, in particular, you can use col("$")-1 which evaluates to the total number of bytes in the line.

Unfortunately, setting airline_section_z isn't that convenient, since when you do that, you override the default set by vim-airline itself, so you need to fully set it to all that you want to include.

For example, if you want to preserve whatever was already there, you'll want to query it on Vim before any changes and then set it in your ~/.vimrc to include whatever it was set to in addition to the modifications you would like to introduce.

For example, in my case:

:let g:airline_section_z

Returns:

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

So I can customize it to add the column number in my ~/.vimrc using:

let g:airline_section_z = '%3p%% %#__accent_bold#%{g:airline_symbols.linenr}%4l%#__restore__#%#__accent_bold#/%L%{g:airline_symbols.maxlinenr}%#__restore__# :%3v/%03{col("$")-1}'

Where I added a /%03{col("$")-1} to the end of the string previously set by default by vim-airline itself. (The %03{...} shows the total column count with width 3 and leading zeroes.)

In your case, you mentioned using : as a separator, so you might be considering further customizations to airline_section_z, in which case this might be more acceptable to you.

  • Thanks, this works, but only to some extent (at least for me), since it becomes problematic when I use non-ASCII characters. For example, if I write a German text and use umlauts 'ä, ö, ü', the byte and character count differ. So it can happen that I get displayed something like 80/79 when the cursor is at the end of the line. I don't know if there would be also a solution for this problem. – Philipp L. Jul 14 at 9:05
  • Hmm I actually tested for that particular case and didn't see an issue at first, but maybe I didn't test completely for that. Yes, I think there's a solution for that, let me take a look if I find it. – filbranden Jul 14 at 15:26
  • I get something different, for instance I get 8/13 when appending to a line with 3 single-byte characters and 5 umlauts. – filbranden Jul 14 at 15:31
  • I get 80/79 if I'm appending to a line, since the column of the cursor is actually longer than the line itself. – filbranden Jul 14 at 15:34
  • If the reason you're looking into this is to spot long lines (for example, style guides that impose 80 or a specific number of columns), then maybe the 'colorcolumn' option is a better approach to visually see that? – filbranden Jul 14 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.