usr_30.txt 22 KB

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