Can I map Alt key in 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?


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).


:help key-notation describes what format needs to be used to map different keys. In the case of alt, you can use either <A- or <M-. So your mapping would be

map <M-d> <C-d>

I'd also recommend using the nore variant of :map (e.g., noremap) unless you explicitly want to allow the right-hand side to be re-evaluated for mappings.


I'm not sure is "possible" anymore. Please read the update below.

Yes, you can even in terminal vim, but there's no real catch all answer. You basically have to follow two steps:

  1. Make sure the <M-d> notation exists, and map exactly what your terminal inputs (^[ is the escape character):

    $ cat
    ^[d
    $
    
    " in your .vimrc
    execute "set <M-d>=\ed"
    " you have to use double quotes!
    
  2. Map something to your newly "created" combination:

    noremap <M-d> :echo "m-d works!"<cr>
    

Understanding how it works, you can expand this "trick" to other "strange" combinations, for instance, I'm using termite, and vim doesn't recognize <S-F1>, using cat I get ^[[1;2P. Then, in my vimrc I do: execute "set <S-F1>=\e[1;2P", and then I can map it to anything.

Note: I don't know why, but for some people using \<Esc> works instead of \e.


Update (february 2016)

Depending on the terminfo your terminal runs, maybe you could... in most terminals, "alt + h", for example, is mapped to ^[h, which is: "escape + h". So it could overwrite keys. I've just tried (again) and it seems to work, but I believe it's a very buggy and error prone implementation.

Nevertheless, for the brave enough, here's an experimental plugin:

  • https://github.com/vim-utils/vim-alt-mappings
  • https://github.com/drmikehenry/vim-fixkey

Map Alt Key in Vim on Mac OSx:

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

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

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

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

map <Esc>[1;9D 

Use:

map <A-D> <C-D>

See :help key-notation.