--
*syntax.txt* For Vim version 9.1. Last change: 2024 Oct 13 VIM REFERENCE MANUAL by Bram Moolenaar Syntax highlighting *syntax* *syntax-highlighting* *coloring* Syntax highlighting enables Vim to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesn't parse the whole file (to keep it fast), so the highlighting has its limitations. Lexical highlighting might be a better name, but since everybody calls it syntax highlighting we'll stick with that. Vim supports syntax highlighting on all terminals. But since most ordinary terminals have very limited highlighting possibilities, it works best in the GUI version, gvim. In the User Manual: |usr_06.txt| introduces syntax highlighting. |usr_44.txt| introduces writing a syntax file. 1. Quick start |:syn-qstart| 2. Syntax files |:syn-files| 3. Syntax loading procedure |syntax-loading| 4. Converting to HTML |2html.vim| 5. Syntax file remarks |:syn-file-remarks| 6. Defining a syntax |:syn-define| 7. :syntax arguments |:syn-arguments| 8. Syntax patterns |:syn-pattern| 9. Syntax clusters |:syn-cluster| 10. Including syntax files |:syn-include| 11. Synchronizing |:syn-sync| 12. Listing syntax items |:syntax| 13. Colorschemes |color-schemes| 14. Highlight command |:highlight| 15. Linking groups |:highlight-link| 16. Cleaning up |:syn-clear| 17. Highlighting tags |tag-highlight| 18. Window-local syntax |:ownsyntax| 19. Color xterms |xterm-color| 20. When syntax is slow |:syntime| {Vi does not have any of these commands} Syntax highlighting is not available when the |+syntax| feature has been disabled at compile time. ============================================================================== 1. Quick start *:syn-qstart* *:syn-enable* *:syntax-enable* This command switches on syntax highlighting: > :syntax enable What this command actually does is to execute the command > :source $VIMRUNTIME/syntax/syntax.vim If the VIM environment variable is not set, Vim will try to find the path in another way (see |$VIMRUNTIME|). Usually this works just fine. If it doesn't, try setting the VIM environment variable to the directory where the Vim stuff is located. For example, if your syntax files are in the "/usr/vim/vim82/syntax" directory, set $VIMRUNTIME to "/usr/vim/vim82". You must do this in the shell, before starting Vim. This command also sources the |menu.vim| script when the GUI is running or will start soon. See |'go-M'| about avoiding that. *:syn-on* *:syntax-on* The `:syntax enable` command will keep most of your current color settings. This allows using `:highlight` commands to set your preferred colors before or after using this command. If you want Vim to overrule your settings with the defaults, use: > :syntax on < *:hi-normal* *:highlight-normal* If you are running in the GUI, you can get white text on a black background with: > :highlight Normal guibg=Black guifg=White For a color terminal see |:hi-normal-cterm|. For setting up your own colors syntax highlighting see |syncolor|. NOTE: The syntax files on MS-Windows have lines that end in. The files for Unix end in . This means you should use the right type of file for your system. Although on MS-Windows the right format is automatically selected if the 'fileformats' option is not empty. NOTE: When using reverse video ("gvim -fg white -bg black"), the default value of 'background' will not be set until the GUI window is opened, which is after reading the |gvimrc|. This will cause the wrong default highlighting to be used. To set the default value of 'background' before switching on highlighting, include the ":gui" command in the |gvimrc|: > :gui " open window and set default for 'background' :syntax on " start highlighting, use 'background' to set colors NOTE: Using ":gui" in the |gvimrc| means that "gvim -f" won't start in the foreground! Use ":gui -f" then. *g:syntax_on* You can toggle the syntax on/off with this command: > :if exists("g:syntax_on") | syntax off | else | syntax enable | endif To put this into a mapping, you can use: > :map :if exists("g:syntax_on") \ syntax off \ else \ syntax enable \ endif [using the |<>| notation, type this literally] Details: The ":syntax" commands are implemented by sourcing a file. To see exactly how this works, look in the file: command file ~ :syntax enable $VIMRUNTIME/syntax/syntax.vim :syntax on $VIMRUNTIME/syntax/syntax.vim :syntax manual $VIMRUNTIME/syntax/manual.vim :syntax off $VIMRUNTIME/syntax/nosyntax.vim Also see |syntax-loading|. NOTE: If displaying long lines is slow and switching off syntax highlighting makes it fast, consider setting the 'synmaxcol' option to a lower value. ============================================================================== 2. Syntax files *:syn-files* The syntax and highlighting commands for one language are normally stored in a syntax file. The name convention is: "{name}.vim". Where {name} is the name of the language, or an abbreviation (to fit the name in 8.3 characters, a requirement in case the file is used on a DOS filesystem). Examples: c.vim perl.vim java.vim html.vim cpp.vim sh.vim csh.vim The syntax file can contain any Ex commands, just like a vimrc file. But the idea is that only commands for a specific language are included. When a language is a superset of another language, it may include the other one, for example, the cpp.vim file could include the c.vim file: > :so $VIMRUNTIME/syntax/c.vim The .vim files are normally loaded with an autocommand. For example: > :au Syntax c runtime! syntax/c.vim :au Syntax cpp runtime! syntax/cpp.vim These commands are normally in the file $VIMRUNTIME/syntax/synload.vim. MAKING YOUR OWN SYNTAX FILES *mysyntaxfile* When you create your own syntax files, and you want to have Vim use these automatically with ":syntax enable", do this: 1. Create your user runtime directory. You would normally use the first item of the 'runtimepath' option. Example for Unix: > mkdir ~/.vim 2. Create a directory in there called "syntax". For Unix: > mkdir ~/.vim/syntax 3. Write the Vim syntax file. Or download one from the internet. Then write it in your syntax directory. For example, for the "mine" syntax: > :w ~/.vim/syntax/mine.vim Now you can start using your syntax file manually: > :set syntax=mine You don't have to exit Vim to use this. If you also want Vim to detect the type of file, see |new-filetype|. If you are setting up a system with many users and you don't want each user to add the same syntax file, you can use another directory from 'runtimepath'. ADDING TO AN EXISTING SYNTAX FILE *mysyntaxfile-add* If you are mostly satisfied with an existing syntax file, but would like to add a few items or change the highlighting, follow these steps: 1. Create your user directory from 'runtimepath', see above. 2. Create a directory in there called "after/syntax". For Unix: > mkdir -p ~/.vim/after/syntax 3. Write a Vim script that contains the commands you want to use. For example, to change the colors for the C syntax: > highlight cComment ctermfg=Green guifg=Green 4. Write that file in the "after/syntax" directory. Use the name of the syntax, with ".vim" added. For our C syntax: > :w ~/.vim/after/syntax/c.vim That's it. The next time you edit a C file the Comment color will be different. You don't even have to restart Vim. If you have multiple files, you can use the filetype as the directory name. All the "*.vim" files in this directory will be used, for example: ~/.vim/after/syntax/c/one.vim ~/.vim/after/syntax/c/two.vim REPLACING AN EXISTING SYNTAX FILE *mysyntaxfile-replace* If you don't like a distributed syntax file, or you have downloaded a new version, follow the same steps as for |mysyntaxfile| above. Just make sure that you write the syntax file in a directory that is early in 'runtimepath'. Vim will only load the first syntax file found, assuming that it sets b:current_syntax. NAMING CONVENTIONS *group-name* *{group-name}* *E669* *W18* A syntax group name is to be used for syntax items that match the same kind of thing. These are then linked to a highlight group that specifies the color. A syntax group name doesn't specify any color or attributes itself. The name for a highlight or syntax group must consist of ASCII letters, digits, underscores, dots, or hyphens. As a regexp: "[a-zA-Z0-9_.-]*". However, Vim does not give an error when using other characters. The maximum length of a group name is about 200 bytes. *E1249* To be able to allow each user to pick their favorite set of colors, there must be preferred names for highlight groups that are common for many languages. These are the suggested group names (if syntax highlighting works properly you can see the actual color, except for "Ignore"): *Comment any comment *Constant any constant String a string constant: "this is a string" Character a character constant: 'c', '\n' Number a number constant: 234, 0xff Boolean a boolean constant: TRUE, false Float a floating point constant: 2.3e10 *Identifier any variable name Function function name (also: methods for classes) *Statement any statement Conditional if, then, else, endif, switch, etc. Repeat for, do, while, etc. Label case, default, etc. Operator "sizeof", "+", "*", etc. Keyword any other keyword Exception try, catch, throw *PreProc generic Preprocessor Include preprocessor #include Define preprocessor #define Macro same as Define PreCondit preprocessor #if, #else, #endif, etc. *Type int, long, char, etc. StorageClass static, register, volatile, etc. Structure struct, union, enum, etc. Typedef A typedef *Special any special symbol SpecialChar special character in a constant Tag you can use CTRL-] on this Delimiter character that needs attention SpecialComment special things inside a comment Debug debugging statements *Underlined text that stands out, HTML links *Ignore left blank, hidden |hl-Ignore| *Error any erroneous construct *Todo anything that needs extra attention; mostly the keywords TODO FIXME and XXX *Added added line in a diff *Changed changed line in a diff *Removed removed line in a diff The names marked with * are the preferred groups; the others are minor groups. For the preferred groups, the "syntax.vim" file contains default highlighting. The minor groups are linked to the preferred groups, so they get the same highlighting. You can override these defaults by using ":highlight" commands after sourcing the "syntax.vim" file. Note that highlight group names are not case sensitive. "String" and "string" can be used for the same group. The following names are reserved and cannot be used as a group name: NONE ALL ALLBUT contains contained *hl-Ignore* When using the Ignore group, you may also consider using the conceal mechanism. See |conceal|. ============================================================================== 3. Syntax loading procedure *syntax-loading* This explains the details that happen when the command ":syntax enable" is issued. When Vim initializes itself, it finds out where the runtime files are located. This is used here as the variable |$VIMRUNTIME|. ":syntax enable" and ":syntax on" do the following: Source $VIMRUNTIME/syntax/syntax.vim | +- Clear out any old syntax by sourcing $VIMRUNTIME/syntax/nosyntax.vim | +- Source first syntax/synload.vim in 'runtimepath' | | | +- Setup the colors for syntax highlighting. If a color scheme is | | defined it is loaded again with ":colors {name}". Otherwise | | ":runtime! syntax/syncolor.vim" is used. ":syntax on" overrules | | existing colors, ":syntax enable" only sets groups that weren't | | set yet. | | | +- Set up syntax autocmds to load the appropriate syntax file when | | the 'syntax' option is set. *synload-1* | | | +- Source the user's optional file, from the |mysyntaxfile| variable. | This is for backwards compatibility with Vim 5.x only. *synload-2* | +- Do ":filetype on", which does ":runtime! filetype.vim". It loads any | filetype.vim files found. It should always Source | $VIMRUNTIME/filetype.vim, which does the following. | | | +- Install autocmds based on suffix to set the 'filetype' option | | This is where the connection between file name and file type is | | made for known file types. *synload-3* | | | +- Source the user's optional file, from the *myfiletypefile* | | variable. This is for backwards compatibility with Vim 5.x only. | | *synload-4* | | | +- Install one autocommand which sources scripts.vim when no file | | type was detected yet. *synload-5* | | | +- Source $VIMRUNTIME/menu.vim, to setup the Syntax menu. |menu.vim| | +- Install a FileType autocommand to set the 'syntax' option when a file | type has been detected. *synload-6* | +- Execute syntax autocommands to start syntax highlighting for each already loaded buffer. Upon loading a file, Vim finds the relevant syntax file as follows: Loading the file triggers the BufReadPost autocommands. | +- If there is a match with one of the autocommands from |synload-3| | (known file types) or |synload-4| (user's file types), the 'filetype' | option is set to the file type. | +- The autocommand at |synload-5| is triggered. If the file type was not | found yet, then scripts.vim is searched for in 'runtimepath'. This | should always load $VIMRUNTIME/scripts.vim, which does the following. | | | +- Source the user's optional file, from the *myscriptsfile* | | variable. This is for backwards compatibility with Vim 5.x only. | | | +- If the file type is still unknown, check the contents of the file, | again with checks like "getline(1) =~ pattern" as to whether the | file type can be recognized, and set 'filetype'. | +- When the file type was determined and 'filetype' was set, this | triggers the FileType autocommand |synload-6| above. It sets | 'syntax' to the determined file type. | +- When the 'syntax' option was set above, this triggers an autocommand | from |synload-1| (and |synload-2|). This find the main syntax file in | 'runtimepath', with this command: | runtime! syntax/ .vim | +- Any other user installed FileType or Syntax autocommands are triggered. This can be used to change the highlighting for a specific syntax. ============================================================================== 4. Conversion to HTML *2html.vim* *convert-to-HTML* 2html is not a syntax file itself, but a script that converts the current window into HTML. Vim opens a new window in which it builds the HTML file. After you save the resulting file, you can view it with any browser. The colors should be exactly the same as you see them in Vim. With |g:html_line_ids| you can jump to specific lines by adding (for example) #L123 or #123 to the end of the URL in your browser's address bar. And with |g:html_dynamic_folds| enabled, you can show or hide the text that is folded in Vim. You are not supposed to set the 'filetype' or 'syntax' option to "2html"! Source the script to convert the current file: > :runtime! syntax/2html.vim < Many variables affect the output of 2html.vim; see below. Any of the on/off options listed below can be enabled or disabled by setting them explicitly to the desired value, or restored to their default by removing the variable using |:unlet|. Remarks: - Some truly ancient browsers may not show the background colors. - From most browsers you can also print the file (in color)! - The latest TOhtml may actually work with older versions of Vim, but some features such as conceal support will not function, and the colors may be incorrect for an old Vim without GUI support compiled in. Here is an example how to run the script over all .c and .h files from a Unix shell: > for f in *.[ch]; do gvim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" $f; done < *g:html_start_line* *g:html_end_line* To restrict the conversion to a range of lines, use a range with the |:TOhtml| command below, or set "g:html_start_line" and "g:html_end_line" to the first and last line to be converted. Example, using the last set Visual area: > :let g:html_start_line = line("'<") :let g:html_end_line = line("'>") :runtime! syntax/2html.vim < *:TOhtml* :[range]TOhtml The ":TOhtml" command is defined in a standard plugin. This command will source |2html.vim| for you. When a range is given, this command sets |g:html_start_line| and |g:html_end_line| to the start and end of the range, respectively. Default range is the entire buffer. If the current window is part of a |diff|, unless |g:html_diff_one_file| is set, :TOhtml will convert all windows which are part of the diff in the current tab and place them side-by-side in a