"M-" bindings in vim on iTerm2/Terminal don't work - vim

I noticed that iTerm2 offers the very convenient feature of using "Option key as +ESC" (or as meta, but apparently that's obsolete).
I tried them both and the option key works as expected in Bash (set -o emacs mode) and Emacs, but not in Vim. Thus, I can't make use of any "M-" bindings.
What I've found is that:
if I set option to "+ESC", vim just understands ESC+key, and has no idea I actually meant Meta-key.
if I set option to "meta", keys modified with "option" behave just as without a modifier (don't know how to formally test what vim understands from a key combination that I'm typing).
"M-" mappings work perfectly in MacVim (7.3.53), but that's outside of a console and not in the scope of this question.
I'm interested in how to make those mappings work in Vim, under iTerm2 or Terminal.
My specs:
MAC OS X Lion 10.7.2
iTerm2 1.0.0.20111020
vim #7.3.107_0+python26 [though MacPorts]

I've downloaded the latest iTerm2 and tried to see what it sent to/what was printed by Vim (i<C-v><M-a>) with the following settings:
Option as Option:
Vim prints æ which is normal and expected on my french keyboard
Option as Meta:
Vim stays there, waiting for something to happen. Nothing is printed. If I press Option and a in sequence I just obtain a. Pressing Option and a in some random order may print á, which is weird and totally unexpected.
Option as +ESC:
Vim prints ^[a which means "Escape character followed by the character a".
From these tests it appears that Vim will never ever receive <M-> without some hypothetical black magic.
If you stick with "Option as +ESC", it seems that you will have to change all your custom <M-something> mappings to <Esc>something. This may work but it will make writing any kind of prose in any non-english language a pain.
What I do: I leave the Option key as it is so that I can type characters like œ…«» easily and I use <Leader> (mapped to ,, see :help mapleader) for all my custom mappings.
Some people here like to reserve it for plugins and advocate a somewhat simpler and potentially safer approach.
inoremap <leader>, <C-x><C-o> "my way (copied elsewhere)
inoremap ,, <C-x><C-o> "another way

I left my option key to act as Normal and discovered that Vim saw them as <T- bindings. So, for example, I have this mapping setup in my .vimrc to move to the end of a word when in Insert mode:
noremap! <T-Right> <C-o><Right>;

Related

Ctrl+6 not an editor command in Vim (switch language in the .vimrc)

I want VIM to start with two keymappings by default (Greek and Latin) but the default to be latin for coding. I have managed to add Greek in the .vimrc with :set keymap=greek, however that leaves Greek as the default language. Within VIM, I can change into latin in insert mode with Ctrl+6 but I want that to happen automatically when vim is fired up so latin will be the default keymap. The problem is Ctrl+6 (or Ctrl+^) is not an editor command but a key binding and as a result I cannot put it in the .vimrc. Any ideas on how to go around this issue?
tl;dr I want Greek keymap to be loaded automatically in vim but not be the default keymap.
This is not exactly the solution you're asking for, but a close one that works for me.
I have two imap commands in my .vimrc to switch between my French and English keyboards:
imap ^f <ESC>:set keymap=canfr<CR>a
imap ^^ <ESC>:set keymap=<CR>a
In insert mode, if I enter ^f it switches to French, and if I enter ^^ it switches back to the default.
You can also add the same in command mode:
map ^f :set keymap=canfr<CR>
map ^^ :set keymap=<CR>
In your case, you would have to replace my :set keymap command by what you need to activate your respective keyboards, and maybe also replace my ^f and ^^ by your preferred char, Ctrl-6 (not sure how to code it, I leave that to you) and a second character of your choice to pick which keyboard.
PS: to create the ctrl-6 rule, I would hit Ctrl-V followed by Ctrl-6 on my keyboard. On one of the too many machines I use, the result looks like this, where I added e for English, and f for French, but it will most likely be different on yours:
map <ESC>[54;5ue :set keymap=<CR>
map <ESC>[54;5uf :set keymap=canfr<CR>
imap <ESC>[54;5ue <ESC>:set keymap=<CR>a
imap <ESC>[54;5uf <ESC>:set keymap=canfr<CR>a
I've also replaced the literal escape character that technique produced, which visually looked liked ^[ but was actually just one character, by <ESC>, so this can be cut-and-pastable.
PPS: what Ctrl-6 will look like when you hit Ctrl-V Ctrl-6 is highly system dependent. What I showed above was from a ubuntu session in a cygwin-mintty terminal from a Windows machine. In gvim for Windows, it's a single character that displays as ^^, in vi inside Git Bash, it's a single character that displays as ^?. The encoding of Ctrl-N varies wildly, for reasons I don't fully understand.
PPPS: for all this to work for you, you'll probably have to remove your existing key binding for Ctrl-6.
I don't remember where I got this from, but I get the behaviour you want with this:
set keymap=greek_utf-8
set iminsert=0
set imsearch=-1
Latin is the default and you switch between Greek and Latin with ctrl+6 in insert mode.

Mapping TO ctrl+[something] doesn't work in gvim

I am using GVim 7.4 and I would like to do some really simple mapping to use CtrlP fuzzy matching of tags when a key combination is used.
I tried 2 approaches and they all seem to fail when vim calls Control + [x] combination. While I do understand that there are restrictions when it comes to mapping Ctrl+[x] codes, I haven't found any information on why ctrl mapping wouldn't work.
noremap \t :CtrlPTag<CR><C-\>w
This one enters CtrlP tag mode but then it doesn't enter word from under the cursor.
noremap \t <C-p><C-\>w
Here we don't even get to CtrlP window (I even omit going into tag mode here for simplicity).
As far as I understand (I'm no CtrlP user), the plugin is triggered via some command / key combination, and then presents interactive selection and filtering. It even has different "source" modes.
Now, this is a pretty heavy integration into Vim, probably using scratch buffers and its own input loop. That's why you cannot simply append keys to the mapping and get them interpreted by the plugin "as typed".
Typically, these plugins offer mode selection and so on via (optional) command arguments. Check the plugin's help, and if you cannot get the plugin into the state you need, best contact the plugin's author and ask for such enhancement.

Mapping Alt-j and Alt-k in vim [duplicate]

I'm running Vim on a gnome terminal. But the alt key mappings are not working. For example:
:imap <A-i> <Esc>
It works fine in GVim. But when I run the same command with Vim in the gnome terminal it does nothing.
I'm using Windows 7, The problem is with the terminal, right?
The problem
There are two ways for a terminal emulator to send an Alt key (usually called a Meta key as actual terminals didn't have Alt). It can either send 8 bit characters and set the high bit when Alt is used, or it can use escape sequences, sending Alt-a as <Esc>a. Vim expects to see the 8 bit encoding rather than the escape sequence.
Some terminal emulators such as xterm can be set to use either mode, but Gnome terminal doesn't offer any such setting. To be honest in these days of Unicode editing, the 8-bit encoding is not such a good idea anyway. But escape sequences are not problem free either; they offer no way of distinguishing between <Esc>j meaning Alt-j vs pressing Esc followed by j.
In earlier terminal use, typing Escj was another way to send a Meta on a keyboard without a Meta key, but this doesn't fit well with vi's use of Esc to leave insert mode.
The solution
It is possible to work around this by configuring vim to map the escape sequences to their Alt combinations.
Add this to your .vimrc:
let c='a'
while c <= 'z'
exec "set <A-".c.">=\e".c
exec "imap \e".c." <A-".c.">"
let c = nr2char(1+char2nr(c))
endw
set timeout ttimeoutlen=50
Alt-letter will now be recognised by vi in a terminal as well as by gvim. The timeout settings are used to work around the ambiguity with escape sequences. Esc and j sent within 50ms will be mapped to <A-j>, greater than 50ms will count as separate keys. That should be enough time to distinguish between Meta encoding and hitting two keys.
If you don't like having timout set, which times out for other mapped key sequences (after a second by default), then you can use ttimeout instead. ttimeout applies only to key codes and not other mappings.
set ttimeout ttimeoutlen=50
For Gnome-terminal, use the following instead:
imap ^[i <Esc>
^[i should be typed by pressing Ctrl-v Alt-i
Attention: You need to yank and put in Vim when you want to copy it elsewhere. If you just copy the mapping in an editor like gedit, the mapping will probably be broken.
EDIT here is an example which makes Alt-k add an empty line above the cursor, and Alt-j add an empty line after the current line.
" Alt-j/k to add a blank line
if has('gui_running')
" the following two lines do not work in vim, but work in Gvim
nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
else
" these two work in vim
" shrtcut with alt key: press Ctrl-v then Alt-k
" ATTENTION: the following two lines should not be
" edited under other editors like gedit. ^[k and ^[j will be broken!
nnoremap ^[k :set paste<CR>m`O<Esc>``:set nopaste<CR>
nnoremap ^[j :set paste<CR>m`o<Esc>``:set nopaste<CR>
endif
Try
<m-i>
Or, if typing alti inserts a character (like in my case, it inserts a carret: ˆ) just map to that character:
:inoremap ˆ <esc>
Be careful, because this one wouldn't work (at least in my system, MacOS 10.6). The caret waits for a letter, because it's not exactly a caret, it is a circumflex.
It may be that the shortcuts are actually from the Gnome Desktop. Try looking at the Gnome Keyboard Shortcuts tool (System menu, Preferences, Keyboard Shortcuts), which lets you view and modify the shortcuts defined on Gnome Desktop. If the key combination is assigned to a function on Gnome Desktop, then remove it and then that key combo should filter down to Vim properly.
Or you may be right that it is a problem of the terminal. Not all terminals support all key combos. Your problem may be the one described in the Vim help docs at :h map-alt-keys. The docs provide a workaround, but not a very good one.
The same thing happens to me. I searched on Google with "gnome terminal alt key", and found that someone asked almost the same question: "How to disable the alt-hotkey behavior on gnome terminal?" in the first link found. (The second link is just this question)
So, maybe you can try that:
Edit > Keyboard Shortcuts, and uncheck "Enable menu access keys"
Take a look at section 1.10 of http://vimdoc.sourceforge.net/htmldoc/map.html. It seems to indicate that gnome-terminal automatically escapes the Alt modifier, so that it doesn't switch the byte sent in the way that Vim is expecting. The document seems to indicate that there isn't really a way around this except for using a different terminal (such as xterm).
This is certainly frustrating because so far as I can tell Linux machines are also incapable of using the D (Mac's Command or Linux's Super) bindings, so at least as far as the terminal goes, we are limited to Shift and Ctrl modifiers, which is frustrating if we want to ensure that we can use all the commands we use in Gvim on terminal Vim (at least without switching terminals, towards which I'm perhaps overly stubborn - gnome-terminal is just so much prettier). I've been looking for a way around this but have been unable to find anything.

How can I see what keys vim thinks I'm hitting?

Is there any way to get a xev-like mode where I can hit keys and key combos and vim will print out what keys or characters it thinks I'm pressing?
Specific related problem: I have key bindings which work in MacVim and GVim but they don't work in a terminal-vim, which I use on Linux over SSH inside a screen. I've come to the conclusion that the reason is because vim thinks the keys I'm pressing are different from how MacVim interprets them.
In my .vimrc:
map <M-,> :split<CR> " Horizontal split
map <M-.> :vsplit<CR> " Vertical split
map <M-/> :close<CR>
In my vim's :map (MacVim shows the same):
¯ :close<CR><Space>
® :vsplit<CR> " Vertical split
¬ :split<CR> " Horizontal split
It works in MacVim and GVim, but doesn't work in any terminal-based vim. I've tried this in multiple terminals (OSX Terminal and Term2, KDE Terminal, Gnome Terminal, etc.). I've also witnessed this with other modifiers and key combos. It seems like vim is capturing the keystrokes, but it's interpreting them as something other than <M-,> for example.
I'd love to have a way to find out what vim thinks I'm pressing so I can write mappings accordingly.
i to switch to insert mode.
<C-v>.
<key> or combo.
Vim prints the "raw" value of the key/combo you have pressed.
Here (Gnome terminal on Ubuntu 11.04), typing i<C-v>, followed by <Alt>, followed by , prints something that looks like ^[, which means "Escape,". The Alt key (which I believe is what you want to use for <Meta>) is neither recognised as <Meta> nor as <Alt>.
The immediate conclusion is that CLI Vim doesn't like <M-> (and many terminal emulators don't deal very well with it anyway). Some terminal emulators allow you to map the <Alt> key to Meta but it's not a perfect cross-platform solution (I use <Alt> a lot on Mac OS X to input special characters).
On this machine,
nnoremap ^[, :split<CR> " ^[ is <C-v><Esc>
does exactly what I think you want, though.
If you absolutely want to continue using <M-> shortcuts both in the GUI and in the CLI you'll need to maintain two separate sets of shortcuts. If you have dozens of custom mappings in your .vimrc, it will quickly become a little hard to manage.
That's why I think you should use mappings that work everywhere like:
:nnoremap <leader>, :split<CR>
:nnoremap <leader>. :vsplit<CR>
:nnoremap <leader>/ :close<CR>
The default <leader> key is \ but I have set it to ,.
Note that you don't need to use <leader>key, simply mapping ,,, ,. and ,/ works, too.
See :help <leader>.

Alt key shortcuts not working on gnome terminal with Vim

I'm running Vim on a gnome terminal. But the alt key mappings are not working. For example:
:imap <A-i> <Esc>
It works fine in GVim. But when I run the same command with Vim in the gnome terminal it does nothing.
I'm using Windows 7, The problem is with the terminal, right?
The problem
There are two ways for a terminal emulator to send an Alt key (usually called a Meta key as actual terminals didn't have Alt). It can either send 8 bit characters and set the high bit when Alt is used, or it can use escape sequences, sending Alt-a as <Esc>a. Vim expects to see the 8 bit encoding rather than the escape sequence.
Some terminal emulators such as xterm can be set to use either mode, but Gnome terminal doesn't offer any such setting. To be honest in these days of Unicode editing, the 8-bit encoding is not such a good idea anyway. But escape sequences are not problem free either; they offer no way of distinguishing between <Esc>j meaning Alt-j vs pressing Esc followed by j.
In earlier terminal use, typing Escj was another way to send a Meta on a keyboard without a Meta key, but this doesn't fit well with vi's use of Esc to leave insert mode.
The solution
It is possible to work around this by configuring vim to map the escape sequences to their Alt combinations.
Add this to your .vimrc:
let c='a'
while c <= 'z'
exec "set <A-".c.">=\e".c
exec "imap \e".c." <A-".c.">"
let c = nr2char(1+char2nr(c))
endw
set timeout ttimeoutlen=50
Alt-letter will now be recognised by vi in a terminal as well as by gvim. The timeout settings are used to work around the ambiguity with escape sequences. Esc and j sent within 50ms will be mapped to <A-j>, greater than 50ms will count as separate keys. That should be enough time to distinguish between Meta encoding and hitting two keys.
If you don't like having timout set, which times out for other mapped key sequences (after a second by default), then you can use ttimeout instead. ttimeout applies only to key codes and not other mappings.
set ttimeout ttimeoutlen=50
For Gnome-terminal, use the following instead:
imap ^[i <Esc>
^[i should be typed by pressing Ctrl-v Alt-i
Attention: You need to yank and put in Vim when you want to copy it elsewhere. If you just copy the mapping in an editor like gedit, the mapping will probably be broken.
EDIT here is an example which makes Alt-k add an empty line above the cursor, and Alt-j add an empty line after the current line.
" Alt-j/k to add a blank line
if has('gui_running')
" the following two lines do not work in vim, but work in Gvim
nnoremap <silent><A-j> :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap <silent><A-k> :set paste<CR>m`O<Esc>``:set nopaste<CR>
else
" these two work in vim
" shrtcut with alt key: press Ctrl-v then Alt-k
" ATTENTION: the following two lines should not be
" edited under other editors like gedit. ^[k and ^[j will be broken!
nnoremap ^[k :set paste<CR>m`O<Esc>``:set nopaste<CR>
nnoremap ^[j :set paste<CR>m`o<Esc>``:set nopaste<CR>
endif
Try
<m-i>
Or, if typing alti inserts a character (like in my case, it inserts a carret: ˆ) just map to that character:
:inoremap ˆ <esc>
Be careful, because this one wouldn't work (at least in my system, MacOS 10.6). The caret waits for a letter, because it's not exactly a caret, it is a circumflex.
It may be that the shortcuts are actually from the Gnome Desktop. Try looking at the Gnome Keyboard Shortcuts tool (System menu, Preferences, Keyboard Shortcuts), which lets you view and modify the shortcuts defined on Gnome Desktop. If the key combination is assigned to a function on Gnome Desktop, then remove it and then that key combo should filter down to Vim properly.
Or you may be right that it is a problem of the terminal. Not all terminals support all key combos. Your problem may be the one described in the Vim help docs at :h map-alt-keys. The docs provide a workaround, but not a very good one.
The same thing happens to me. I searched on Google with "gnome terminal alt key", and found that someone asked almost the same question: "How to disable the alt-hotkey behavior on gnome terminal?" in the first link found. (The second link is just this question)
So, maybe you can try that:
Edit > Keyboard Shortcuts, and uncheck "Enable menu access keys"
Take a look at section 1.10 of http://vimdoc.sourceforge.net/htmldoc/map.html. It seems to indicate that gnome-terminal automatically escapes the Alt modifier, so that it doesn't switch the byte sent in the way that Vim is expecting. The document seems to indicate that there isn't really a way around this except for using a different terminal (such as xterm).
This is certainly frustrating because so far as I can tell Linux machines are also incapable of using the D (Mac's Command or Linux's Super) bindings, so at least as far as the terminal goes, we are limited to Shift and Ctrl modifiers, which is frustrating if we want to ensure that we can use all the commands we use in Gvim on terminal Vim (at least without switching terminals, towards which I'm perhaps overly stubborn - gnome-terminal is just so much prettier). I've been looking for a way around this but have been unable to find anything.

Resources