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.
: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.
: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:CTRL-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":
vi
(the ancestor of Vim) was created by Bill Joy. At the time, he was using
a PDP-11 with limited memory and I/O operation capabilities. Back then, it
was common to optimize the size of source code with the following trick.
The ASCII table was first designed to remotely control teleprinters. When
control character 9 (the Horizontal Tab, caret notation: ^I) was sent to a
teleprinter, it would move the carriage to the next tab stop. Assuming tab
stops were separated by 8 columns (a typical standard), this means that a
single control character could produce the same visual effect as up to 8 space
characters. For example, the following two lines will display identically1234^I9 1234 9Using the
<Tab>
key was also faster than typing <Space>
several times; the
same was true for <BS>
.vi
had the 'shiftwidth' option. When set to 4, on a new
line, pressing <C-t>
in Insert mode would indent the line by 4 spaces,
a result impossible to get with the <Tab>
key and 'tabstop' set to 8.
To optimize space, vi
would also silently remove packs of spaces and replace
them with tab characters. The following shows what happens pressing <C-t>
a few times.
A "." stands for a space character and "------->" for a tab character.<C-t>
....
<C-t>
<C-t>
------->
<C-t>
<C-t>
<C-t>
------->....<C-d>
in Insert mode would decrease the indent. Hence
with set tabstop=8 shiftwidth=2
one has<C-t>
<Tab>
<C-t>
..----->..
<C-t>
<Tab>
<C-t>
<C-d>
------->vi
was 'autoindent'. It copies the
indent level of the previous lines,<Space>
<Tab>
hello .------>hello
<Space>
<Tab>
hello<Enter> .------>hello
------->tabstop=4
. Every
time text is displayed with a different 'tabstop' value, it risks misaligning
the text, especially once the file is shared and opened on another machine.
In the meantime, computers got much better and the few octets saved by using
tabs were no longer making any real difference. It became possible to use
only spaces and thus guarantee the same resulting text everywhere. But using
only spaces was impossible in vi
without sacrificing features. Remember that
'autoindent' would systematically try to input a tab character when it could.
Vim 4.0 made working with only spaces as convenient as working only with
tabs (or a mix of tabs and spaces), by introducing the 'expandtab' option.
When set, Vim will replace any horizontal tab character it would normally
insert with an equivalent number of spaces, to end up with the same visual
effect. <BS>
would continue to remove only one character at a time.<Tab>
........
<Tab>
<BS>
.......:set expandtab :retabThis 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 actual tab characters inside a string. Replace them with "\t" to avoid trouble.
:set noexpandtab :retab!
<Tab>
and <BS>
do not act in mirror, as
they do when using only tab characters.
Vim 5.4 introduced the 'softtabstop' option. On top of the (hard) tab stops
used to display the horizontal tab characters in the text, Vim adds extra
soft tab stops dedicated only to the cursor. When 'softtabstop' is set to a
positive value, and the <Tab>
key will push the cursor to the next soft tab
stop. Vim will insert the correct combination of tab characters and spaces to
make the effect visually. Likewise pressing <BS>
will have the cursor try to
reach the nearest soft tab stop. The following example uses
:set softtabstop=4
<Tab>
....
<Tab>
<Tab>
a ------->a
<Tab>
<Tab>
a<Tab> ------->a...
<Tab>
<Tab>
a<Tab><BS> ------->a:set softtabstop=-1
so that
the value of 'shiftwidth' is use for the number of columns between two soft
tab stops.<C-t>
to indent with 'shiftwidth'. Or you can
use the 'smarttab' option introduced in Vim 5.6, allowing for a unified
<Tab>
key that knows what to do in the different situations.+vartabs
and :set vartabstop=2,4
one gets:set varsofttabstop=2,4
one gets<Tab>
..
<Tab>
<Tab>
......
<Tab>
<Tab>
<Tab>
------->....:set tabstop=8 :set shiftwidth=8 :set noexpandtab :set softtabstop=0 :set nosmarttab
:set shiftwidth=4 :set softtabstop=-1 :set expandtab
:set shiftwidth=4 :set softtabstop=2 :set expandtab :set smarttab
:set shiftwidth=4 :set softtabstop=-1
/*
* 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.