Map vim mac key with alt

1k Views Asked by At

What I want to do

I want to map <S-A-j> and <S-A-k> on a Mac keyboard to move selected lines up and down. So, I map:

" Move selected lines
xnoremap <S-A-j> :m'>+<CR>gv=gv
xnoremap <S-A-k> :m-2<CR>gv=gv

But it's not working, so type the <S-A-j> keys in insert mode:

  • For <S-A-j> it's writing Í
  • For <S-A-k> it's writing Ë

So I try this :

xnoremap Í :m'>+<CR>gv=gv
xnoremap Ë :m-2<CR>gv=gv

It's perfectly working. The problem is that is not easy to read and it feels like a bug right?

What I try

Mappings

"xnoremap <S-j> <A-j> :m'>+<CR>gv=gv
"xnoremap <S-k> <A-k> :m-2<CR>gv=gv

"xnoremap <S-J> <A-J> :m'>+<CR>gv=gv
"xnoremap <S-K> <A-K> :m-2<CR>gv=gv

"xnoremap <S-j><A-j> :m'>+<CR>gv=gv
"xnoremap <S-k><A-k> :m-2<CR>gv=gv

"xnoremap <S-j> <M-j> :m'>+<CR>gv=gv
"xnoremap <S-k> <M-k> :m-2<CR>gv=gv

Differents Vim

MacVim and Neovim

Terminals

I try to run on the default terminal.

I also configure my Iterm2 terminal with pref > profile > keys -> option key=Esc+

My environment

  • Apple M1 Pro
  • macOs Ventura 13.4
  • Iterm2
  • zsh
  • level: I use Ideavim frequently on Android Studio but don't really understand Vim and how it works.

My Solution

Remap your AZERTY input to make understand Vim what you want

map Í <S-A-j>
map Ë <S-A-k>
xnoremap <S-A-j> :m'>+<CR>gv=gv
xnoremap <S-A-k> :m-2<CR>gv=gv

It will be interesting to remap all the keys from AZERTY to QWERTY for specials key (with Alt or/and Ctrl) on a different file and then source it. I didn't find any error at the moment, in insert mode he recognizes well the right character.

2

There are 2 best solutions below

1
romainl On

This is mostly a Mac thing with international keyboards.

Vim doesn't know that you pressed Ctrl, Opt, and j together: all it sees is Í, so that's all you can use in your mapping.

Moreover, Í is actually produced by Opt+j. The Ctrl modifier does nothing, here.

So… what are your options?

  • Use the actual character produced by the key combination:

    xnoremap Í :m'>+<CR>gv=gv
    

    Pros:

    • works
    • no setup necessary

    Cons:

    • not portable
    • unreadable if you don't know your keyboard layout
  • Set up your terminal emulator to use "Option as Meta" (Terminal.app) or "Esc+" (iTerm.app) and use <Esc> in your mapping:

    xnoremap <Esc>j :m'>+<CR>gv=gv
    

    Pros:

    • more readable than the alternative
    • slightly more portable

    Cons:

    • makes inserting special characters harder
    • potentially breaks trivial things like leaving insert mode and moving the cursor down
    • still hides the exact keys you press
    • requires setup
0
MakiX On

You can add this file (for example to your USER folder) .vimMacKeys

" keyboard remap for mac
if has('mac')                            
    source ~/.vimmackeys
endif 

Also can add the content directly inside your .vimrc file of course. Note it's done from an FR keyboard and only for letter keys. So feel free to update it for your keyboard type.

Works for Vim & IdeaVim.