3

I've found few Stack Overflow questions talking about this, but they are all regarding only the :nmap or :noremap commands. I want a command, not just a keybinding. Is there any way to accomplish this?

Use-case:

When I run :make, I doesn't saves automatically. So I'd like to combine :make and :w. I'd like to create a command :Compile/:C or :Wmake to achieve this.

1

3 Answers 3

6

The general information about concatenating Ex command via | can be found at :help cmdline-lines.

You can apply this for interactive commands, in mappings, and in custom commands as well.

Note that you only need to use the special <bar> in mappings (to avoid to prematurely conclude the mapping definition and execute the remainder immediately, a frequent beginner's mistake: :nnoremap <F1> :write | echo "This causes an error during Vim startup!"<CR>). For custom commands, you can just write |, but keep in mind which commands see this as their argument themselves.

:help line-continuation will help with overly long command definitions. Moving multiple commands into a separate :help :function can help, too (but note that this subtly changes the error handling).

arguments

If you want to pass custom command-line arguments, you can add -nargs=* to your :command definition and then specify the insertion point on the right-hand side via <args>. For example, to allow commands to your :write command, you could use

:command -nargs=* C w <args> | silent make | redraw!
Sign up to request clarification or add additional context in comments.

Comments

4

You can combine commands with |, see help for :bar:

command! C update | silent make | redraw!

However, there is a cleaner way to achieve what you want. Just enable the 'autowrite' option to automatically write modified files before a :make:

                                 'autowrite' 'aw' 'noautowrite' 'noaw'
'autowrite' 'aw'        boolean (default off)
                        global
        Write the contents of the file, if it has been modified, on each
        :next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!,
        :make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I,
        '{A-Z0-9}, or `{A-Z0-9} command takes one to another file.
        Note that for some commands the 'autowrite' option is not used, see
        'autowriteall' for that.

This option is mentioned in the help for :make.

Comments

2

I have found a solution after a bit of trial and error.

Solution for my usecase

command C w <bar> silent make <bar> redraw!

This is for compiling using make and it prints output only if there is nonzero output.

General solution

command COMMAND_NAME COMMAND_TO_RUN

Where COMMAND_TO_RUN can be constructed using more than one command using the following construct.

COMMAND_1_THAN_2 = COMMAND_1 <bar> COMMAND_2

You can use this multiple times and It is very similar to pipes in shell.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.