Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I map Alt key in Vim?

Tags:

vim

I tried to map to as adding the below line to .vimrc, but it doesn't work. I checked the .vimrc is loaded by Vim.

map <Alt-D> <C-D>

is there any error in this mapping?

like image 788
Thomson Avatar asked Sep 21 '11 14:09

Thomson


People also ask

How do I map a key in Vim?

Creating keymaps To map a sequence of keys to execute another sequence of keys, use the ':map' command. For example, the following command maps the <F2> key to display the current date and time. The ':map' command creates a key map that works in normal, visual, select and operator pending modes. The ':map!'

Does vim allow for custom key bindings?

Creating a set of key bindings that is familiar, eases the learning curve of any new editor, and the flexibility vim allows in this configuration makes it easy to not only leverage the power of vim, but also make it feel like a familiar old friend.

What is Ctrl in Vim?

vim file is used, then CTRL-V is mapped to paste text from the clipboard. In this case, you can use CTRL-Q or CTRL+SHIFT+V instead of CTRL-V to escape control characters. To create a map for the Ctrl-v key, you have to enter it four times: :imap ^V^V^V^V EscapeCharacter.

What's the Alt button on a Mac?

The PC-keyboard equivalent of Alt on a Mac is called the Option key, and you'll find the Option Key on your Mac if you go two keys to the left of the spacebar. However, the option key on a Mac keyboard is used in a different way than the alt key on a Windows PC.


1 Answers

To Mac users out there: for mapping ALT+hjkl, use instead the real character generated (find out which character using the combination while in INSERT mode), for example with my keyboard I get:

<ALT+j> ==> ª <ALT+k> ==> º 

and so on. Solution found here on StackOverflow.

I used this to move lines up and down with ALT+k\j, using this on my .vimrc:

nnoremap ª :m .+1<CR>== nnoremap º :m .-2<CR>==  inoremap ª <Esc>:m .+1<CR>==gi inoremap º <Esc>:m .-2<CR>==gi  vnoremap ª :m '>+1<CR>gv=gv vnoremap º :m '<-2<CR>gv=gv 

as explained here.

Hope it's useful, enjoy Vim :)

ADDENDUM BY Dylan_Larkin (2019): For this to work on a Mac, "Use Option as Meta Key" must be turned OFF in Terminal->Preferences->Keyboard

UPDATE 09/2021

I recently switched from a "British" keyboard to "ABC - Extended" and noticed this configuration doesn't work as expected. As an alternative, I mapped the <up> and <down> keys to do the same operation (which, I guess, also solves most of the complexity explained in other answers of this very question):

nnoremap <down> :m .+1<CR>== nnoremap <up> :m .-2<CR>==  inoremap <down> <Esc>:m .+1<CR>==gi inoremap <up> <Esc>:m .-2<CR>==gi  vnoremap <down> :m '>+1<CR>gv=gv vnoremap <up> :m '<-2<CR>gv=gv 

This is also a great way for beginners to rewire the habit of using the arrows and instead learn the much more efficient Vim motion way to move around the code. ;)

You can complete your transition mapping <left> and <right> to quickly move between tabs with:

nnoremap <left> gT nnoremap <right> gt 

Or whatever you fancy (even a brutal <NOP>, like I did at the beginning of my journey).

like image 92
Bruno Belotti Avatar answered Nov 16 '22 00:11

Bruno Belotti