usr_06.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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| Further reading
  13. Next chapter: |usr_07.txt| Editing more than one file
  14. Previous chapter: |usr_05.txt| Set your settings
  15. Table of contents: |usr_toc.txt|
  16. ==============================================================================
  17. *06.1* Switching it on
  18. Syntax highlighting is enabled by default. Nvim will automagically detect the
  19. type of file and load the right syntax highlighting.
  20. ==============================================================================
  21. *06.2* No or wrong colors?
  22. There can be a number of reasons why you don't see colors:
  23. - Your terminal does not support colors.
  24. Vim will use bold, italic and underlined text, but this doesn't look
  25. very nice. You probably will want to try to get a terminal with
  26. colors.
  27. - Your terminal does support colors, but Vim doesn't know this.
  28. Make sure your $TERM setting is correct. For example, when using an
  29. xterm that supports colors: >
  30. setenv TERM xterm-color
  31. <
  32. or (depending on your shell): >
  33. TERM=xterm-color; export TERM
  34. < The terminal name must match the terminal you are using.
  35. - The file type is not recognized.
  36. Vim doesn't know all file types, and sometimes it's near to impossible
  37. to tell what language a file uses. Try this command: >
  38. :set filetype
  39. <
  40. If the result is "filetype=" then the problem is indeed that Vim
  41. doesn't know what type of file this is. You can set the type
  42. manually: >
  43. :set filetype=fortran
  44. < To see which types are available, look in the directory
  45. $VIMRUNTIME/syntax. For the GUI you can use the Syntax menu.
  46. Setting the filetype can also be done with a |modeline|, so that the
  47. file will be highlighted each time you edit it. For example, this
  48. line can be used in a Makefile (put it near the start or end of the
  49. file): >
  50. # vim: syntax=make
  51. < You might know how to detect the file type yourself. Often the file
  52. name extension (after the dot) can be used.
  53. See |new-filetype| for how to tell Vim to detect that file type.
  54. - There is no highlighting for your file type.
  55. You could try using a similar file type by manually setting it as
  56. mentioned above. If that isn't good enough, you can write your own
  57. syntax file, see |mysyntaxfile|.
  58. Or the colors could be wrong:
  59. - The colored text is very hard to read.
  60. Vim guesses the background color that you are using. If it is black
  61. (or another dark color) it will use light colors for text. If it is
  62. white (or another light color) it will use dark colors for text. If
  63. Vim guessed wrong the text will be hard to read. To solve this, set
  64. the 'background' option. For a dark background: >
  65. :set background=dark
  66. < And for a light background: >
  67. :set background=light
  68. < Make sure you put this _before_ the ":syntax enable" command,
  69. otherwise the colors will already have been set. You could do
  70. ":syntax reset" after setting 'background' to make Vim set the default
  71. colors again.
  72. - The colors are wrong when scrolling bottom to top.
  73. Vim doesn't read the whole file to parse the text. It starts parsing
  74. wherever you are viewing the file. That saves a lot of time, but
  75. sometimes the colors are wrong. A simple fix is hitting CTRL-L. Or
  76. scroll back a bit and then forward again.
  77. For a real fix, see |:syn-sync|. Some syntax files have a way to make
  78. it look further back, see the help for the specific syntax file. For
  79. example, |tex.vim| for the TeX syntax.
  80. ==============================================================================
  81. *06.3* Different colors *:syn-default-override*
  82. If you don't like the default colors, you can select another color scheme. In
  83. the GUI use the Edit/Color Scheme menu. You can also type the command: >
  84. :colorscheme evening
  85. "evening" is the name of the color scheme. There are several others you might
  86. want to try out. Look in the directory $VIMRUNTIME/colors.
  87. When you found the color scheme that you like, add the ":colorscheme" command
  88. to your |init.vim| file.
  89. You could also write your own color scheme. This is how you do it:
  90. 1. Select a color scheme that comes close. Copy this file to your own Vim
  91. directory. For Unix, this should work: >
  92. !mkdir -p ~/.config/nvim/colors
  93. !cp $VIMRUNTIME/colors/morning.vim ~/.config/nvim/colors/mine.vim
  94. <
  95. This is done from Vim, because it knows the value of $VIMRUNTIME.
  96. 2. Edit the color scheme file. These entries are useful:
  97. cterm attributes in a color terminal
  98. ctermfg foreground color in a color terminal
  99. ctermbg background color in a color terminal
  100. gui attributes in the GUI
  101. guifg foreground color in the GUI
  102. guibg background color in the GUI
  103. For example, to make comments green: >
  104. :highlight Comment ctermfg=green guifg=green
  105. <
  106. Attributes you can use for "cterm" and "gui" are "bold" and "underline".
  107. If you want both, use "bold,underline". For details see the |:highlight|
  108. command.
  109. 3. Tell Vim to always use your color scheme. Put this line in your |vimrc|: >
  110. colorscheme mine
  111. If you want to see what the most often used color combinations look like, use
  112. this command: >
  113. :runtime syntax/colortest.vim
  114. You will see text in various color combinations. You can check which ones are
  115. readable and look nice.
  116. ==============================================================================
  117. *06.4* With colors or without colors
  118. Displaying text in color takes a lot of effort. If you find the displaying
  119. too slow, you might want to disable syntax highlighting for a moment: >
  120. :syntax clear
  121. When editing another file (or the same one) the colors will come back.
  122. If you want to stop highlighting completely use: >
  123. :syntax off
  124. This will completely disable syntax highlighting and remove it immediately for
  125. all buffers. See |:syntax-off| for more details.
  126. *:syn-manual*
  127. If you want syntax highlighting only for specific files, use this: >
  128. :syntax manual
  129. This will enable the syntax highlighting, but not switch it on automatically
  130. when starting to edit a buffer. To switch highlighting on for the current
  131. buffer, set the 'syntax' option: >
  132. :set syntax=ON
  133. <
  134. ==============================================================================
  135. *06.5* Further reading
  136. |usr_44.txt| Your own syntax highlighted.
  137. |syntax| All the details.
  138. ==============================================================================
  139. Next chapter: |usr_07.txt| Editing more than one file
  140. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: