codekingpro/portable-devtools
114k
1*usr_06.txt* For Vim version 9.2. Last change: 2026 Feb 142 3 4 VIM USER MANUAL by Bram Moolenaar5 6 7 Using syntax highlighting8 9 10Black and white text is boring. With colors your file comes to life. This11not only looks nice, it also speeds up your work. Change the colors used for12the different sorts of text. Print your text, with the colors you see on the13screen.14 15|06.1| Switching it on16|06.2| No or wrong colors?17|06.3| Different colors18|06.4| With colors or without colors19|06.5| Printing with colors20|06.6| Further reading21 22 Next chapter: |usr_07.txt| Editing more than one file23 Previous chapter: |usr_05.txt| Set your settings24Table of contents: |usr_toc.txt|25 26==============================================================================27*06.1* Switching it on28 29It all starts with one simple command: >30 31 :syntax enable32 33That should work in most situations to get color in your files. Vim will34automagically detect the type of file and load the right syntax highlighting.35Suddenly comments are blue, keywords brown and strings red. This makes it36easy to overview the file. After a while you will find that black&white text37slows you down!38 39If you always want to use syntax highlighting, put the ":syntax enable"40command in your |vimrc| file.41 42If you want syntax highlighting only when the terminal supports colors, you43can put this in your |vimrc| file: >44 45 if &t_Co > 146 syntax enable47 endif48 49If you want syntax highlighting only in the GUI version, put the ":syntax50enable" command in your |gvimrc| file.51 52==============================================================================53*06.2* No or wrong colors?54 55There 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 look59 very nice. You probably will want to try to get a terminal with60 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 an65 xterm that supports colors: >66 67 setenv TERM xterm-color68<69 or (depending on your shell): >70 71 TERM=xterm-color; export TERM72 73< The terminal name must match the terminal you are using. If it74 still doesn't work, have a look at |xterm-color|, which shows a few75 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 impossible79 to tell what language a file uses. Try this command: >80 81 :set filetype82<83 If the result is "filetype=" then the problem is indeed that Vim84 doesn't know what type of file this is. You can set the type85 manually: >86 87 :set filetype=fortran88 89< To see which types are available, look in the directory90 $VIMRUNTIME/syntax. For the GUI you can use the Syntax menu.91 Setting the filetype can also be done with a |modeline|, so that the92 file will be highlighted each time you edit it. For example, this93 line can be used in a Makefile (put it near the start or end of the94 file): >95 96 # vim: syntax=make97 98< You might know how to detect the file type yourself. Often the file99 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 as104 mentioned above. If that isn't good enough, you can write your own105 syntax file, see |mysyntaxfile|.106 107 108Or 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 black112 (or another dark color) it will use light colors for text. If it is113 white (or another light color) it will use dark colors for text. If114 Vim guessed wrong the text will be hard to read. To solve this, set115 the 'background' option. For a dark background: >116 117 :set background=dark118 119< And for a light background: >120 121 :set background=light122 123< Make sure you put this _before_ the ":syntax enable" command,124 otherwise the colors will already have been set. You could do125 ":syntax reset" after setting 'background' to make Vim set the default126 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 parsing130 wherever you are viewing the file. That saves a lot of time, but131 sometimes the colors are wrong. A simple fix is hitting CTRL-L. Or132 scroll back a bit and then forward again.133 For a real fix, see |:syn-sync|. Some syntax files have a way to make134 it look further back, see the help for the specific syntax file. For135 example, |tex.vim| for the TeX syntax.136 137==============================================================================138*06.3* Different colors *:syn-default-override*139 140If you don't like the default colors, you can select another color scheme. In141the GUI use the Edit/Color Scheme menu. You can also type the command: >142 143 :colorscheme evening144 145"evening" is the name of the color scheme. There are several others you might146want to try out. Look in the directory $VIMRUNTIME/colors.147 148When you found the color scheme that you like, add the ":colorscheme" command149to your |vimrc| file.150 151You could also write your own color scheme. This is how you do it:152 1531. Select a color scheme that comes close. Copy this file to your own Vim154 directory. For Unix, this should work: >155 156 !mkdir ~/.vim/colors157 !cp $VIMRUNTIME/colors/morning.vim ~/.vim/colors/mine.vim158<159 This is done from Vim, because it knows the value of $VIMRUNTIME.160 1612. Edit the color scheme file. These entries are useful:162 163 term attributes in a B&W terminal164 cterm attributes in a color terminal165 ctermfg foreground color in a color terminal166 ctermbg background color in a color terminal167 gui attributes in the GUI168 guifg foreground color in the GUI169 guibg background color in the GUI170 171 For example, to make comments green: >172 173 :highlight Comment ctermfg=green guifg=green174<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 1793. Tell Vim to always use your color scheme. Put this line in your |vimrc|: >180 181 colorscheme mine182 183If you want to see what the most often used color combinations look like, use184this command: >185 186 :runtime syntax/colortest.vim187 188You will see text in various color combinations. You can check which ones are189readable and look nice. These aren't the only colors available to you though.190You can specify #rrggbb hex colors and you can define new names for hex191colors in |v:colornames| like so: >192 193 let v:colornames['mine_red'] = '#aa0000'194 195If you are authoring a color scheme for others to use, it is important196to define these colors only when they do not exist: >197 198 call extend(v:colornames, {'mine_red': '#aa0000'}, 'keep')199 200This allows users of the color scheme to override the precise definition of201that color prior to loading your color scheme. For example, in a |.vimrc|202file: >203 204 runtime colors/lists/css_colors.vim205 let v:colornames['your_red'] = v:colornames['css_red']206 colorscheme yourscheme207 208As a color scheme author, you should be able to rely on some color names for209GUI colors. These are defined in `colors/lists/default.vim`. All such files210found on the 'runtimepath' are loaded each time the colorscheme command is211run. A canonical list is provided by the vim distribution, which should212include all X11 colors (previously defined in rgb.txt).213 214==============================================================================215*06.4* With colors or without colors216 217Displaying text in color takes a lot of effort. If you find the displaying218too slow, you might want to disable syntax highlighting for a moment: >219 220 :syntax clear221 222When editing another file (or the same one) the colors will come back.223 224If you want to stop highlighting completely use: >225 226 :syntax off227 228This will completely disable syntax highlighting and remove it immediately for229all buffers. See |:syntax-off| for more details.230 231 *:syn-manual*232If you want syntax highlighting only for specific files, use this: >233 234 :syntax manual235 236This will enable the syntax highlighting, but not switch it on automatically237when starting to edit a buffer. To switch highlighting on for the current238buffer, set the 'syntax' option: >239 240 :set syntax=ON241<242==============================================================================243*06.5* Printing with colors *syntax-printing*244 245In the MS-Windows version you can print the current file with this command: >246 247 :hardcopy248 249You will get the usual printer dialog, where you can select the printer and a250few settings. If you have a color printer, the paper output should look the251same as what you see inside Vim. But when you use a dark background the252colors will be adjusted to look good on white paper.253 254There are several options that change the way Vim prints:255 'printdevice'256 'printheader'257 'printfont'258 'printoptions'259 260To print only a range of lines, use Visual mode to select the lines and then261type the command: >262 263 v100j:hardcopy264 265"v" starts Visual mode. "100j" moves a hundred lines down, they will be266highlighted. Then ":hardcopy" will print those lines. You can use other267commands to move in Visual mode, of course.268 269This also works on Unix, if you have a PostScript printer. Otherwise, you270will have to do a bit more work. You need to convert the text to HTML first,271and then print it from a web browser.272 273Convert the current file to HTML with this command: >274 275 :TOhtml276 277In case that doesn't work: >278 279 :source $VIMRUNTIME/syntax/2html.vim280 281You will see it crunching away, this can take quite a while for a large file.282Some time later another window shows the HTML code. Now write this somewhere283(doesn't matter where, you throw it away later):284>285 :write main.c.html286 287Open this file in your favorite browser and print it from there. If all goes288well, the output should look exactly as it does in Vim. See |2html.vim| for289details. Don't forget to delete the HTML file when you are done with it.290 291Instead of printing, you could also put the HTML file on a web server, and let292others look at the colored text.293 294==============================================================================295*06.6* Further reading296 297|usr_44.txt| Your own syntax highlighted.298|syntax| All the details.299 300==============================================================================301 302Next chapter: |usr_07.txt| Editing more than one file303 304Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl:305 