Show menu when pressing the alt-key. Or: how to map the <alt> key?

500 views
Skip to first unread message

Tom Link

unread,
Nov 11, 2008, 3:43:58 AM11/11/08
to vim_use
Hi,

I'd like to show the menu bar only when the alt key is pressed.
Alternatively: I'd like to press the alt key alone to toggle the
display of the menu bar.

Creating a function to toggle the display of the menu bar is trivial.
But how could I map the alt key? Is there a way to do this or do I
have to map this function to some regular key? I use gvim for windows.

Regards,
Thomas.

fritzophrenic

unread,
Nov 11, 2008, 8:57:25 AM11/11/08
to vim_use
I'm about 99.5% certain that Vim is not capable of recognizing the alt
key all by itself. You'd need to map an alt key combination (like <A-
m> for holding ALT and pressing the lowercase m key) or an alternate
key (suggestion: the <F2> key and up are all free, the <F1> key by
default does the same as :help<CR>).

Tom Link

unread,
Nov 11, 2008, 9:06:51 AM11/11/08
to vim_use
> You'd need to map an alt key combination (like <A-
> m> for holding ALT and pressing the lowercase m key)

Yes, that's what I do now. Anyway, it just occured to me that, when
the menu bar isn't displayed, vim handles the alt-accelerator keys
that would otherwise open a window. So one should be able to do
something like this:

map <a-d> :call TToggleMenu()\|call feedkeys("\<a-d>", "n")<cr>

But this doesn't work because the keys sent by feedkeys() seem to
bypass the functions responsible for the menus. So how do I open a
menu from within vimscript? Any ideas?

:popup works to some extent but opens the menu as popupmenu where the
cursor is -- as if torn of from the menu bar.

Matt Wozniski

unread,
Nov 11, 2008, 9:09:54 AM11/11/08
to vim...@googlegroups.com
On Tue, Nov 11, 2008 at 9:06 AM, Tom Link wrote:
>
>> You'd need to map an alt key combination (like <A-
>> m> for holding ALT and pressing the lowercase m key)
>
> map <a-d> :call TToggleMenu()\|call feedkeys("\<a-d>", "n")<cr>
>
> But this doesn't work because the keys sent by feedkeys() seem to
> bypass the functions responsible for the menus. So how do I open a
> menu from within vimscript? Any ideas?

Maybe with :simalt?

~Matt

Tom Link

unread,
Nov 11, 2008, 10:18:37 AM11/11/08
to vim_use
> Maybe with :simalt?

Yes. Thanks. I forgot about that command.

Tony Mechelynck

unread,
Nov 12, 2008, 1:16:25 AM11/12/08
to vim...@googlegroups.com

You can't, or at least not by Vim means. Some OSes allows remapping of
keyboard keys (e.g. to switch Ctrl with CapsLock, or CapsLock with Esc)
but that would mean mapping the Alt key to something that Vim can detect
(F10 maybe, or F13 if that means anything), but I suppose it would
disable all Alt-something keychords.

You can trigger the Vim menu by means of the ":emenu" command, even in
Console Vim provided that you've sourced $VIMRUNTIME/menu.vim

Here's the relevant part of in my vimrc:

if 1
runtime! menu.vim
endif
runtime vimrc_example.vim
if has("wildmenu")
set wildmenu wildmode=longest:full,full
else
set wildmode=longest,list:longest,full
endif
set wildcharm=<C-T> " or any key not needed in Command-line mode
if has('menu')
" override Ctrl-Z (minimize) by a text-mode menu
map <C-Z> :emenu <C-T>
imap <C-Z> <C-O>:emenu <C-T>
endif

Best regards,
Tony.
--
Space is big. You just won't believe how vastly, hugely, mind-
bogglingly big it is. I mean, you may think it's a long way down the
road to the drug store, but that's just peanuts to space.
-- "The Hitchhiker's Guide to the Galaxy"

Tom Link

unread,
Nov 12, 2008, 11:35:24 AM11/12/08
to vim_use
> You can trigger the Vim menu by means of the ":emenu" command

This makes it even more useful when on the console. Thanks for the
tip.

I wrapped my solution up as a plugin in case somebody is interested in
something like this.

http://www.vim.org/scripts/script.php?script_id=2437

A. S. Budden

unread,
Nov 13, 2008, 3:33:23 AM11/13/08
to vim...@googlegroups.com
2008/11/12 Tom Link <mica...@gmail.com>:

>
>> You can trigger the Vim menu by means of the ":emenu" command
>
> This makes it even more useful when on the console. Thanks for the
> tip.
>
> I wrapped my solution up as a plugin in case somebody is interested in
> something like this.

Looks interesting... a few small comments.

If you press "Alt-F" and then "Alt", the menu remains until you start
doing something (I guess there's probably nothing you can do about
this though).

In the documentation

:67s/|&wildcharm|/'wildcharm'/
:86s/shemenu/shymenu/

Al

Tom Link

unread,
Nov 13, 2008, 3:55:19 AM11/13/08
to vim_use
> If you press "Alt-F" and then "Alt", the menu remains until you start
> doing something

Unfortunately, I cannot find an autocommand event that gets fired when
not selecting an item from the menu. The first event that gets
triggered in such a situation seems to be CursorMoved[I].
Reply all
Reply to author
Forward
0 new messages