Can Windows key be mapped in Vim? - vim

Using Windows key as Meta is very useful in Emacs, is there the way to do it in Vim?

You definitely can, even in terminal, although you have to use it as a meta key (I found no way to use it on its own).
Edit your .vimrc file with vim. Say you want to remap Win+q in normal mode to quit vim.
Simply add your mapping and, when trying to indicate your shortcut, press Ctrl+V, then Win+q.
This will add something similar to ^X#sq to your file (but do not type it directly, the ^X is a special character).
In the end your line should look like:
nnoremap ^X#sq :q<CR>
Save and quit, launch vim again, and that's it.
Note: Ctrl+v in insert mode inserts followoing key/combination of keys literally. For more info try :help i_CTRL-V in vim.

Sorry for answering so ancient question, but solution is really simple: it is impossible to use Win key in the terminal, but it is possible to use it with Gvim. Just pass it as modifier T. For example,
:nmap <T-F5> :q<cr>
will map Win+F5 to :q command. But it is usable only under *nix.

You can use AutoHotkey to map the windows key to a different key. Only activate the mapping when vim is active:
#IfWinActive ahk_class GVIM
RWin::Alt
LWin::Alt
#IfWinActive ; This puts subsequent remappings and hotkeys in effect for all windows.

None of these answers (including this one) is vim-specific, and the selected answer is Windows-specific. Here's one for *nix running X.
I map my left Win key to the Esc key. This won't work in virtual terminals, but it works in X.
Either:
(1) Append keysym Super_L = Escape to ~/.Xmodmap and execute xmodmap .Xmodmap.
|______(1a) ~same as echo "keysym Super_L = Escape" >> ~/.Xmodmap && xmodmap .Xmodmap .
(2) Execute xmodmap -e "keysym Super_L = Escape" .
If you want it to work in virtual terminals, see [0].
REFERENCES:
[0] http://www.mail-archive.com/screen-users#gnu.org/msg02859.html
[1] http://www.paganini.net/index.cgi/linux/nocaps.html
[2] http://ubuntuforums.org/archive/index.php/t-975229.html

Not quite sure, but the Ctrl+Esc key combo is a windows only key mapping. It won't help with vim

Related

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 to remap <C-;>

I want to use the following code to quickly go from insert mode -> command mode. The problem is it just doesnt seem to be registering my keys. I've tried with control (nothing happens) and i've also tried with command (D) and it just says spellcheck not activated.
" Quick command mode from insert
imap <C-;> <esc>:
How do i go about doing this? is there an easier vim way that I'm not aware of?
Certain Ctrl chords can't be mapped, including Ctrl-;.
This is mentioned in this FAQ, see also the Vim FAQ:
20.4. I am not able to create a mapping for the <xxx> key. What is wrong?
First make sure that the key is passed to Vim. In insert mode, press Ctrl-V
followed by the desired key. You should see the keycode corresponding to
the key. If you do see the keycode, then you can create a mapping for the
key using the following command:
:map <C-V><xxx> <your_command_to_be_mapped>
For more information, read
:help map-keys-fails
:help :map-special-keys
:help key-codes
The tip about trying to print the character using Ctrl-V is good to remember if you run into this problem with another key combo.
I tried it, however it doesn't seem to work, also as pb2q said, it just can't be mapped. But there are other ways to escape using a Ctrl key combination.
For example, you can also escape insert mode with the following key presses:
Ctrl-[
Ctrl-c
On OS X (I think Lion and above), you are able to map alt - ; using this method, alt - ; on OS X outputs ç, which you can map.
But at a MacBook, I prefer to use PCKeyboardHack to map caps lock to esc. Or at Windows, use a tool I've created myself for that or even Ctrl2Cap.
EDIT
oh sorry, I thought you wanted to switch to normal mode, that's why I talked about caps lock mappings.

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.

How to map Menu key ("Application key") to Escape key in vim?

I think that using Menu key to quit vim's insert mode would be a great thing. It would be also nice to use Super key for that, but I'm not sure if it possible since Super key is a modifier.
Anyway, I couldn't find anything related to this. Looking for your help and thanks in advance!
I don't think there's any way you can configure Vim to pay attention to the Menu key as such,
but depending on your system there are various ways to turn the Menu key into an Escape key.
If you're using X11 on Linux:
The command xmodmap -e 'keycode 135 = Escape' will turn your Menu key into an Escape key for the current session, but is not permanent. To make it permanent under Gnome, you might try adding it under System → Preferences → Startup Applications.
The xkeycaps program will give you a GUI for similar remappings.
For more information:
http://46dogs.blogspot.com/2008/05/remap-keys-in-ubuntu-804-hardy-heron.html
http://ubuntuforums.org/archive/index.php/t-106209.html
I haven't found a way to map it in gVim yet, but I was able to successfully map the Menu key in a urxvt+screen+vim stack by the following method:
In a terminal, type Ctrl+v and press Menu. This is mapped to the quoted-insert function in Zsh and in Readline-based tools like Bash.
It will generate an escape sequence like ^[[29~ at the prompt. (The initial ^[ must be translated to <Esc> for use in .vimrc.)
Open up .vimrc and add a line like this:
imap <Esc>[29~ <Esc>
(or imap <Esc>[29~ <Esc><Esc> if you don't want it to wait for further input like the Escape key does.)
Note that not all keys return something usable from Ctrl+v. This is a limitation of terminal emulators and can be remedied by remapping the key. You may be able to do that at the level of the terminal emulator rather than for all X apps.
For example, for urxvt, I had to add the following lines to ~/.Xresources and run xrdb -merge ~/.Xresources to apply them:
! Unbreak zsh keys in URxvt
URxvt*keysym.Home: \033[1~
URxvt*keysym.End: \033[4~
(\033 is <Esc> in ~/.Xresources syntax.)
Just try using ctrl+[ instead of binding another key. This combination is a standard one in vim, btw.
This is even easier when having rebound capslock into an additional ctrl.

Resources