I'm throwing this up here after scouring through tons of SO questions and answers. This solution worked for me with iTerm2 + Mojave.
- In iTerm2, go to Preferences -> Profile -> Keys. In the bottom right, change your left (or right) option key to fire off an Esc+ sequence instead of Meta/Normal.
- Put the following code in your .vimrc:
execute "set <A-j>=\ej"
execute "set <A-k>=\ek"
nnoremap <A-j> :m .+1<
nnoremap <A-k> :m .-2<
inoremap <A-j> <Esc>:m .+1<CR>==
inoremap <A-k> <Esc>:m .-2<CR>==
vnoremap <A-j> :m '>+1<CR>gv=
vnoremap <A-k> :m '<-2<CR>gv=gv
What is this doing?
Admittedly, my understanding is not great. When you send an ALT+j
command to Terminal, iTerm will receive it as ^[j
which is an Escape sequence. When mapping it to Vim, ^[
ends up as \e
. If you want to double check what your ALT+j
(or ALT+k
) is firing as, type sed -n l
in a Terminal window and then hit ALT+j
– you should see this output: ^[j
The execute set
commands are remapping <A-j>
and <A-k>
mappings for Vim to fire off ^[j
/^[k
respectively to match what's being fired by iTerm.
This fixed it for me and I can now move line blocks like a pro. Hopefully it helps some people (sorry for my lackluster explanation–still learning the Vim ecosystem).