Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim key map not registering

I am trying to map Alt-r in Vim to save then execute the current file, and Alt-R to save then execute, but allow for extra parameters to be passed to the script.

In my vimrc I have the following (key mappings at the end of the file):

" Basic setup
set modeline
syntax on
filetype plugin indent on

" Load pathoen
execute pathogen#infect()

" Solarized colourscheme
if has('gui_running')
    set background=light
else
    set background=dark
endif
colorscheme solarized

" Map Alt-r to run current file as if it were a script
nmap <M-r> :w<CR>:!./%<CR>
nmap <M-R> :w<CR>:!./%<Space>

" Allow Alt-<CursorKey> to move around windows
nmap <M-Up> :wincmd k<CR>
nmap <M-Down> :wincmd j<CR>
nmap <M-Left> :wincmd h<CR>
nmap <M-Right> :wincmd l<CR>

The key mappings for window movement work fine, but Alt-r doesn't seem to do anything while Alt-R puts Vim into replace mode. I checked with :help M^r and :help M^R but they didn't list any mappings, nor does :map list those keys as taken. I have also already disabled Alt key mappings in the Gnome terminal.

Any suggestions??

like image 256
Tim Jones Avatar asked Nov 20 '25 16:11

Tim Jones


1 Answers

This could be a problem with your terminal emulator which may encode the alt key different from what vim expects.

Ctrl+v causes vim (and most shells) to output the next character or key combination verbatim (literally). This way, you can insert the keycode produced when you press Alt+r directly into your configuration file by adding

set <M-r>=^[r
set <M-R>=^[R

where the escaped characters like ^[x have to be entered by pressing Ctrl+v and then Alt+x and may produce a different result on different terminal emulators.

Notice that there are no spaces around the equal signs. Vim would interpret additional spaces as part of the assignment which would make the directive misbehave.

like image 82
XZS Avatar answered Nov 22 '25 23:11

XZS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!