Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gVim makes alt+(letter) key produce an accented character instead of exiting insert mode and executing the command

Tags:

vim

In terminal mode, I frequently use things like alt-l to exit insert mode (while keeping the cursor in the same place). Essentially I am very used to this behavior as a method to exit insert mode and then subsequently execute the command that follows.

However, while using gVim, alt+(some character) produces an accented character. From my searches I have learned that this may be due to utf-8 encoding or something. I couldn't care less about accented characters, so I don't need utf-8 encoding. Is there some way to get the (M-l) to behave like l, the way it works in terminal mode?

EDIT: Following the suggestion to just remap to (character), I know have this in my .vimrc. It does the trick.

if has('gui_running')
  :set guioptions -=m
  :inoremap <M-l> <Esc>l
  :inoremap <M-j> <Esc>j
  :inoremap <M-k> <Esc>k
  :inoremap <M-h> <Esc>h
endif

The :set guioptions -=m is just to make the menu disappear so that pressing Alt-h does what I want it to do instead of bringing down the help menu.

like image 835
AetasAaM Avatar asked Oct 25 '25 02:10

AetasAaM


1 Answers

Your terminal most likely interprets Alt as Esc.

In CLI Vim, Alt+l is thus actually Esc+l: it exits from insert mode and move the cursor one character to the right.

Basically, you grew a habit around a Vim limitation.

In GUI Vim, Alt+l is really Alt+l: it inserts the special character normally associated with that key combination in your keyboard layout. On this Mac, I get ¬ and mapping something to <A-l> doesn't work at all.

Alt key handling is a common complaint but AFAIK there's no bulletproof way to have proper cross-platform and working <A-character> mappings.

You can get the desired behavior in GVim with this simple mapping (using the output of Alt+l on this machine, hit Alt+l to insert the correct glyph):

inoremap ¬ <Esc>l

But it doesn't sound like a good strategy.

I actually never tried to do Alt mappings in GVim on Linux or Windows so YMMV.

like image 116
romainl Avatar answered Oct 28 '25 04:10

romainl