2

I've got some code to print the current mode in the status line.

let g:currentmode={
    \ 'n'  : 'Normal',
    \ 'no' : 'Normal·Operator Pending',
    \ 'v'  : 'Visual',
    \ 'V'  : 'V·Line',
    \ '^V' : 'V·Block',
    \ 's'  : 'Select',
    \ 'S'  : 'S·Line',
...

Used like this set statusline+=\ %{toupper(g:currentmode[mode()])}.

It works great for all modes except Visual Block where I get this error E716: Key not present in Dictionary: ^V.

I've tried

  • printing the mode() directly and copied that into the dictionary
  • insert the Unicode character ^ (U+005E) as described here
  • writing on my Swedish keyboard: <SHIFT-¨><SPACE>. " key is placed to the bottom left of backspace, and requires you to push it twice to produce output, or once and then space. Just to be totally safe I've tested both clicking twice, and clicking once, and then SPACE.

None of these methods work. I've tried finding out more about the more() function (or what is it?), but without luck. I'm not sure where that functionality is coming from, but it's being used in several blog posts, without specifying where it's documented, for example here.

I guess there's something with my Swedish keyboard setup. I've also had problems with mappings.

  • 1
    you need a literal Ctrl-V, that is press Ctrl-L, followed by Ctrl-V in insert mode – Christian Brabandt May 8 '20 at 15:26
  • 2
    "\<C-V>": 'V-Block' – Matt May 8 '20 at 15:41
2

The mode() function returns an actual Ctrl+V character when you're in Visual Block mode.

It might look like ^V in a Vim buffer, but it's actually a single character (rather than separate ^ and V), which you can typically tell by moving the cursor through it using l or a right arrow (cursor will only stop on the ^ and skip the V to the next character in line) and it will usually be highlighted in a different color to indicate it's a control character and not a couple of normal characters.

You have two main options to represent a Ctrl+V here:

  1. You can enter a literal Ctrl+V in your file. It turns out that Ctrl+V itself is the sequence used to enter a literal special key in a buffer, so you could use the following sequence:

    ', Ctrl+V, Ctrl+V, '

    Which will show up as '^V' but you'll see that the ^V is highlighted differently and when moving across it it will only stop on the ^, so you'll know it's a literal character.

  2. Or you could use a <C-v> sequence that represents a literal Ctrl+V without having to enter a literal Ctrl+V in your document. In order to use such a sequence, you'll need to use double quoted strings and use a backslash escaped \<xxx> sequence, like so:

    "\<C-v>" : 'V·Block',

My recommendation would be to use the second solution ("\<C-v>"), since that avoids entering a literal control character into your file, which should make it easier to for example copy and paste those contents without falling into the trap of copying a literal ^V into two separate characters ^ and V.

  • 2
    Great explanation, solved my problem, and very informative. – Max May 9 '20 at 0:33

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.