3

Is there a way to show the character under the cursor in the statusline?

I already use %b\ (0x%B) to display decimal and hexadecimal value of the character. I would like to display the char itself as well before these two.

Pontiac_CZ
  • 619
  • 1
  • 6
  • 13

1 Answers1

6

There's no predefined item (as listed under :help 'statusline', but you can implement this with a custom expression (item %{...}):

let &statusline .= "%{matchstr(getline('.'), '\\%' . col('.') . 'c.')}"

(I'm using :let instead of :set to avoid having to escape all spaces; it's more readable this way.)

getline('.') obtains the current line, and the character under the cursor is retrieved via the special \%c atom that matches at a certain column; col('.') is the current column. The . then matches the character there, and matchstr() extracts it.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Excellent solution! Works nicely and here is a small improvement which pads with two characters to handle Unicode characters that are two characters wide and will keep these two spaces if you fall on an empty line. This way, the length of the expression always remains 2, no matter what. `let &statusline .= "%2.2(%{matchstr(getline('.'), '\\%' . col('.') . 'c.')}%)"` – Hans Deragon Feb 06 '20 at 22:33