If suppose my source code file name is "foo.c". While editing and debugging i always execute this command:-

:!gcc -g foo.c -o foo;gdb foo

Can I add a custom command to Vim such that If i type ":debug" then the above command executes? How do I achieve that?

4 Answers 4


Yes. Vim documentation, section 40.2, Command-line mappings:

The Vim editor enables you to define your own commands. You execute these commands just like any other Command-line mode command. To define a command, use the ":command" command, as follows:

:command DeleteFirst 1delete

Now when you execute the command ":DeleteFirst" Vim executes ":1delete", which deletes the first line.

Note: User-defined commands must start with a capital letter. You cannot use ":X", ":Next" and ":Print". The underscore cannot be used! You can use digits, but this is discouraged.

Put that line in your ~/.vimrc (minus the inital : of course) and it will be defined every time you start vim. Also, use %:t to make reference to the file being edited (writing ! gcc %:t gets replaced by ! gcc foo.c).

If you'd only like it to be defined for the one file, or for certain files, you want an autocommand.

  • 6
    if you are replacing an existing command, you need to use command!.
    – phyatt
    Commented Jan 12, 2017 at 20:00
  • If you want the command to take arguments this has to be specified. The document at the top of this answer addresses that. It isn't on by default.
    – flickerfly
    Commented Mar 3, 2020 at 15:29
  • command! is also useful when defining an autocommand (i.e. a file type specific command) otherwise error E174: Command already exists: add ! to replace it will appear each time you press ctrl-o to jump back to a previous position or when viewing a git diff for example. Commented Jun 16, 2020 at 10:13
  • How can I define a command :DoThing that has different behaviour for :DoThing and for :DoThing! with the exclamation mark?
    – minseong
    Commented Feb 16 at 15:42

Vim already has support for Makefiles (:make). If you create one for your source, you can specify in it what you want done. Also, through ctags, Vim will be able to iterate through any errors found during compilation.


Add a user defined vi editor command:

This will make it so that when the user types :Legend in normal mode, a block of text is added under the cursor.

Step 1: Put this line into a file named ~/legend_header.txt`

this text will be added

Step 2: Put this command into your ~/.vimrc file (if the file doesn't exist, create it):

"The following command puts the contents of ~/legend_header.txt 
"under the cursor when the user types ":Legend" in normal mode.
command Legend :r ~/legend_header.txt

Step 3: Restart vi for the changes to take effect. You're done.

Test it

Step 1: Open a new file in vi. Be in the default "normal mode" in vi. Type :Legend

Step 2: The contents of ~/legend_header.txt should be added where the cursor is.

  • You don't need to restart vim as stated in step 3. You can use the command :source ~/.vimrc to reload.
    – flickerfly
    Commented Mar 3, 2020 at 15:24

I use this command in my .vimrc to compile and execute the c code:

:command Gcc !set $1 `echo "%" | sed 's/\.c//g'` ;gcc -o $1 "%" ; chmod o+x $1; ./$1

, but for your needs you could put:

:command Gdb !set $1 `echo "%" | sed 's/\.c//g'` ;gcc -o $1 "%" ; gdb $1

You can be sure the name of the executable file will be the source name without .c extension.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .