Highlight for Qt classes in vim

Maybe some of you like me like to use a sophisticated library for C++ development. My choice is Qt. I really like to have highlighting for most of the classes so here is my ~/.vim/syntax/cpp.vim. This will highlight all of the Qt classes as built in structures of the language, which is really nice. I just copied all of the classes from the Qt’s web site ;) .

UPDATE:
Today Vidas Katinas send me the modified version so i have updated the link. I have also added a checking for a variable. I am also using stl lately so here is the installation guide if you use them both:

mkdir -p ~/.vim/after/syntax/cpp
wget -c http://nikolavp.googlepages.com/cpp.vim -O ~/.vim/after/syntax/cpp/qt.vim
wget -c http://www.vim.org/scripts/download_script.php?src_id=8628 -O ~/.vim/after/syntax/cpp/stl.vim

Now if you want to disable the Qt highlighting just put that somewhere in your vimrc file

let g:qt_syntax=0

If for example you want to disable the syntax for the current file(it is too slow ot whatever) type this

:let g:qt_syntax=0:e

and

:e!

if you have made changes.

8 thoughts on “Highlight for Qt classes in vim

  1. Know of any way to make the indentation work properly for the Qt-isms – like ‘public slots:’, ‘signals:’, ‘Q_OBJECT’?

  2. Yes indeed. I created modified the cpp.vim file that Nikola posted, to include all functions used by Qt. There are over 6800 of them.

    The process itself was a great example of the power vim puts at your fingertips. E.g. I copied/pasted all functions from Trolltech webpage into vim. The format was:

    * function: ObjectThatUsesIt

    over 6800 lines…

    enter vim:
    :%s/:.\+/ /
    :%s/\*/ /
    gg
    :j6800

    and all functions are in the right format, all in one line, neatly separated by spaces.
    Man, people really knew how to write software back in the day!

    Anyway, if I could get in touch with Nikola, I could shoot him the updated version.

    ping me at vidask _at_ gmail _dot_ com

  3. To set the indentation after "Q_OBJECT" put a semicolon:

    class MyObject : public QWidget
    {
    Q_OBJECT;
    void blahblah(int);
    et cetera….
    };

  4. I'm working in the terminal, so I had to adapt this scipt a bit:

    :hi QtObject guifg=lightgreen

    to

    hi QtObject ctermfg=lightgreen guifg=lightgreen

    to make class highlighting work for me.
    Thanks for great scripts!

    • Yes it seems the google link is no longer working. Thanks for the comment, you can find a current version at my github dotfiles repo here

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>