1

I am using a macbook with touchbar. As many others I would like to map a different key to the esc key, since the touchbar esc is a bit inconvenient. My problem is that I have some weird behaviour of the §-key which I would like to use as alternative. I had to remove the

map § <esc>

since the §-key doesn't work in the first place. I assume this has something to do with how I configured vim. I use vim inside zsh and have almost the same vimrc as this one https://github.com/amix/vimrc. I also have installed tabnine-vim which might interferes in some way.

In normal mode the §-key does nothing. Expect my mac beeps shortly. In Insert mode the §-key goes on character left but then remains there if I press it again. The Insert mode is not left.

The key works normal in the zsh. Also the key works if I write for example . By accident I also noted that whenever I type some ° characters, those get replaced by <80>kl when I hit the §-key on them in Insert mode. As far as I know this means left, and so far this replacement only appeared on ° characters.

How can I debug this? I have searched the vimrc if a remapping of the §-key already exists but could not find anything so far. I am rather new to vim. I still can't see how one best debuggs problems like this.

8
  • 1
    Your mapping map § <esc> only maps this in normal mode (and visual and select modes actually). You typically want to hit <esc> to exit insert mode, so you probably want something like inoremap § <esc> primarily instead? You can see whether the key will produce the correct code in insert mode by pressing Ctrl+V followed by that key, to see the exact sequence it produces. (I'd expect a literal §.) Have you tried any of the ways to map a key to <esc> globally on your macbook? In your keyboard preferences, you should be able to map Caps Lock to it... There's also Karabiner Elements.
    – filbranden
    Commented Apr 15, 2020 at 11:34
  • Thank you for your answer. I just tested the inoremap. Changed nothing. Ctrl+V followed by the key does show me the actual key. I would rather not change the esc key globally on my macbook since I sometimes use the key (but rarely to be honest). Is this the expected behaviour of the §-key? I mean what happens when you press this button? I assume my remapping should work once I am able to type a § without doing ctrl+V first.
    – Mr.Sh4nnon
    Commented Apr 15, 2020 at 11:41
  • Hm weird. It's a Swiss keyboard. The key should actually act as a normal key (no dead key). Disabling vimrc should have been an obvious test, sorry for that. Tested it right now. Now it works... so there has to be an error in my virmc
    – Mr.Sh4nnon
    Commented Apr 15, 2020 at 11:52
  • :set fenc? returns:'fileencoding= while :set enc? does show utf-8
    – Mr.Sh4nnon
    Commented Apr 15, 2020 at 12:03
  • See vi.stackexchange.com/q/2003/18609 and vi.stackexchange.com/q/7722/18609 for potentially useful troubleshooting advice.
    – filbranden
    Commented Apr 15, 2020 at 12:03

2 Answers 2

3

TL;DR: To prevent conflicts with mapping the § key and the auto-pairs plug-in, add the following to your vimrc:

let g:AutoPairsMoveCharacter = '()[]{}"'

The root of the problem is that Vim sees the § key the same as it sees the combination Alt+', or Meta+'.

The § symbol has character code U+00A7.

To represent keystrokes involving the Alt or Meta key, Vim OR's the character code with 0x80 hexadecimal. This is historically how this has been done back in the days of ASCII, of 8-bit character encodings, before Unicode and UTF-8. But Vim still uses that representation internally.

In any case, U+0027 is the character code for the ' symbol. So in Vim, <M-'> (corresponding to the aforementioned Alt+') will clash with §, particularly in a mapping.

This can be easily demonstrated, by creating a mapping for <M-'>, such as inoremap <M-'> something and then triggering it by pressing the § key, which should have Vim expand it to something.

It turns out the auto-pairs plug-in creates a mapping for <M-'>. More exactly, it creates mappings for all keys in g:AutoPairsMoveCharacter with the Meta modifier. This variable defaults to include the ()[]{}"' characters.

(For the point of these mappings, see the commit that introduced the quick move feature, although I find it doesn't work as well with the quotes and I don't much see the point of the open braces either. I'd say using the surround.vim plug-in in normal mode is a much better way of accomplishing that goal.)

In any case, to prevent auto-pairs from creating the <M-'> mapping that clashes with the §, you can simply set the g:AutoPairsMoveCharacter variable not to include the ' character. (But note that means it won't then work for the aforementioned auto-pairs feature.)

If you want to preserve all others:

let g:AutoPairsMoveCharacter = '()[]{}"'

If you want to only keep the closing braces, which seem to be the most useful ones for this feature:

let g:AutoPairsMoveCharacter = ')]}'

If you want to disable the feature altogether:

let g:AutoPairsMoveCharacter = ''

After removing the ' from this variable, it should be safe to create a mapping for § and at the same time keep the auto-pairs plug-in enabled. The plug-in will no longer clobber your mapping.

1

So thanks to @filbranden I was able to find the source of the error. This plugin causes it: https://github.com/jiangmiao/auto-pairs I have yet no clue how the error is produced. The plugin should not care about § characters. Also the § character does not appear a single time in the repository. I actually like the plugin. If I find an alternative my problem would be solved. Despite that I would like to find the error to get a better understanding of vim and its plugins. But thats probably something for another question :)

2
  • 1
    Try re-enabling auto-pairs and adding this to your vimrc: let g:AutoPairsMoveCharacter = '()[]{}"'. I expect that should fix it. (The issue is a clash between § and the <Meta-'> combination.)
    – filbranden
    Commented Apr 15, 2020 at 13:32
  • 1
    It did work. Thank you! You definitely know VIM :)
    – Mr.Sh4nnon
    Commented Apr 15, 2020 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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