One of the things I like about Vim is its support for syntax highlighting and folding. Especially when reviewing or editing config-files, the coloring is a true relieve. (Yes, I am aware that emacs can do this much better, nicer, easier and with a lot more Meta-Control sequences.)

Googling for a syntax file which would color the Cisco configuration files for me didn’t get me very far. So I decided to run my own. Here is the result:

(Public IPs in the screenshot are masked by 0′s)

The goal was to aid in reading a config file, not to provide error-checking when writing one. The script checks for often used tokens and colors them. The recognized tokens are:

  • Comments (start with an ! mark)
  • IP addresses (only IPv4 currently)
  • Interface names. All variants that I came across are included, but the list will certainly be incomplete.

Besides tokens, the script also checks for groups of commands to fold them together:

  • username statements
  • ip host statements
  • interface statements (all interfaces folded together; then each interface subfolded)
  • router statements (all routing protocols folded together; then each protocol subfolded)
  • ip route statements
  • ip access-list statements and old access-list statements (all access-lists folded together; then each access-list subfolded)
  • route-map statements (all route-maps folded together; then each route-map subfolded)
  • crypto isakmp statements
  • crypto isakmp key statements
  • crypto ipsec transform-set statements
  • crypto map statements

Update 2009-07-11

Added some minor improvements to the config-script. I also wrote a seperate script to fold routing tables (sh ip route).

11 Comments

  1. Guest says:

    How do I actually use it?

    I copied the script into .vim/plugin/cisco.vim. Tried couple of times with different versions of vim (7.1.138 ), gvim and it does not seem to work?? Any ideas?

  2. Niobos says:

    I (have to) use Windows, so I’ll start out giving the GVim for-windows explanation:
    I placed the file in my HOME-drive (being H: on my machine) in H:\vimfiles\after\syntax\ciscoconfig.vim
    For *nix based setups, it’ll fit in ~/.vim/syntax/ciscoconfig.vim

    I didn’t configure vim to automatically detect the filetype, so I manualy type “:set ft=ciscoconfig” whenever I want to. The reason behind this is that most of the config-files resides somewhere in a bigger log-file.
    If you want Gvim to automatically detect the file, see this topic: http://vimdoc.sourceforge.net/htmldoc/filetype.html#new-filetype

  3. Niobos says:

    Post Update: added some more crypto-related syntax-detection

  4. Chris says:

    Hi,
    I get an error on line 21: Error 401 and 475
    :-(

  5. Niobos says:

    During the copy paste, somehow the ATM-line fell out. I corrected that.
    Could you try again?

  6. Espen Nilsen says:

    Oh, this is perfect! Nice work!

  7. Broeisi says:

    Niobos,

    This looks great, but in vim 7.0 whenever is invoke this colorscheme the interface statements and ip route statements disappears.
    The class map and policy map also doesn’t colour.

    Can you assist maybe?

    Cheers,

    Broeisi

  8. Niobos says:

    Broeisi,

    I’m not sure what you mean by “disappear”. Could it be the Folding taking place? (put the cursor on the line and type “zR”, without quotes and with correct capitalization)

    Class maps and policy maps don’t color because I didn’t write the rules for them yet. If you send me a (piece of) a config file with a bunch of class/policy maps, I’ll try to get these colored/folded as well.

  9. Broeisi says:

    Niobos,

    I will send you a piece of a config file with class/policy maps.
    Better… if you want I can sent you a whole config and that way you (and I :) ) can color the whole cisco config.

    I was about the learn how to do syntax highlighting in vim, because lots of colleagues wanted such syntax highlighting.

    Sent me your email at my email address please..

    That way we can communicate better.

    P.S. the “zR” didn’t help….. but is this syntax file for vim or gvim?… I understand that there is a slight difference between a syntax file for the two.

  10. Niobos says:

    The syntax file should work for both vim and gvim, although I only tested it in gvim.

    You should have received an email to arrange for the config file.

  11. Niobos says:

    Post Update: added folding of class-map’s and policy-map’s

Leave a Reply