4

I created many menu commands in VIM.

. repeats the last command in normal mode.
@: repeats the last command from commandline

Is there a way to repeat the last command invoked from vim's menu?

Update:

example menu command:

an 98.80.00 &MyMenu.Test\ :call <SID>Test("%")<CR>

If I use this Menu Command created by myself, how can I repeat it again (repeat last used menu command)?
In above case it would be :call <SID>Test("%")<CR>
I can't find these commands in command line history.
@: and :<UP> doesn't work

Does anyone know where Vim saves function calls/menu commands actions?

Update2

Kent proposed to build a function around the above command:

an 98.80.00 &MyMenu.Test\ :call SubExe('call <SID>Test("%")')<CR>

 function! SubExe(argument)
  let g:lastcommand = a:argument
  exe g:lastcommand
 endfun

It seems to work, the disadvantage is that I have to change all current commands ;)

3
  • I always thought it would be great if I can repeat a group of commands, rather than just the last command. The answer to this is obviously using macros (qa<set of commands>q then @a to repeat), but I was looking for something easier and faster. Can't you use macros?
    – Rafid
    Mar 17, 2011 at 8:27
  • Yes, but if I'm not mistaken you have to create a macro for every menu command that you need to repeat.
    – Reman
    Mar 17, 2011 at 10:08
  • Yes, that's write, but I suppose you wouldn't need that frequently, yeah? Otherwise, just map a key to the menu command then keep pressing that key.
    – Rafid
    Mar 21, 2011 at 10:06

2 Answers 2

3
+50

If there is no built-in support, you have to build one on your own, if it is so important to you. Basic idea is:

You create a function, like ExecMenuCmd(cmd) the argument is the command, like wq, in the function, you save the command into a variable, then execute it.

Then you can create mapping to "repeat" last menu cmd by reading the variable and execute.

When you create menu items, you do something like:

:menu File.SaveAndExit :call ExecMenuCmd('wq')

If you like, you could maintain a stack to store the commands triggered by menu, to implement more features.

17
  • Hi Kent, Tnx for replying. This would mean that I have to change all commands I created, isn't it? In above example the command would be :call ExecMenuCmd(':call <SID>Test("%")<CR>') isn't it? And in this function I execute :call <SID>Test("%")<CR> is that correct? Btw Is it not possible to capture also the command in the invoked function (Test()) and put it in a variable?
    – Reman
    Aug 11, 2014 at 13:39
  • you can exec a :call F("arg") like exec 'call F("arg")'. yes you need change the menu creation lines, if the feature is important enough for you. @Remonn
    – Kent
    Aug 11, 2014 at 13:49
  • 1
    @Remonn you can make a list, to push every command in. when you want to repeat, pop it out. it give you the possibility to track the command history. but it is further than your requirement.
    – Kent
    Aug 11, 2014 at 16:41
  • 1
    @Remonn what I can think of, when you define your menu command, better not define normal things. E.g. your example could be done by wincmd Again, the function is a work round. You have to check different situations to do corresponding handling.
    – Kent
    Aug 17, 2014 at 17:15
  • 1
    @Remonn I don't know what do you want, if you want to do <C-W>T you could do with wincmd and your function: execute 'wincmd T' this does Move the current window to a new tab page.
    – Kent
    Aug 18, 2014 at 9:10
0

You could make a mapping in your .vimrc for entering the command mode, and then running the last command. Something like:

noremap <F8> :<Up><Cr>

That will repeat the last thing you ran from command mode whenever you press F8 in normal mode. You can change F8 to whatever you want.

2
  • @: will do the same , without any mapping
    – mb14
    Mar 28, 2011 at 13:21
  • 1
    @mb14, thank you. I regret that it still doesn't work with menu commands calling a function.
    – Reman
    Apr 7, 2011 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.