usr_06.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. *usr_06.txt* Nvim
  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. Syntax highlighting is enabled by default. Nvim will automagically detect the
  20. type of file and load the right syntax highlighting.
  21. ==============================================================================
  22. *06.2* No or wrong colors?
  23. There can be a number of reasons why you don't see colors:
  24. - Your terminal does not support colors.
  25. Vim will use bold, italic and underlined text, but this doesn't look
  26. very nice. You probably will want to try to get a terminal with
  27. colors.
  28. - Your terminal does support colors, but Vim doesn't know this.
  29. Make sure your $TERM setting is correct. For example, when using an
  30. xterm that supports colors: >
  31. setenv TERM xterm-color
  32. <
  33. or (depending on your shell): >
  34. TERM=xterm-color; export TERM
  35. < The terminal name must match the terminal you are using.
  36. - The file type is not recognized.
  37. Vim doesn't know all file types, and sometimes it's near to impossible
  38. to tell what language a file uses. Try this command: >
  39. :set filetype
  40. <
  41. If the result is "filetype=" then the problem is indeed that Vim
  42. doesn't know what type of file this is. You can set the type
  43. manually: >
  44. :set filetype=fortran
  45. < To see which types are available, look in the directory
  46. $VIMRUNTIME/syntax. For the GUI you can use the Syntax menu.
  47. Setting the filetype can also be done with a |modeline|, so that the
  48. file will be highlighted each time you edit it. For example, this
  49. line can be used in a Makefile (put it near the start or end of the
  50. file): >
  51. # vim: syntax=make
  52. < You might know how to detect the file type yourself. Often the file
  53. name extension (after the dot) can be used.
  54. See |new-filetype| for how to tell Vim to detect that file type.
  55. - There is no highlighting for your file type.
  56. You could try using a similar file type by manually setting it as
  57. mentioned above. If that isn't good enough, you can write your own
  58. syntax file, see |mysyntaxfile|.
  59. Or the colors could be wrong:
  60. - The colored text is very hard to read.
  61. Vim guesses the background color that you are using. If it is black
  62. (or another dark color) it will use light colors for text. If it is
  63. white (or another light color) it will use dark colors for text. If
  64. Vim guessed wrong the text will be hard to read. To solve this, set
  65. the 'background' option. For a dark background: >
  66. :set background=dark
  67. < And for a light background: >
  68. :set background=light
  69. < Make sure you put this _before_ the ":syntax enable" command,
  70. otherwise the colors will already have been set. You could do
  71. ":syntax reset" after setting 'background' to make Vim set the default
  72. colors again.
  73. - The colors are wrong when scrolling bottom to top.
  74. Vim doesn't read the whole file to parse the text. It starts parsing
  75. wherever you are viewing the file. That saves a lot of time, but
  76. sometimes the colors are wrong. A simple fix is hitting CTRL-L. Or
  77. scroll back a bit and then forward again.
  78. For a real fix, see |:syn-sync|. Some syntax files have a way to make
  79. it look further back, see the help for the specific syntax file. For
  80. example, |tex.vim| for the TeX syntax.
  81. ==============================================================================
  82. *06.3* Different colors *:syn-default-override*
  83. If you don't like the default colors, you can select another color scheme. In
  84. the GUI use the Edit/Color Scheme menu. You can also type the command: >
  85. :colorscheme evening
  86. "evening" is the name of the color scheme. There are several others you might
  87. want to try out. Look in the directory $VIMRUNTIME/colors.
  88. When you found the color scheme that you like, add the ":colorscheme" command
  89. to your |init.vim| file.
  90. You could also write your own color scheme. This is how you do it:
  91. 1. Select a color scheme that comes close. Copy this file to your own Vim
  92. directory. For Unix, this should work: >
  93. !mkdir -p ~/.config/nvim/colors
  94. !cp $VIMRUNTIME/colors/morning.vim ~/.config/nvim/colors/mine.vim
  95. <
  96. This is done from Vim, because it knows the value of $VIMRUNTIME.
  97. 2. Edit the color scheme file. These entries are useful:
  98. cterm attributes in a color terminal
  99. ctermfg foreground color in a color terminal
  100. ctermbg background color in a color terminal
  101. gui attributes in the GUI
  102. guifg foreground color in the GUI
  103. guibg background color in the GUI
  104. For example, to make comments green: >
  105. :highlight Comment ctermfg=green guifg=green
  106. <
  107. Attributes you can use for "cterm" and "gui" are "bold" and "underline".
  108. If you want both, use "bold,underline". For details see the |:highlight|
  109. command.
  110. 3. Tell Vim to always use your color scheme. Put this line in your |vimrc|: >
  111. colorscheme mine
  112. If you want to see what the most often used color combinations look like, use
  113. this command: >
  114. :runtime syntax/colortest.vim
  115. You will see text in various color combinations. You can check which ones are
  116. readable and look nice.
  117. ==============================================================================
  118. *06.4* With colors or without colors
  119. Displaying text in color takes a lot of effort. If you find the displaying
  120. too slow, you might want to disable syntax highlighting for a moment: >
  121. :syntax clear
  122. When editing another file (or the same one) the colors will come back.
  123. If you want to stop highlighting completely use: >
  124. :syntax off
  125. This will completely disable syntax highlighting and remove it immediately for
  126. all buffers. See |:syntax-off| for more details.
  127. *:syn-manual*
  128. If you want syntax highlighting only for specific files, use this: >
  129. :syntax manual
  130. This will enable the syntax highlighting, but not switch it on automatically
  131. when starting to edit a buffer. To switch highlighting on for the current
  132. buffer, set the 'syntax' option: >
  133. :set syntax=ON
  134. <
  135. ==============================================================================
  136. *06.5* Printing with colors *syntax-printing*
  137. In the MS-Windows version you can print the current file with this command: >
  138. :hardcopy
  139. You will get the usual printer dialog, where you can select the printer and a
  140. few settings. If you have a color printer, the paper output should look the
  141. same as what you see inside Vim. But when you use a dark background the
  142. colors will be adjusted to look good on white paper.
  143. There are several options that change the way Vim prints:
  144. 'printdevice'
  145. 'printheader'
  146. 'printfont'
  147. 'printoptions'
  148. To print only a range of lines, use Visual mode to select the lines and then
  149. type the command: >
  150. v100j:hardcopy
  151. "v" starts Visual mode. "100j" moves a hundred lines down, they will be
  152. highlighted. Then ":hardcopy" will print those lines. You can use other
  153. commands to move in Visual mode, of course.
  154. This also works on Unix, if you have a PostScript printer. Otherwise, you
  155. will have to do a bit more work. You need to convert the text to HTML first,
  156. and then print it from a web browser.
  157. Convert the current file to HTML with this command: >
  158. :TOhtml
  159. In case that doesn't work: >
  160. :source $VIMRUNTIME/syntax/2html.vim
  161. You will see it crunching away, this can take quite a while for a large file.
  162. Some time later another window shows the HTML code. Now write this somewhere
  163. (doesn't matter where, you throw it away later):
  164. >
  165. :write main.c.html
  166. Open this file in your favorite browser and print it from there. If all goes
  167. well, the output should look exactly as it does in Vim. See |2html.vim| for
  168. details. Don't forget to delete the HTML file when you are done with it.
  169. Instead of printing, you could also put the HTML file on a web server, and let
  170. others look at the colored text.
  171. ==============================================================================
  172. *06.6* Further reading
  173. |usr_44.txt| Your own syntax highlighted.
  174. |syntax| All the details.
  175. ==============================================================================
  176. Next chapter: |usr_07.txt| Editing more than one file
  177. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: