Turning on XML syntax highlighting in vim for a non-XML file

7

I'm using vim to browse through some log files (the file extension is .log) which contain a lot of XML.

How can I turn on XML syntax highlighting after opening the file? Of course, I don't actually want to associate XML syntax highlighting with all log files.

Mansoor Siddiqui

Posted 2012-01-27T21:32:02.280

Reputation: 263

Answers

9

The command you want is ":setfiletype". For example:

:setf xml

See ":help :setfiletype".

Heptite

Posted 2012-01-27T21:32:02.280

Reputation: 16 267

Beautiful, this is exactly what I was looking for. Thanks! – Mansoor Siddiqui – 2012-01-27T22:42:14.820

5

You can place this in your ~/.vimrc file as well

au BufNewFile, BufRead *.extension_name set filetype=xml

Replace extension_name with the extension you want, reopen vim, and you will see xml syntax highlight.

RubyFanatic

Posted 2012-01-27T21:32:02.280

Reputation: 151

1

This didn't work for me, this answer did work: autocmd BufEnter *.extension_name :setlocal filetype=xml

– agold – 2015-12-22T16:18:12.617

It should be noted that this will cause all .extension_name files to be treated as the given filetype, which is behavior specifically mentioned as not wanted in the question. Still, potentially useful to others, so no downvote from me. – 8bittree – 2016-04-27T16:28:03.803

1

Expanding on @RubyFanatic answer, what worked for me was (in vim's command mode):

au BufNewFile,BufRead *.extension_name setf xml

Mar Ian

Posted 2012-01-27T21:32:02.280

Reputation: 11