Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any mapping in vimrc with Alt isn't working

Tags:

vim

I have some mappings in my .vimrc with ALT. They are:

nnoremap <A-J> :m .+1<CR>==
nnoremap <A-K> :m .-2<CR>==

Expected behavior: These two mappings are supposed to move a line up and down. But they are not working. I am not even getting any error or warning. But when I use shift, they are working fine. Below are given the working mappings.

nnoremap <A-J> :m .+1<CR>==
nnoremap <A-K> :m .-2<CR>==

Why isn't ALT working?

like image 565
odbhut.shei.chhele Avatar asked Sep 13 '25 01:09

odbhut.shei.chhele


2 Answers

Firstly, add this to your zsh file

alias vim="stty stop '' -ixoff ; vim"

This is not necessary but it does get rid of vim mapping issues. Secondly, start by viewing the key code your terminal is sending to vim:

$ sed -n l

I am on Ubuntu, and Alt+j leads to

^[j

This basically prints out the combination of keycodes that is being sent to vim from your terminal. Note that the first part ^[ is the Escape key and not part of the key pressed.

Add this to your .vimrc

map <Esc>j <A-j>

Afterwards, you can use this to map to other functions:

nnoremap <A-j> :m .+1<CR>==

Source: https://vim.fandom.com/wiki/Mapping_fast_keycodes_in_terminal_Vim

like image 148
Raza Avatar answered Sep 14 '25 18:09

Raza


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

$ sed -n l

I am on Ubuntu, and Alt+j leads to

^[j

This basically prints out the combination of keycodes that is being sent to vim from your terminal.

If you are having difficulties with the <A+j> key combination, you can get what is sending to vim and then use that in the .vimrc.

as follows:

nnoremap ^[j :m .+1<CR>==

This will map <Alt + J> to moving a line up.

Note: Don't copy and paste nnoremap ^[j :m .+1<CR>== into your .vimrc. You will have to find what keycodes are being sent this in your terminal, and it could be very different from ^[j as each terminal might send different keycodes to vim. I am using Putty + Ubuntu running on a VM.

like image 40
alpha_989 Avatar answered Sep 14 '25 18:09

alpha_989