Syntax highlighting allows text of specific file types to be color coded for readability. Note the additional links and reference to this topic: Overview:Syntax files (file type description files)Each file describes a different file type (.txt, .conf, .http, etc.), what words and variables to look for, and how to color them. You can download additional or custom syntax files for any filetype. Syntax files should be located in one of these paths: $HOME/.vim/syntax/ /usr/share/vim/vim74/syntax/ Using a prebuilt highlighting recipe:You can download text highlighting recipes and add them to your repository to use. To do this do the following:
" Special Configs au BufNewFile,BufRead *.conf set filetype=ciscoacl The challenge is if you want more then one file with that extension. (for example proftpd.conf* uses an apachestyle formater.) I have not figured this out just yet. Making your own recipe:I was looking for a simple highlighting syntax that would color code comments but nothing else. With limited google searching, I decided to try this on my own, and realised that it's really easy. I started with current recipe for log files called txt.vim, made a few changes, and got what I was looking for.Note: sync, and HiLink are modifications in the txt.vim file. syn region:The "syn" region (where every line starts with the Within the syntax file are a bunch of ways to match text or strings of text and assign a type of formatting. Note that the syntax only assigns a "flag" for the area of text. The color file in use is what defines how that area of text should be colored. Region:A region is defined as having some kind of defined start and end, but within it can be anything. The following statement says to assign the "txtComment" to any string within two brackets "( )" syn region txtComment start="(" end=")" contains=@txtContains,txtCite,@txtAlwaysContains Match: A match is any string that matches a regex. The following statement finds any string with <add>{any_amount_of_spaces}: and assigns it as a "txtChangelogs"syn match txtChangelogs "\<add\>\s*:" contains=txtOperator Keyword: A keyword is simply any string of characters. The following statement finds any of the following words "todo" "fixme" "xxx" and "note", and assignes them as "txtTodo". syn keyword txtTodo todo fixme xxx note The first line in the example below is for anything that starts with a bang " ! ". Anything in that "oneline" should be color coded however region "txtBang " is defined.syn region txtBang start="^!" end="$" oneline
HiLink:The "HiLink" section are the lines that start with " HiLink " (or link highlighting). Here we take a region like "txtBang ", and define a color pattern to it like "LineNr "Aliases: The search matches can use their own variable names for the text that they match (like txtChangelog, or txtTodo), but then they need to make sure that they are "reassigned" to globally used names that would be found in the color files. This is done with the HiLink command. The following converts the variables used above to more globally recognized variable names (like Comment, Keyword, and Todo). HiLink txtComment Comment HiLink txtChangelogs Keyword HiLink txtTodo Todo HiLink txtBang LineNr HiLink txtPound Special Where the "Type" format is already defined in vim. You can view all of the formatting types by either selecting Syntax > Highlight test in gvim, I also didn't like the coloring it did to numbers so i marked out some of the lines in txt.vim: "syn match txtNumber "\d" "syn match txtOperator "[~\-_+*<>\[\]{}=|#@$%&\\/:&\^\.,!?]" Filetype (pointer file)Vim looks to the configuration file filetype.vim to match file extensions to syntax files. filetype.vim files can be found in one of the following paths: /Applications/MacVim.app/Contents/Resources/vim/runtime/filetype.vim ~/.vimrc For mac's, the filetype file gets overwritten, so you can also update your .vimrc file with these changes. (Whatever you're entering below that was supposed to go in the filetype file can also go into the .vimrc file. Updating Filetype:Don't forget to also update the filetype.vim file as noted above in section 1.4. For this modification, I simply added the following:" TXT - Basic Text file
au BufNewFile,BufReadPost *.txt setf txt colors filesOverview:The colors files are lists of predefined color templates that you can choose from for the look-and-feel of your vim terminal. a color file can be chosen in the terminal by entering in :colorscheme <tab> :colorscheme elflord There are a few color files that come standard with all vim deployments, but you can also download others and install them. One source of color files is http://vimcolorschemetest.googlecode.com/svn/html/index-pl.html To see the current color file that you are using in your terminal, enter in: :echo g:colors_name windows: C:\Program Files\vim\vim72\colors\ unix: ~/.vim/colors Example:As an example of a color file, lets look at some sections of the elflord.vim file. First we start with the color definitions. This defines a name of a type of text, and how that text should be modified. This includes the formatting of the text (bold/underline/etc), the font color, and the color of the background to the font. It also includes the ability to have this look different depending on the type of a terminal (black/white, color terminal, or color GUI-terminal.) let g:colors_name = "elflord"
Next we have a section of aliases. After the "hi link" (highlight link) constant, is the name of the 'other' highlight phrase, and its replacement that is defined within this document. For example, if a syntax file says that a piece of text should be colored as a " number ", then this color file will color it as a "constant ". hi link String Constant hi link Character Constant hi link Number Constant hi link Boolean Constant hi link Float Number hi link Conditional Repeat hi link Label Statement hi link Keyword Statement hi link Exception Statement hi link Include PreProc hi link Define PreProc hi link Macro PreProc hi link PreCondit PreProc hi link StorageClass Type hi link Structure Type hi link Typedef Type hi link Tag Special hi link SpecialChar Special hi link Delimiter Special hi link SpecialComment Special hi link Debug Special References: |