Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim keyboard mapping problems on mac OSX

Tags:

vim

macos

iterm2

1.Env

vim7.3+zsh+iTerm2 on mavericks

2. Mapping Problem

Here are my problem list:

  1. My vim could not get some shortcuts: <C-q>,<C-s>,<A-Left|Right|..>,<D-char>.
  2. I could not map some special keys such as <A-char> , <A-S-char> , <C-A-char>in mac.

<C-q> and <C-s>

In iTerm2, <C-q> will delete whole line, (and <C-s> will launch Fwd-i-search).
It doesn't behave like start/stop character , so that I thought this problem is nothing to do with stty start and stty stop.
But after I add this into ~/.zshrc, I surprised to see that the map for <C-q> and <C-s> works well in Vim. At the same time it doesn't change the behavior of <C-q> and <C-s> in iTerm2(I don't know why).

stty start undef
stty stop undef

<A-char> problem in mac

In mac, mappings for <A-char> won't directly work in mac.

:inoremap <A-u> type some string A-u
:inoremap <A-p> type some string A-p
:inoremap
i  õ           * type some string A-u
i  ð           * type some string A-p

As far as I know about <A-char> in mac, hit <A-u> will print nothing but with a sound alert. hit <A-p> will print π. I found that map for π works well.

:inoremap π type some string A-p

Is it possible to map <A-u> in vim?

Another complex question, is it possible for <A-S-char>, <C-A-char>

:map <A-S-p> not work
:map ∏ works well(A-S-p will print `∏`)

:map <C-A-p> not work
:map <C-π> still not work

:map <C-S-p> do work

Another complex question related above, is it possible mapping <C-A-char>?

<A-Left|Right|UP|Down

In iTerm2 , hitting <A-Left|... will result in(iTerm2 doesn't bind any shortcut for them.):

<A-Up> print 'A' with a sound alert.
<A-Down> print 'B' with a sound alert.
<A-Left> print 'D' with a sound alert.
<A-Down> print 'C' with a sound alert.

In vim, <A-Left|... is same as <Left><Right><Up><Down> in insert mode , normal mode, visual mdoe and Ex Mode, etc.

<D-char>

Some keys about <D-char> are iTerms's own hotkeys, such as : 1. <D-q> will quit iTerm2 1. <D-w> will close current tab in iTerm2

On the other hand, the other keys such as <D-s> are not hotkey, and do not print any char in vim and terminal. Is it possible to map them in vim?

like image 680
ahuigo Avatar asked Jun 17 '26 00:06

ahuigo


1 Answers

Map <C-1>

Vim doesn't provide keycode <C-1>, but you can use other unused vim keycodes instead

Potentially unused Vim keycodes that can be used include:

<F13> to <F37>
<S-F13> to <S-F37>
<xF1> to <xF4>
<S-xF1> to <S-xF4>

Type :set termcap to see which vim keycodes are unused.

If you want to use hotkey Ctrl+1, map it to Esc Sequence (such as ^[C-1) in terminal preference first, then bind this terminal keycode with an unused vim keycode such as <F13>, <xF1>, <t_bc>, etc.

"bind vim keycode `<F13>` to terminal keycode `^[C-1`
:set <F13>=^[C-1
:imap <F13> Input some characters

For more details, refer to fastcodes in vim


Map <A-char>

By default, <A-char> will print special character in mac. In generally, you can map this special character. Or you can remap <Alt> as Meta in your terminal.

Map <C-A-char> and <D-char>

Same with <C-1>, if you use termainal vim you could use fastcodes instead of.

  1. Bind <C-A-char> with terminal any keycodes, for example : ^[C-A-a
  2. Set vim fastkeycodes and do mapping:

Mapping:

:set <C-A-a>=^[C-A-a
:map <C-A-a> Input some characters
like image 56
ahuigo Avatar answered Jun 21 '26 15:06

ahuigo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!