How do I define a custom Ex command that forwards arguments with file completion?
Answer
:command! -nargs=* -complete=file W w <args>
Explanation
When you repeatedly type a long Ex command with filenames, define a user command that keeps the behavior but shortens the keystrokes. :command! -nargs=* -complete=file W w <args> creates a custom :W command that forwards all arguments to :w, including filename targets. This is practical for high-speed command-line workflows where you still want proper completion and argument handling.
How it works
:command!defines (or force-redefines) a user command-nargs=*allows zero or more arguments after:W-complete=fileenables filename completion when typing argumentsWis the new command namew <args>is the implementation: it runs write with forwarded arguments
Examples after defining it:
:W
:W backup.txt
:W %:h/new-name.txt
Example
Before:
You often type: :w /tmp/scratch-output.txt
After defining the command:
You can type: :W /tmp/scratch-output.txt
The behavior is the same, but shorter and still completion-friendly.
Tips
- Use uppercase names to avoid clashing with built-in lowercase commands
- Add
-barif you want the command to allow trailing|command chaining - Put custom commands in your config so they are available in every session