Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> ==> a
<ALT+k> ==> o

and so on. Solution found here on StackOverflow.

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

nnoremap a :m .+1<CR>==
nnoremap o :m .-2<CR>==

inoremap a <Esc>:m .+1<CR>==gi
inoremap o <Esc>:m .-2<CR>==gi

vnoremap a :m '>+1<CR>gv=gv
vnoremap o :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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...