2
votes

I'm trying to adapt to vim, again, I'm doing pretty well for now but unfortunately Gvim and Vim doesn't handle the same way the alt key.

In Vim, pressing ALT+ any other key) is the same to press (ESC + any other key). Even in bash's prompt using the vi mode (set -o vi).

If I use the same shortcuts in Gvim, estranges UTF-8 characters are printed.

How can I disable

2

2 Answers

5
votes

In Vim, pressing (ALT + any other key) is the same to press (ESC + any other key). Even in bash's prompt using the vi mode (set -o vi).

Vim doesn't do that, your terminal does -- which is why you see the same behavior in other programs in that terminal, like bash. Instead of removing behavior from gvim, you need to add behavior to gvim that matches the terminal behavior you expect.

Depending on your window manager, you may be able to map to do what you want:

# in .vimrc, or without guards in .gvimrc
if has("gui_running")
    map <m-j> (something)
endif

Use map, nmap, imap, ... depending on which modes you want.

2
votes

As mentioned by @fred-nurk the difference in key mappings between terminal based vim and gvim is based on the key mappings and capabilities of your GUI environment (window manager) and your terminal emulator(s).

I'm assuming also that your question is referring to 'insert mode' issues so I'm answering accordingly.

One way to map the gvim keys to behave more closely to vim would be remap each offending key and also disable menu keys. Honestly, this is pretty much all you probably need in your .vimrc somewhere (near the bottom[?]):

[edited to add menubar toggles]:

" vim.gtk/gvim: map alt+[hjkl] to normal terminal behaivior
if has("gui_running")
    " inoremap == 'ignore any other mappings'
    inoremap <M-h> <ESC>h
    inoremap <M-j> <ESC>j
    inoremap <M-k> <ESC>k
    inoremap <M-l> <ESC>l

    " uncomment to disable Alt+[menukey] menu keys (i.e. Alt+h for help)
    set winaltkeys=no " same as `:set wak=no`

    " uncomment to disable menubar
    set guioptions -=m

    " uncomment to disable icon menubar
    set guioptions -=T

    " macro to toggle window menu keys
    noremap ,wm :call ToggleWindowMenu()<CR>

    " function to toggle window menu keys
    function ToggleWindowMenu()
        if (&winaltkeys == 'yes')
            set winaltkeys=no   "turn off menu keys
            set guioptions -=m  "turn off menubar

            " uncomment to remove icon menubar
            "set guioptions -=T
        else
            set winaltkeys=yes  "turn on menu keys
            set guioptions +=m  "turn on menubar

            " uncomment to add icon menubar
            "set guioptions +=T
        endif
    endfunction
endif