usr_30.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. *usr_30.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Editing programs
  4. Vim has various commands that aid in writing computer programs. Compile a
  5. program and directly jump to reported errors. Automatically set the indent
  6. for many languages and format comments.
  7. |30.1| Compiling
  8. |30.2| Indenting C files
  9. |30.3| Automatic indenting
  10. |30.4| Other indenting
  11. |30.5| Tabs and spaces
  12. |30.6| Formatting comments
  13. Next chapter: |usr_31.txt| Exploiting the GUI
  14. Previous chapter: |usr_29.txt| Moving through programs
  15. Table of contents: |usr_toc.txt|
  16. ==============================================================================
  17. *30.1* Compiling
  18. Vim has a set of so called "quickfix" commands. They enable you to compile a
  19. program from within Vim and then go through the errors generated and fix them
  20. (hopefully). You can then recompile and fix any new errors that are found
  21. until finally your program compiles without any error.
  22. The following command runs the program "make" (supplying it with any argument
  23. you give) and captures the results: >
  24. :make {arguments}
  25. If errors were generated, they are captured and the editor positions you where
  26. the first error occurred.
  27. Take a look at an example ":make" session. (Typical :make sessions generate
  28. far more errors and fewer stupid ones.) After typing ":make" the screen looks
  29. like this:
  30. :!make | &tee /tmp/vim215953.err ~
  31. gcc -g -Wall -o prog main.c sub.c ~
  32. main.c: In function 'main': ~
  33. main.c:6: too many arguments to function 'do_sub' ~
  34. main.c: At top level: ~
  35. main.c:10: parse error before '}' ~
  36. make: *** [prog] Error 1 ~
  37. 2 returned ~
  38. "main.c" 11L, 111C ~
  39. (3 of 6): too many arguments to function 'do_sub' ~
  40. Press ENTER or type command to continue ~
  41. From this you can see that you have errors in the file "main.c". When you
  42. press <Enter>, Vim displays the file "main.c", with the cursor positioned on
  43. line 6, the first line with an error. You did not need to specify the file or
  44. the line number, Vim knew where to go by looking in the error messages.
  45. >
  46. +---------------------------------------------------+
  47. |int main() |
  48. |{ |
  49. | int i=3; |
  50. cursor -> | do_sub("foo"); |
  51. | ++i; |
  52. | return (0); |
  53. |} |
  54. |} |
  55. | ~ |
  56. |(3 of 12): too many arguments to function 'do_sub' |
  57. +---------------------------------------------------+
  58. <
  59. The following command goes to where the next error occurs: >
  60. :cnext
  61. Vim jumps to line 10, the last line in the file, where there is an extra '}'.
  62. When there is not enough room, Vim will shorten the error message. To see
  63. the whole message use: >
  64. :cc
  65. You can get an overview of all the error messages with the ":clist" command.
  66. The output looks like this: >
  67. :clist
  68. < 3 main.c: 6:too many arguments to function 'do_sub' ~
  69. 5 main.c: 10:parse error before '}' ~
  70. Only the lines where Vim recognized a file name and line number are listed
  71. here. It assumes those are the interesting lines and the rest is just boring
  72. messages. However, sometimes unrecognized lines do contain something you want
  73. to see. Output from the linker, for example, about an undefined function.
  74. To see all the messages add a "!" to the command: >
  75. :clist!
  76. < 1 gcc -g -Wall -o prog main.c sub.c ~
  77. 2 main.c: In function 'main': ~
  78. 3 main.c:6: too many arguments to function 'do_sub' ~
  79. 4 main.c: At top level: ~
  80. 5 main.c:10: parse error before '}' ~
  81. 6 make: *** [prog] Error 1 ~
  82. Vim will highlight the current error. To go back to the previous error, use:
  83. >
  84. :cprevious
  85. Other commands to move around in the error list:
  86. :cfirst to first error
  87. :clast to last error
  88. :cc 3 to error nr 3
  89. USING ANOTHER COMPILER
  90. The name of the program to run when the ":make" command is executed is defined
  91. by the 'makeprg' option. Usually this is set to "make", but Visual C++ users
  92. should set this to "nmake" by executing the following command: >
  93. :set makeprg=nmake
  94. You can also include arguments in this option. Special characters need to
  95. be escaped with a backslash. Example: >
  96. :set makeprg=nmake\ -f\ project.mak
  97. You can include special Vim keywords in the command specification. The %
  98. character expands to the name of the current file. So if you execute the
  99. command: >
  100. :set makeprg=make\ %:S
  101. When you are editing main.c, then ":make" executes the following command: >
  102. make main.c
  103. This is not too useful, so you will refine the command a little and use the :r
  104. (root) modifier: >
  105. :set makeprg=make\ %:r:S.o
  106. Now the command executed is as follows: >
  107. make main.o
  108. More about these modifiers here: |filename-modifiers|.
  109. OLD ERROR LISTS
  110. Suppose you ":make" a program. There is a warning message in one file and an
  111. error message in another. You fix the error and use ":make" again to check if
  112. it was really fixed. Now you want to look at the warning message. It doesn't
  113. show up in the last error list, since the file with the warning wasn't
  114. compiled again. You can go back to the previous error list with: >
  115. :colder
  116. Then use ":clist" and ":cc {nr}" to jump to the place with the warning.
  117. To go forward to the next error list: >
  118. :cnewer
  119. Vim remembers ten error lists.
  120. SWITCHING COMPILERS
  121. You have to tell Vim what format the error messages are that your compiler
  122. produces. This is done with the 'errorformat' option. The syntax of this
  123. option is quite complicated and it can be made to fit almost any compiler.
  124. You can find the explanation here: |errorformat|.
  125. You might be using various different compilers. Setting the 'makeprg' option,
  126. and especially the 'errorformat' each time is not easy. Vim offers a simple
  127. method for this. For example, to switch to using the Microsoft Visual C++
  128. compiler: >
  129. :compiler msvc
  130. This will find the Vim script for the "msvc" compiler and set the appropriate
  131. options.
  132. You can write your own compiler files. See |write-compiler-plugin|.
  133. OUTPUT REDIRECTION
  134. The ":make" command redirects the output of the executed program to an error
  135. file. How this works depends on various things, such as the 'shell'. If your
  136. ":make" command doesn't capture the output, check the 'makeef' and
  137. 'shellpipe' options. The 'shellquote' and 'shellxquote' options might also
  138. matter.
  139. In case you can't get ":make" to redirect the file for you, an alternative is
  140. to compile the program in another window and redirect the output into a file.
  141. Then have Vim read this file with: >
  142. :cfile {filename}
  143. Jumping to errors will work like with the ":make" command.
  144. ==============================================================================
  145. *30.2* Indenting C style text
  146. A program is much easier to understand when the lines have been properly
  147. indented. Vim offers various ways to make this less work. For C or C style
  148. programs like Java or C++, set the 'cindent' option. Vim knows a lot about C
  149. programs and will try very hard to automatically set the indent for you. Set
  150. the 'shiftwidth' option to the amount of spaces you want for a deeper level.
  151. Four spaces will work fine. One ":set" command will do it: >
  152. :set cindent shiftwidth=4
  153. With this option enabled, when you type something such as "if (x)", the next
  154. line will automatically be indented an additional level.
  155. if (flag)
  156. Automatic indent ---> do_the_work();
  157. Automatic unindent <-- if (other_flag) {
  158. Automatic indent ---> do_file();
  159. keep indent do_some_more();
  160. Automatic unindent <-- }
  161. When you type something in curly braces ({}), the text will be indented at the
  162. start and unindented at the end. The unindenting will happen after typing the
  163. '}', since Vim can't guess what you are going to type.
  164. One side effect of automatic indentation is that it helps you catch errors in
  165. your code early. When you type a } to finish a function, only to find that
  166. the automatic indentation gives it more indent than what you expected, there
  167. is probably a } missing. Use the "%" command to find out which { matches the
  168. } you typed.
  169. A missing ) and ; also cause extra indent. Thus if you get more white
  170. space than you would expect, check the preceding lines.
  171. When you have code that is badly formatted, or you inserted and deleted lines,
  172. you need to re-indent the lines. The "=" operator does this. The simplest
  173. form is: >
  174. ==
  175. This indents the current line. Like with all operators, there are three ways
  176. to use it. In Visual mode "=" indents the selected lines. A useful text
  177. object is "a{". This selects the current {} block. Thus, to re-indent the
  178. code block the cursor is in: >
  179. =a{
  180. If you have really badly indented code, you can re-indent the whole file with:
  181. >
  182. gg=G
  183. However, don't do this in files that have been carefully indented manually.
  184. The automatic indenting does a good job, but in some situations you might want
  185. to overrule it.
  186. SETTING INDENT STYLE
  187. Different people have different styles of indentation. By default Vim does a
  188. pretty good job of indenting in a way that 90% of programmers do. There are
  189. different styles, however; so if you want to, you can customize the
  190. indentation style with the 'cinoptions' option.
  191. By default 'cinoptions' is empty and Vim uses the default style. You can
  192. add various items where you want something different. For example, to make
  193. curly braces be placed like this:
  194. if (flag) ~
  195. { ~
  196. i = 8; ~
  197. j = 0; ~
  198. } ~
  199. Use this command: >
  200. :set cinoptions+={2
  201. There are many of these items. See |cinoptions-values|.
  202. ==============================================================================
  203. *30.3* Automatic indenting
  204. You don't want to switch on the 'cindent' option manually every time you edit
  205. a C file. This is how you make it work automatically: >
  206. :filetype indent on
  207. Actually, this does a lot more than switching on 'cindent' for C files. First
  208. of all, it enables detecting the type of a file. That's the same as what is
  209. used for syntax highlighting.
  210. When the filetype is known, Vim will search for an indent file for this
  211. type of file. The Vim distribution includes a number of these for various
  212. programming languages. This indent file will then prepare for automatic
  213. indenting specifically for this file.
  214. If you don't like the automatic indenting, you can switch it off again: >
  215. :filetype indent off
  216. If you don't like the indenting for one specific type of file, this is how you
  217. avoid it. Create a file with just this one line: >
  218. :let b:did_indent = 1
  219. Now you need to write this in a file with a specific name:
  220. {directory}/indent/{filetype}.vim
  221. The {filetype} is the name of the file type, such as "cpp" or "java". You can
  222. see the exact name that Vim detected with this command: >
  223. :set filetype
  224. In this file the output is:
  225. filetype=help ~
  226. Thus you would use "help" for {filetype}.
  227. For the {directory} part you need to use your runtime directory. Look at
  228. the output of this command: >
  229. set runtimepath
  230. Now use the first item, the name before the first comma. Thus if the output
  231. looks like this:
  232. runtimepath=~/.config/nvim,/usr/local/share/vim/vim60/runtime,~/.config/nvim/after ~
  233. You use "~/.config/nvim" for {directory}. Then the resulting file name is:
  234. ~/.config/nvim/indent/help.vim ~
  235. Instead of switching the indenting off, you could write your own indent file.
  236. How to do that is explained here: |indent-expression|.
  237. ==============================================================================
  238. *30.4* Other indenting
  239. The simplest form of automatic indenting is with the 'autoindent' option.
  240. It uses the indent from the previous line. A bit smarter is the 'smartindent'
  241. option. This is useful for languages where no indent file is available.
  242. 'smartindent' is not as smart as 'cindent', but smarter than 'autoindent'.
  243. With 'smartindent' set, an extra level of indentation is added for each {
  244. and removed for each }. An extra level of indentation will also be added for
  245. any of the words in the 'cinwords' option. Lines that begin with # are
  246. treated specially: all indentation is removed. This is done so that
  247. preprocessor directives will all start in column 1. The indentation is
  248. restored for the next line.
  249. CORRECTING INDENTS
  250. When you are using 'autoindent' or 'smartindent' to get the indent of the
  251. previous line, there will be many times when you need to add or remove one
  252. 'shiftwidth' worth of indent. A quick way to do this is using the CTRL-D and
  253. CTRL-T commands in Insert mode.
  254. For example, you are typing a shell script that is supposed to look like
  255. this:
  256. if test -n a; then ~
  257. echo a ~
  258. echo "-------" ~
  259. fi ~
  260. Start off by setting these options: >
  261. :set autoindent shiftwidth=3
  262. You start by typing the first line, <Enter> and the start of the second line:
  263. if test -n a; then ~
  264. echo ~
  265. Now you see that you need an extra indent. Type CTRL-T. The result:
  266. if test -n a; then ~
  267. echo ~
  268. The CTRL-T command, in Insert mode, adds one 'shiftwidth' to the indent, no
  269. matter where in the line you are.
  270. You continue typing the second line, <Enter> and the third line. This time
  271. the indent is OK. Then <Enter> and the last line. Now you have this:
  272. if test -n a; then ~
  273. echo a ~
  274. echo "-------" ~
  275. fi ~
  276. To remove the superfluous indent in the last line press CTRL-D. This deletes
  277. one 'shiftwidth' worth of indent, no matter where you are in the line.
  278. When you are in Normal mode, you can use the ">>" and "<<" commands to
  279. shift lines. ">" and "<" are operators, thus you have the usual three ways to
  280. specify the lines you want to indent. A useful combination is: >
  281. >i{
  282. This adds one indent to the current block of lines, inside {}. The { and }
  283. lines themselves are left unmodified. ">a{" includes them. In this example
  284. the cursor is on "printf":
  285. original text after ">i{" after ">a{"
  286. if (flag) if (flag) if (flag) ~
  287. { { { ~
  288. printf("yes"); printf("yes"); printf("yes"); ~
  289. flag = 0; flag = 0; flag = 0; ~
  290. } } } ~
  291. ==============================================================================
  292. *30.5* Tabs and spaces
  293. 'tabstop' is set to eight by default. Although you can change it, you quickly
  294. run into trouble later. Other programs won't know what tabstop value you
  295. used. They probably use the default value of eight, and your text suddenly
  296. looks very different. Also, most printers use a fixed tabstop value of eight.
  297. Thus it's best to keep 'tabstop' alone. (If you edit a file which was written
  298. with a different tabstop setting, see |25.3| for how to fix that.)
  299. For indenting lines in a program, using a multiple of eight spaces makes
  300. you quickly run into the right border of the window. Using a single space
  301. doesn't provide enough visual difference. Many people prefer to use four
  302. spaces, a good compromise.
  303. Since a <Tab> is eight spaces and you want to use an indent of four spaces,
  304. you can't use a <Tab> character to make your indent. There are two ways to
  305. handle this:
  306. 1. Use a mix of <Tab> and space characters. Since a <Tab> takes the place of
  307. eight spaces, you have fewer characters in your file. Inserting a <Tab>
  308. is quicker than eight spaces. Backspacing works faster as well.
  309. 2. Use spaces only. This avoids the trouble with programs that use a
  310. different tabstop value.
  311. Fortunately, Vim supports both methods quite well.
  312. SPACES AND TABS
  313. If you are using a combination of tabs and spaces, you just edit normally.
  314. The Vim defaults do a fine job of handling things.
  315. You can make life a little easier by setting the 'softtabstop' option.
  316. This option tells Vim to make the <Tab> key look and feel as if tabs were set
  317. at the value of 'softtabstop', but actually use a combination of tabs and
  318. spaces.
  319. After you execute the following command, every time you press the <Tab> key
  320. the cursor moves to the next 4-column boundary: >
  321. :set softtabstop=4
  322. When you start in the first column and press <Tab>, you get 4 spaces inserted
  323. in your text. The second time, Vim takes out the 4 spaces and puts in a <Tab>
  324. (thus taking you to column 8). Thus Vim uses as many <Tab>s as possible, and
  325. then fills up with spaces.
  326. When backspacing it works the other way around. A <BS> will always delete
  327. the amount specified with 'softtabstop'. Then <Tab>s are used as many as
  328. possible and spaces to fill the gap.
  329. The following shows what happens pressing <Tab> a few times, and then using
  330. <BS>. A "." stands for a space and "------->" for a <Tab>.
  331. type result ~
  332. <Tab> ....
  333. <Tab><Tab> ------->
  334. <Tab><Tab><Tab> ------->....
  335. <Tab><Tab><Tab><BS> ------->
  336. <Tab><Tab><Tab><BS><BS> ....
  337. An alternative is to use the 'smarttab' option. When it's set, Vim uses
  338. 'shiftwidth' for a <Tab> typed in the indent of a line, and a real <Tab> when
  339. typed after the first non-blank character. However, <BS> doesn't work like
  340. with 'softtabstop'.
  341. JUST SPACES
  342. If you want absolutely no tabs in your file, you can set the 'expandtab'
  343. option: >
  344. :set expandtab
  345. When this option is set, the <Tab> key inserts a series of spaces. Thus you
  346. get the same amount of white space as if a <Tab> character was inserted, but
  347. there isn't a real <Tab> character in your file.
  348. The backspace key will delete each space by itself. Thus after typing one
  349. <Tab> you have to press the <BS> key up to eight times to undo it. If you are
  350. in the indent, pressing CTRL-D will be a lot quicker.
  351. CHANGING TABS IN SPACES (AND BACK)
  352. Setting 'expandtab' does not affect any existing tabs. In other words, any
  353. tabs in the document remain tabs. If you want to convert tabs to spaces, use
  354. the ":retab" command. Use these commands: >
  355. :set expandtab
  356. :%retab
  357. Now Vim will have changed all indents to use spaces instead of tabs. However,
  358. all tabs that come after a non-blank character are kept. If you want these to
  359. be converted as well, add a !: >
  360. :%retab!
  361. This is a little bit dangerous, because it can also change tabs inside a
  362. string. To check if these exist, you could use this: >
  363. /"[^"\t]*\t[^"]*"
  364. It's recommended not to use hard tabs inside a string. Replace them with
  365. "\t" to avoid trouble.
  366. The other way around works just as well: >
  367. :set noexpandtab
  368. :%retab!
  369. ==============================================================================
  370. *30.6* Formatting comments
  371. One of the great things about Vim is that it understands comments. You can
  372. ask Vim to format a comment and it will do the right thing.
  373. Suppose, for example, that you have the following comment: >c
  374. /*
  375. * This is a test
  376. * of the text formatting.
  377. */
  378. You then ask Vim to format it by positioning the cursor at the start of the
  379. comment and type: >
  380. gq]/
  381. "gq" is the operator to format text. "]/" is the motion that takes you to the
  382. end of a comment. The result is: >c
  383. /*
  384. * This is a test of the text formatting.
  385. */
  386. Notice that Vim properly handled the beginning of each line.
  387. An alternative is to select the text that is to be formatted in Visual mode
  388. and type "gq".
  389. To add a new line to the comment, position the cursor on the middle line and
  390. press "o". The result looks like this: >c
  391. /*
  392. * This is a test of the text formatting.
  393. *
  394. */
  395. Vim has automatically inserted a star and a space for you. Now you can type
  396. the comment text. When it gets longer than 'textwidth', Vim will break the
  397. line. Again, the star is inserted automatically: >c
  398. /*
  399. * This is a test of the text formatting.
  400. * Typing a lot of text here will make Vim
  401. * break
  402. */
  403. For this to work some flags must be present in 'formatoptions':
  404. r insert the star when typing <Enter> in Insert mode
  405. o insert the star when using "o" or "O" in Normal mode
  406. c break comment text according to 'textwidth'
  407. See |fo-table| for more flags.
  408. DEFINING A COMMENT
  409. The 'comments' option defines what a comment looks like. Vim distinguishes
  410. between a single-line comment and a comment that has a different start, end
  411. and middle part.
  412. Many single-line comments start with a specific character. In C++ // is
  413. used, in Makefiles #, in Vim scripts ". For example, to make Vim understand
  414. C++ comments: >
  415. :set comments=://
  416. The colon separates the flags of an item from the text by which the comment is
  417. recognized. The general form of an item in 'comments' is:
  418. {flags}:{text}
  419. The {flags} part can be empty, as in this case.
  420. Several of these items can be concatenated, separated by commas. This
  421. allows recognizing different types of comments at the same time. For example,
  422. let's edit an e-mail message. When replying, the text that others wrote is
  423. preceded with ">" and "!" characters. This command would work: >
  424. :set comments=n:>,n:!
  425. There are two items, one for comments starting with ">" and one for comments
  426. that start with "!". Both use the flag "n". This means that these comments
  427. nest. Thus a line starting with ">" may have another comment after the ">".
  428. This allows formatting a message like this:
  429. > ! Did you see that site? ~
  430. > ! It looks really great. ~
  431. > I don't like it. The ~
  432. > colors are terrible. ~
  433. What is the URL of that ~
  434. site? ~
  435. Try setting 'textwidth' to a different value, e.g., 80, and format the text by
  436. Visually selecting it and typing "gq". The result is:
  437. > ! Did you see that site? It looks really great. ~
  438. > I don't like it. The colors are terrible. ~
  439. What is the URL of that site? ~
  440. You will notice that Vim did not move text from one type of comment to
  441. another. The "I" in the second line would have fit at the end of the first
  442. line, but since that line starts with "> !" and the second line with ">", Vim
  443. knows that this is a different kind of comment.
  444. A THREE PART COMMENT
  445. A C comment starts with "/*", has "*" in the middle and "*/" at the end. The
  446. entry in 'comments' for this looks like this: >
  447. :set comments=s1:/*,mb:*,ex:*/
  448. The start is defined with "s1:/*". The "s" indicates the start of a
  449. three-piece comment. The colon separates the flags from the text by which the
  450. comment is recognized: "/*". There is one flag: "1". This tells Vim that the
  451. middle part has an offset of one space.
  452. The middle part "mb:*" starts with "m", which indicates it is a middle
  453. part. The "b" flag means that a blank must follow the text. Otherwise Vim
  454. would consider text like "*pointer" also to be the middle of a comment.
  455. The end part "ex:*/" has the "e" for identification. The "x" flag has a
  456. special meaning. It means that after Vim automatically inserted a star,
  457. typing / will remove the extra space.
  458. For more details see |format-comments|.
  459. ==============================================================================
  460. Next chapter: |usr_31.txt| Exploiting the GUI
  461. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: