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