Nvim :help
pages, generated
from source
using the tree-sitter-vimdoc parser.
: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:
<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' | +---------------------------------------------------+
:cnextVim 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:
:ccYou can get an overview of all the error messages with the ":clist" command. The output looks like this:
:clist
:clist!
:cpreviousOther commands to move around in the error list:
:set makeprg=nmakeYou can also include arguments in this option. Special characters need to be escaped with a backslash. Example:
:set makeprg=nmake\ -f\ project.makYou can include special Vim keywords in the command specification. The % character expands to the name of the current file. So if you execute the command:
:set makeprg=make\ %:SWhen you are editing main.c, then ":make" executes the following command:
make main.cThis is not too useful, so you will refine the command a little and use the :r (root) modifier:
:set makeprg=make\ %:r:S.oNow the command executed is as follows:
make main.oMore about these modifiers here: filename-modifiers.
:colderThen use ":clist" and ":cc
{nr}
" to jump to the place with the warning.
To go forward to the next error list::cnewerVim remembers ten error lists.
:compiler msvcThis will find the Vim script for the "msvc" compiler and set the appropriate options. You can write your own compiler files. See write-compiler-plugin.
:cfile {filename}Jumping to errors will work like with the ":make" command.
30.2
Indenting C style text:set cindent shiftwidth=4With this option enabled, when you type something such as "if (x)", the next line will automatically be indented an additional level.
==This indents the current line. Like with all operators, there are three ways to use it. In Visual mode "=" indents the selected lines. A useful text object is "a{". This selects the current {} block. Thus, to re-indent the code block the cursor is in:
=a{If you have really badly indented code, you can re-indent the whole file with:
gg=GHowever, don't do this in files that have been carefully indented manually. The automatic indenting does a good job, but in some situations you might want to overrule it.
:set cinoptions+={2There are many of these items. See cinoptions-values.
30.3
Automatic indenting:filetype indent onActually, this does a lot more than switching on 'cindent' for C files. First of all, it enables detecting the type of a file. That's the same as what is used for syntax highlighting. When the filetype is known, Vim will search for an indent file for this type of file. The Vim distribution includes a number of these for various programming languages. This indent file will then prepare for automatic indenting specifically for this file.
:filetype indent offIf you don't like the indenting for one specific type of file, this is how you avoid it. Create a file with just this one line:
:let b:did_indent = 1Now you need to write this in a file with a specific name:
{directory}
/indent/{filetype}.vim{filetype}
is the name of the file type, such as "cpp" or "java". You can
see the exact name that Vim detected with this command::set filetypeIn this file the output is:
{filetype}
.
For the {directory}
part you need to use your runtime directory. Look at
the output of this command:set runtimepathNow use the first item, the name before the first comma. Thus if the output looks like this:
{directory}
. Then the resulting file name is:30.4
Other indentingCTRL-D
and
CTRL-T commands in Insert mode.
For example, you are typing a shell script that is supposed to look like
this::set autoindent shiftwidth=3You start by typing the first line,
<Enter>
and the start of the second line:CTRL-T
. The result:CTRL-T
command, in Insert mode, adds one 'shiftwidth' to the indent, no
matter where in the line you are.
You continue typing the second line, <Enter>
and the third line. This time
the indent is OK. Then <Enter>
and the last line. Now you have this:CTRL-D
. This deletes
one 'shiftwidth' worth of indent, no matter where you are in the line.
When you are in Normal mode, you can use the ">>" and "<<" commands to
shift lines. ">" and "<" are operators, thus you have the usual three ways to
specify the lines you want to indent. A useful combination is:>i{This adds one indent to the current block of lines, inside {}. The { and } lines themselves are left unmodified. ">a{" includes them. In this example the cursor is on "printf":
30.5
Tabs and spaces<Tab>
is eight spaces and you want to use an indent of four spaces,
you can't use a <Tab>
character to make your indent. There are two ways to
handle this:<Tab>
and space characters. Since a <Tab>
takes the place of
eight spaces, you have fewer characters in your file. Inserting a <Tab>
is quicker than eight spaces. Backspacing works faster as well.<Tab>
key look and feel as if tabs were set
at the value of 'softtabstop', but actually use a combination of tabs and
spaces.
After you execute the following command, every time you press the <Tab>
key
the cursor moves to the next 4-column boundary::set softtabstop=4When you start in the first column and press
<Tab>
, you get 4 spaces inserted
in your text. The second time, Vim takes out the 4 spaces and puts in a <Tab>
(thus taking you to column 8). Thus Vim uses as many <Tab>
s as possible, and
then fills up with spaces.
When backspacing it works the other way around. A <BS>
will always delete
the amount specified with 'softtabstop'. Then <Tab>
s are used as many as
possible and spaces to fill the gap.
The following shows what happens pressing <Tab>
a few times, and then using
<BS>
. A "." stands for a space and "------->" for a <Tab>
.<Tab>
....
<Tab>
<Tab>
------->
<Tab>
<Tab>
<Tab>
------->....
<Tab>
<Tab>
<Tab>
<BS>
------->
<Tab>
<Tab>
<Tab>
<BS>
<BS>
....<Tab>
typed in the indent of a line, and a real <Tab>
when
typed after the first non-blank character. However, <BS>
doesn't work like
with 'softtabstop'.:set expandtabWhen this option is set, the
<Tab>
key inserts a series of spaces. Thus you
get the same amount of white space as if a <Tab>
character was inserted, but
there isn't a real <Tab>
character in your file.
The backspace key will delete each space by itself. Thus after typing one
<Tab>
you have to press the <BS>
key up to eight times to undo it. If you are
in the indent, pressing CTRL-D
will be a lot quicker.:set expandtab :%retabNow Vim will have changed all indents to use spaces instead of tabs. However, all tabs that come after a non-blank character are kept. If you want these to be converted as well, add a !:
:%retab!This is a little bit dangerous, because it can also change tabs inside a string. To check if these exist, you could use this:
/"[^"\t]*\t[^"]*"It's recommended not to use hard tabs inside a string. Replace them with "\t" to avoid trouble.
:set noexpandtab :%retab!
30.6
Formatting comments/*
* This is a test
* of the text formatting.
*/
You then ask Vim to format it by positioning the cursor at the start of the
comment and type:gq]/"gq" is the operator to format text. "]/" is the motion that takes you to the end of a comment. The result is:
/*
* This is a test of the text formatting.
*/
Notice that Vim properly handled the beginning of each line.
An alternative is to select the text that is to be formatted in Visual mode
and type "gq"./*
* This is a test of the text formatting.
*
*/
Vim has automatically inserted a star and a space for you. Now you can type
the comment text. When it gets longer than 'textwidth', Vim will break the
line. Again, the star is inserted automatically:/*
* This is a test of the text formatting.
* Typing a lot of text here will make Vim
* break
*/
For this to work some flags must be present in 'formatoptions':<Enter>
in Insert mode
o insert the star when using "o" or "O" in Normal mode
c break comment text according to 'textwidth':set comments=://The colon separates the flags of an item from the text by which the comment is recognized. The general form of an item in 'comments' is:
{flags}
:{text}{flags}
part can be empty, as in this case.
Several of these items can be concatenated, separated by commas. This
allows recognizing different types of comments at the same time. For example,
let's edit an e-mail message. When replying, the text that others wrote is
preceded with ">" and "!" characters. This command would work::set comments=n:>,n:!There are two items, one for comments starting with ">" and one for comments that start with "!". Both use the flag "n". This means that these comments nest. Thus a line starting with ">" may have another comment after the ">". This allows formatting a message like this:
:set comments=s1:/*,mb:*,ex:*/The start is defined with "s1:/*". The "s" indicates the start of a three-piece comment. The colon separates the flags from the text by which the comment is recognized: "/*". There is one flag: "1". This tells Vim that the middle part has an offset of one space. The middle part "mb:*" starts with "m", which indicates it is a middle part. The "b" flag means that a blank must follow the text. Otherwise Vim would consider text like "*pointer" also to be the middle of a comment. The end part "ex:*/" has the "e" for identification. The "x" flag has a special meaning. It means that after Vim automatically inserted a star, typing / will remove the extra space.