Enhanced syntax highlighting for C++ using Vim Syntax Extra

Vim ships with the syntax highlighting rules for C and C++. This is a curse in disguise, because those rules are extremely old and they only highlight the keywords. This means that any C or C++ source file looks pretty much like plain text, no matter which color scheme you apply.

Vim Syntax Extra is a plugin that fixes this by adding more syntax rules for C and C++. There is a visible difference only in the syntax highlighting once this plugin is installed. Add its Github URL using any of your favorite Vim plugin manager and restart Vim to enjoy it 🙂

Tried with: Vim 7.4 and Ubuntu 14.04

Advertisements

How to enable syntax highlighting for Strace output in Vim

20150327_strace_vim

Strace output can be written to a log file by using the -o option. This output is typically verbose and I find it helpful if the editor can apply syntax highlighting on it. This gives the system call, its parameters and its output different colors and makes it easy to understand the log.

Vim has support for syntax highlighting of strace output. You can see this in the /usr/share/vim/vim74/syntax/strace.vim file. Vim tries to detect if the file is strace output by looking for an execve or libc pattern in the first line. This logic can be seen in its /usr/share/vim/vim74/scripts.vim file. I have never seen these patterns in the first line in my strace output.

Since Vim does not automatically detect my strace output file, I enable it explicitly using the command: :set filetype=strace

Another trick is to output strace to a file of particular extension, say .strace and look for that file extension to set the strace filetype automatically. If you want this, then add this line to your .vimrc:

autocmd BufRead,BufNewFile *.strace set filetype=strace

Tried with: Vim 7.4, Strace 4.8-1 and Ubuntu 14.04

Syntax highlighting for CUDA in Vim

Problem

Vim has had syntax highlighting support for CUDA source files for a long time now. You can check this: open any .cu file and try the command :set filetype?. You will see that Vim knows that it is a CUDA source file. It applies the syntax highlighting from the file /usr/share/vim/vim74/syntax/cuda.vim.

So, what is the problem? The syntax highlighting done by Vim for CUDA is very very minimal. My CUDA source files look like pages of plain white text in Vim! Also, the comments in the cuda.vim file that it was last updated in 2007. Now that is old!

Solution 1

There is an alternate cu.vim or cuda.vim syntax file for Vim floating around on the web. You can get it, for example, from here.

I replaced the cuda.vim that ships with Vim with this one and found it slightly better. But not by much. It still looks like lots of plain text.

Solution 2

The better solution for me was to just syntax highlight CUDA file as C++ file. This gave the best results, with the highest number of elements in the file being colored when compared to above two methods.

To do this, add this line to your .vimrc:

autocmd BufRead,BufNewFile *.cu set filetype=cpp

Tried with: Vim 7.4 and Ubuntu 14.04

EasyTags plugin for Vim

EasyTags is a fantastic plugin for Vim. It achieves two objectives that are related to each other: automatically index your source code and enhance the syntax highlighting of your source code. These two tasks are related because improving the syntax highlighting of code requires knowledge of what every word means in your code, which is what the tags provide. This adds another IDE-like feature to Vim.

Install

Usage

  • This plugin automatically generates tags for the currently open files using the ctags tool. This is written to ~/.vimtags. The tags are generated automatically when you do not move the cursor for a few milliseconds. Vim generates the CursorHold when you pause typing and EasyTags uses this to do its work. Note that the tags are generated even if your file is not yet saved, since EasyTags is using the buffer, not the file.

  • This plugin also automatically highlights the tags it has indexed. Vim can only highlight the keywords of a language, not the new types or functions you define in it. EasyTags uses the index it has generated and highlights all the tags. So, you should see a lot more of your code in colors.

  • This plugin can be configured to read and update the tags file of your project, instead of using ~/.vimtags. To do this, your Vim should be configured to walk back the directory hierarchy to pick the first tags file it can find. Then EasyTags can be requested to use this tags file for updating, if it is found. I achieve this by adding these two lines to .vimrc:

" Let Vim walk up directory hierarchy from CWD to root looking for tags file
set tags=tags;/
" Tell EasyTags to use the tags file found by Vim
let g:easytags_dynamic_files = 1
  • To generate tags only when a file is saved, add this to .vimrc:
let g:easytags_events = ['BufWritePost']
  • To disable automatic highlighting of tags, add this to .vimrc:
let g:easytags_auto_highlight = 0
  • To explicitly ask EasyTags to index files, use the command :UpdateTags

  • To explicitly ask EasyTags to highlight tags, use the command :HighlightTags

Tried with: Vim 7.4 and Ubuntu 14.04

How to get syntax highlighting for GLSL in Vim

GLSL shaders are typically written in standalone files. To get syntax highlighting for these files in Vim:

  • Make sure that the file has one of these extensions: .glsl, .vert, .frag, .geom, .vs or .fs.

  • Install the Vim-GLSL plugin:

$ cd ~/.vim/bundle
$ git clone https://github.com/tikhomirov/vim-glsl.git

Tried with: Vim 7.4 and Ubuntu 14.04

How to get syntax highlighting for cat using pygments

Some people like to quickly have a look at short source files using cat at the terminal. It would be nice to have syntax highlighting for such a use. Though cat does not support syntax highlighting, it is easy to get a similar experience using Pygments, a syntax highlighter written in Python.

First, install Pygments using pip:

$ sudo pip install Pygments

This installs a pygmentize program, whose -g can be used to request it to guess the type of input file and apply the appropriate syntax highlighting:

$ pygmentize -g foobar.cpp

If you use this regularly, create an alias for it with a name that is easy to remember like ccat.

Tried with: Pygments 1.6, Python 2.7.3 and Ubuntu 12.04

CUDA: Syntax Highlighting in Visual Studio

It is painful to work with CUDA source files (*.cu) in Visual Studio without syntax highlighting. Nvidia provides a usertype.dat file that can be used for this purpose. Here is how:

  1. Install the GPU Computing SDK. Note that this is not the same as the CUDA Toolkit, which is a prerequisite for the SDK. The SDK installs into the %NVSDKCOMPUTE_ROOT% directory. On Windows 7, typically this is C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 3.2
  2. Go to the %NVSDKCOMPUTE_ROOT%\C\doc\syntax_highlighting\visual_studio_8 directory. Use the usertype.dat file that is here and follow the instructions in the Readme.txt file that is here.
  3. Restart Visual Studio.

 

Tried with: CUDA 3.2