14

before describing my problem, I'd list the env. applications here:

OS:linux 2.6.37-ARCH  (archlinux i686)
vim: 7.2.436
Terminal emulator: urxvt (with 256colors patch)

kent$ echo $TERM
rxvt-256color

screen: Screen version 4.00.03 (FAU) 23-Oct-06

I run vim in terminal. I want to move cursor in INSERT mode by pressing ALT-hjkl, after the cursor moved, stay in INSERT mode, so that I could continue typing words.

articles I found:

http://vim.wikia.com/wiki/Mapping_fast_keycodes_in_terminal_Vim

http://vim.wikia.com/wiki/Get_Alt_key_to_work_in_terminal

what I tried:

in .vimrc do a keyCode mapping with ttimeoutlen=50 like this: ( only alt-j mapping was pasted as example):

set timeout ttimeoutlen=50
set <F13>=^[j  "ctrl-v alt-j
imap <F13> <down>

with this conf, moving cursor in INSERT mode was ok. If I press <ESC> and j. Vim brings me back to insert Mode. I don't know why the ttimeoutlen=50 didn't work.

also tried:

set timeout ttimeoutlen=50
set <M-j>=^[j

With this setting, when I pressed ALT-j, a "e" with an accent mark was typed.

Can you guys give me any hints how should I map the ALT-hjkl in terminal ?

Thanks in advance

Kent

Kent
  • 189,393
  • 32
  • 233
  • 301

2 Answers2

29

It's easier to map what your key combination does. Alt+something generally results in a character, differently from Ctrl+something.

For example, on my Mac Alt plus hjkl generates ˙∆˚¬. So:

imap ˙ <Left>
imap ∆ <Down>
imap ˚ <Up>
imap ¬ <Right>

would do it.

sidyll
  • 57,726
  • 14
  • 108
  • 151
  • 2
    if i type ctrl-v alt-h,j,k,l in INSERT mode with my urxvt terminal. I got ^[j ^[h ^[k ^[l – Kent Mar 22 '11 at 12:39
  • @Kent, not typing ctrl-v before, if you press only alt+h, alt+j, alt+k and alt+l, what's the output? – sidyll Mar 22 '11 at 14:06
  • if I type alt-j for example, in INSERT mode (under terminal), vim will be changed into Normal Mode, and cursor will move one line down. this because, in terminal "ALT-*" terminal keycode is ^[*, ^[ is if we talk about vim keycode. – Kent Mar 23 '11 at 11:06
  • @Kent, I think I understand it correctly now. Searching over the internet, I've found messages in a Yahoo! Group describing the same problem. You may want to have a look on the other messages also, but the one that contains the solution (actually 2 of them!) is here: http://tech.groups.yahoo.com/group/vim/message/55886 Basically, you want to put a `` in the mapping or set `` to ``. I hope it works! – sidyll Mar 23 '11 at 23:19
  • I have tried the 'solutions' there before I posted the question. they didn't work by me... :( but thanks anyway. – Kent Mar 24 '11 at 09:36
  • @Kent, I'm sorry, but my resources ended. I hope you can find the solution! Once you do so, please post it here as an answer so we can have it registered. Regards – sidyll Mar 24 '11 at 11:50
  • For French people (with a mac AZERTY keyboard), the corresponding keys are ÌÏȬ – Max Jul 09 '12 at 14:13
  • 4
    For anyone else that comes across this, for use with a mac, you are overall better off just making alt work as alt should. For terminal.app: settings -> keyboard -> "use option as meta". For iterm: settings -> profile -> your profile -> keys -> set option to "+Esc" – demure May 18 '13 at 17:14
  • Do you know to make an analogous solution work in tmux? See http://stackoverflow.com/questions/35344479/makikng-alt-h-switch-to-the-left-pane-in-tmux for a question I just asked that uses your advice. – George Feb 11 '16 at 16:17
15

For arrow keys:

Start by viewing the key code your terminal is sending to vim:

$ sed -n l
^[[1;9D 

In the above example, i ran the sed command and pressed Alt + Left.

The ^[[1;9D is the escaped sequence being sent to vim, so we can user that for our mapping.

Add this to your .vimrc

map <Esc>[1;9D :tabn<CR>

Now we can cycle through vim tabs by using Alt + Left

random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
  • Thanks, it works for me. BTW, in cygwin ALT+Left output is \033[1;3D$, here \033 is , and we need to remove the '$' – camino Oct 08 '15 at 20:35
  • 1
    Had to use this approach with osx mavericks / iTerm / Vim 7.4. Works like a champ- thanks for the alternate solution. – visyoual Nov 25 '15 at 20:48
  • Do you know to make an analogous solution work in tmux? See http://stackoverflow.com/questions/35344479/makikng-alt-h-switch-to-the-left-pane-in-tmux for a question I just asked that uses your advice. – George Feb 11 '16 at 16:17
  • @George let me check with a friend he might have the answer – random-forest-cat Feb 11 '16 at 19:01
  • 1
    @lfender6445: If this were real life, I would offer you a beer :) – George Feb 12 '16 at 00:05