usr_06.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. *usr_06.txt* For Vim version 9.0. Last change: 2021 Nov 07
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Using syntax highlighting
  4. Black and white text is boring. With colors your file comes to life. This
  5. not only looks nice, it also speeds up your work. Change the colors used for
  6. the different sorts of text. Print your text, with the colors you see on the
  7. screen.
  8. |06.1| Switching it on
  9. |06.2| No or wrong colors?
  10. |06.3| Different colors
  11. |06.4| With colors or without colors
  12. |06.5| Printing with colors
  13. |06.6| Further reading
  14. Next chapter: |usr_07.txt| Editing more than one file
  15. Previous chapter: |usr_05.txt| Set your settings
  16. Table of contents: |usr_toc.txt|
  17. ==============================================================================
  18. *06.1* Switching it on
  19. It all starts with one simple command: >
  20. :syntax enable
  21. That should work in most situations to get color in your files. Vim will
  22. automagically detect the type of file and load the right syntax highlighting.
  23. Suddenly comments are blue, keywords brown and strings red. This makes it
  24. easy to overview the file. After a while you will find that black&white text
  25. slows you down!
  26. If you always want to use syntax highlighting, put the ":syntax enable"
  27. command in your |vimrc| file.
  28. If you want syntax highlighting only when the terminal supports colors, you
  29. can put this in your |vimrc| file: >
  30. if &t_Co > 1
  31. syntax enable
  32. endif
  33. If you want syntax highlighting only in the GUI version, put the ":syntax
  34. enable" command in your |gvimrc| file.
  35. ==============================================================================
  36. *06.2* No or wrong colors?
  37. There can be a number of reasons why you don't see colors:
  38. - Your terminal does not support colors.
  39. Vim will use bold, italic and underlined text, but this doesn't look
  40. very nice. You probably will want to try to get a terminal with
  41. colors. For Unix, I recommend the xterm from the XFree86 project:
  42. |xfree-xterm|.
  43. - Your terminal does support colors, but Vim doesn't know this.
  44. Make sure your $TERM setting is correct. For example, when using an
  45. xterm that supports colors: >
  46. setenv TERM xterm-color
  47. <
  48. or (depending on your shell): >
  49. TERM=xterm-color; export TERM
  50. < The terminal name must match the terminal you are using. If it
  51. still doesn't work, have a look at |xterm-color|, which shows a few
  52. ways to make Vim display colors (not only for an xterm).
  53. - The file type is not recognized.
  54. Vim doesn't know all file types, and sometimes it's near to impossible
  55. to tell what language a file uses. Try this command: >
  56. :set filetype
  57. <
  58. If the result is "filetype=" then the problem is indeed that Vim
  59. doesn't know what type of file this is. You can set the type
  60. manually: >
  61. :set filetype=fortran
  62. < To see which types are available, look in the directory
  63. $VIMRUNTIME/syntax. For the GUI you can use the Syntax menu.
  64. Setting the filetype can also be done with a |modeline|, so that the
  65. file will be highlighted each time you edit it. For example, this
  66. line can be used in a Makefile (put it near the start or end of the
  67. file): >
  68. # vim: syntax=make
  69. < You might know how to detect the file type yourself. Often the file
  70. name extension (after the dot) can be used.
  71. See |new-filetype| for how to tell Vim to detect that file type.
  72. - There is no highlighting for your file type.
  73. You could try using a similar file type by manually setting it as
  74. mentioned above. If that isn't good enough, you can write your own
  75. syntax file, see |mysyntaxfile|.
  76. Or the colors could be wrong:
  77. - The colored text is very hard to read.
  78. Vim guesses the background color that you are using. If it is black
  79. (or another dark color) it will use light colors for text. If it is
  80. white (or another light color) it will use dark colors for text. If
  81. Vim guessed wrong the text will be hard to read. To solve this, set
  82. the 'background' option. For a dark background: >
  83. :set background=dark
  84. < And for a light background: >
  85. :set background=light
  86. < Make sure you put this _before_ the ":syntax enable" command,
  87. otherwise the colors will already have been set. You could do
  88. ":syntax reset" after setting 'background' to make Vim set the default
  89. colors again.
  90. - The colors are wrong when scrolling bottom to top.
  91. Vim doesn't read the whole file to parse the text. It starts parsing
  92. wherever you are viewing the file. That saves a lot of time, but
  93. sometimes the colors are wrong. A simple fix is hitting CTRL-L. Or
  94. scroll back a bit and then forward again.
  95. For a real fix, see |:syn-sync|. Some syntax files have a way to make
  96. it look further back, see the help for the specific syntax file. For
  97. example, |tex.vim| for the TeX syntax.
  98. ==============================================================================
  99. *06.3* Different colors *:syn-default-override*
  100. If you don't like the default colors, you can select another color scheme. In
  101. the GUI use the Edit/Color Scheme menu. You can also type the command: >
  102. :colorscheme evening
  103. "evening" is the name of the color scheme. There are several others you might
  104. want to try out. Look in the directory $VIMRUNTIME/colors.
  105. When you found the color scheme that you like, add the ":colorscheme" command
  106. to your |vimrc| file.
  107. You could also write your own color scheme. This is how you do it:
  108. 1. Select a color scheme that comes close. Copy this file to your own Vim
  109. directory. For Unix, this should work: >
  110. !mkdir ~/.vim/colors
  111. !cp $VIMRUNTIME/colors/morning.vim ~/.vim/colors/mine.vim
  112. <
  113. This is done from Vim, because it knows the value of $VIMRUNTIME.
  114. 2. Edit the color scheme file. These entries are useful:
  115. term attributes in a B&W terminal
  116. cterm attributes in a color terminal
  117. ctermfg foreground color in a color terminal
  118. ctermbg background color in a color terminal
  119. gui attributes in the GUI
  120. guifg foreground color in the GUI
  121. guibg background color in the GUI
  122. For example, to make comments green: >
  123. :highlight Comment ctermfg=green guifg=green
  124. <
  125. Attributes you can use for "cterm" and "gui" are "bold" and "underline".
  126. If you want both, use "bold,underline". For details see the |:highlight|
  127. command.
  128. 3. Tell Vim to always use your color scheme. Put this line in your |vimrc|: >
  129. colorscheme mine
  130. If you want to see what the most often used color combinations look like, use
  131. this command: >
  132. :runtime syntax/colortest.vim
  133. You will see text in various color combinations. You can check which ones are
  134. readable and look nice. These aren't the only colors available to you though.
  135. You can specify #rrggbb hex colors and you can define new names for hex
  136. colors in |v:colornames| like so: >
  137. let v:colornames['mine_red'] = '#aa0000'
  138. If you are authoring a color scheme for others to use, it is important
  139. to define these colors only when they do not exist: >
  140. call extend(v:colornames, {'mine_red': '#aa0000'}, 'keep')
  141. This allows users of the color scheme to override the precise definition of
  142. that color prior to loading your color scheme. For example, in a |.vimrc|
  143. file: >
  144. runtime colors/lists/css_colors.vim
  145. let v:colornames['your_red'] = v:colornames['css_red']
  146. colorscheme yourscheme
  147. As a color scheme author, you should be able to rely on some color names for
  148. GUI colors. These are defined in `colors/lists/default.vim`. All such files
  149. found on the |'runtimepath'| are loaded each time the colorscheme command is
  150. run. A canonical list is provided by the vim distribution, which should
  151. include all X11 colors (previously defined in rgb.txt).
  152. ==============================================================================
  153. *06.4* With colors or without colors
  154. Displaying text in color takes a lot of effort. If you find the displaying
  155. too slow, you might want to disable syntax highlighting for a moment: >
  156. :syntax clear
  157. When editing another file (or the same one) the colors will come back.
  158. If you want to stop highlighting completely use: >
  159. :syntax off
  160. This will completely disable syntax highlighting and remove it immediately for
  161. all buffers. See |:syntax-off| for more details.
  162. *:syn-manual*
  163. If you want syntax highlighting only for specific files, use this: >
  164. :syntax manual
  165. This will enable the syntax highlighting, but not switch it on automatically
  166. when starting to edit a buffer. To switch highlighting on for the current
  167. buffer, set the 'syntax' option: >
  168. :set syntax=ON
  169. <
  170. ==============================================================================
  171. *06.5* Printing with colors *syntax-printing*
  172. In the MS-Windows version you can print the current file with this command: >
  173. :hardcopy
  174. You will get the usual printer dialog, where you can select the printer and a
  175. few settings. If you have a color printer, the paper output should look the
  176. same as what you see inside Vim. But when you use a dark background the
  177. colors will be adjusted to look good on white paper.
  178. There are several options that change the way Vim prints:
  179. 'printdevice'
  180. 'printheader'
  181. 'printfont'
  182. 'printoptions'
  183. To print only a range of lines, use Visual mode to select the lines and then
  184. type the command: >
  185. v100j:hardcopy
  186. "v" starts Visual mode. "100j" moves a hundred lines down, they will be
  187. highlighted. Then ":hardcopy" will print those lines. You can use other
  188. commands to move in Visual mode, of course.
  189. This also works on Unix, if you have a PostScript printer. Otherwise, you
  190. will have to do a bit more work. You need to convert the text to HTML first,
  191. and then print it from a web browser.
  192. Convert the current file to HTML with this command: >
  193. :TOhtml
  194. In case that doesn't work: >
  195. :source $VIMRUNTIME/syntax/2html.vim
  196. You will see it crunching away, this can take quite a while for a large file.
  197. Some time later another window shows the HTML code. Now write this somewhere
  198. (doesn't matter where, you throw it away later):
  199. >
  200. :write main.c.html
  201. Open this file in your favorite browser and print it from there. If all goes
  202. well, the output should look exactly as it does in Vim. See |2html.vim| for
  203. details. Don't forget to delete the HTML file when you are done with it.
  204. Instead of printing, you could also put the HTML file on a web server, and let
  205. others look at the colored text.
  206. ==============================================================================
  207. *06.6* Further reading
  208. |usr_44.txt| Your own syntax highlighted.
  209. |syntax| All the details.
  210. ==============================================================================
  211. Next chapter: |usr_07.txt| Editing more than one file
  212. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: