*usr_30.txt* Nvim
VIM USER MANUAL - by Bram Moolenaar
Editing programs
Vim has various commands that aid in writing computer programs. Compile a
program and directly jump to reported errors. Automatically set the indent
for many languages and format comments.
|30.1| Compiling
|30.2| Indenting C files
|30.3| Automatic indenting
|30.4| Other indenting
|30.5| Tabs and spaces
|30.6| Formatting comments
Next chapter: |usr_31.txt| Exploiting the GUI
Previous chapter: |usr_29.txt| Moving through programs
Table of contents: |usr_toc.txt|
==============================================================================
*30.1* Compiling
Vim has a set of so called "quickfix" commands. They enable you to compile a
program from within Vim and then go through the errors generated and fix them
(hopefully). You can then recompile and fix any new errors that are found
until finally your program compiles without any error.
The following command runs the program "make" (supplying it with any argument
you give) and captures the results: >
:make {arguments}
If errors were generated, they are captured and the editor positions you where
the first error occurred.
Take a look at an example ":make" session. (Typical :make sessions generate
far more errors and fewer stupid ones.) After typing ":make" the screen looks
like this:
:!make | &tee /tmp/vim215953.err ~
gcc -g -Wall -o prog main.c sub.c ~
main.c: In function 'main': ~
main.c:6: too many arguments to function 'do_sub' ~
main.c: At top level: ~
main.c:10: parse error before '}' ~
make: *** [prog] Error 1 ~
2 returned ~
"main.c" 11L, 111C ~
(3 of 6): too many arguments to function 'do_sub' ~
Press ENTER or type command to continue ~
From this you can see that you have errors in the file "main.c". When you
press <Enter>, Vim displays the file "main.c", with the cursor positioned on
line 6, the first line with an error. You did not need to specify the file or
the line number, Vim knew where to go by looking in the error messages.
>
+---------------------------------------------------+
|int main() |
|{ |
| int i=3; |
cursor -> | do_sub("foo"); |
| ++i; |
| return (0); |
|} |
|} |
| ~ |
|(3 of 12): too many arguments to function 'do_sub' |
+---------------------------------------------------+
<
The following command goes to where the next error occurs: >
:cnext
Vim jumps to line 10, the last line in the file, where there is an extra '}'.
When there is not enough room, Vim will shorten the error message. To see
the whole message use: >
:cc
You can get an overview of all the error messages with the ":clist" command.
The output looks like this: >
:clist
< 3 main.c: 6:too many arguments to function 'do_sub' ~
5 main.c: 10:parse error before '}' ~
Only the lines where Vim recognized a file name and line number are listed
here. It assumes those are the interesting lines and the rest is just boring
messages. However, sometimes unrecognized lines do contain something you want
to see. Output from the linker, for example, about an undefined function.
To see all the messages add a "!" to the command: >
:clist!
< 1 gcc -g -Wall -o prog main.c sub.c ~
2 main.c: In function 'main': ~
3 main.c:6: too many arguments to function 'do_sub' ~
4 main.c: At top level: ~
5 main.c:10: parse error before '}' ~
6 make: *** [prog] Error 1 ~
Vim will highlight the current error. To go back to the previous error, use:
>
:cprevious
Other commands to move around in the error list:
:cfirst to first error
:clast to last error
:cc 3 to error nr 3
USING ANOTHER COMPILER
The name of the program to run when the ":make" command is executed is defined
by the 'makeprg' option. Usually this is set to "make", but Visual C++ users
should set this to "nmake" by executing the following command: >
:set makeprg=nmake
You can also include arguments in