2

I want to map an action that repeats the last make command.

Ideally, such a mapping would work also in insert mode (i.e. it would automatically switch to command mode.)

Also, it would be nice, if such an action also automatically saved the buffers with unsaved changes in them.

Basically, this would automate steps like:

Ctrl+[
:w
:ma
Arrow-Up
Return

Such steps could be mapped to F9, for example.

Ok, saving all buffers with unsaved changes (e.g. when in split-window mode) probably complicates the action.

2 Answers 2

1

It's fairly easy to translate your keystrokes into a mapping:

nnoremap <F9> :wa<CR>:ma<Up><CR>
imap <F9> <Esc><F9>i

Here <CR> means carriage return. You can also use <Enter> but that's longer to type!

The imap jumps out of insert mode with <Esc>, invokes the normal-mode F9 mapping, then goes back into insert mode with i. We can't make it a "noremap" because we want it to expand the other F9 mapping.

1
  • I wasn't aware of :wa, thought writing all changed buffers would be more complicated! Jul 30, 2015 at 6:45
2

You could use Ingo Karkat's Redocommand plugin which allows to call the last command matching a pattern.

The idea would be to add the following function to your .vimrc:

function! LastMake()
    bufdo w
    Redocommand :make
endfunction

In this function bufdo w saves all unsaved buffers and the second line use Redocommand to call the last make function.

Then you create the folowing mappings (add these two lines to your .vimrc)

nnoremap <F9> :call LastMake()
inoremap <F9> <Esc>:call LastMake()

which will allow you to use F9 in normal and insert mode to call the function.

EDIT As @Jjaderberg pointed it out in comments, bufdo will change the current buffer which could be an issue if your make command does use an absolute path. So a good solution is to replace it by wall (for Write All) as @joeytwiddle suggested to get the same result without changing the current buffer. (Thanks to both of you for your comments) :

function! LastMake()
    wall 
    Redocommand :make
endfunction
3
  • :bufdo will make last processed buffer the current buffer. If the :make command that you are redoing refers to "current buffer" or "file of current buffer", or if making the last processed buffer the current buffer involves changes to 'pwd' or other pertinent options you may end up running a different command than intended.
    – jjaderberg
    Jul 29, 2015 at 16:18
  • Instead of bufdo w you could wall and avoid changing buffer. Jul 29, 2015 at 22:06
  • Or use this :BufDo which returns you back to your starting window. Jul 2, 2016 at 18:43

Your Answer

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

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