This blog post gives a simple way to use Vim with svn diff. Unfortunately, Vim doesn't understand the left-hand-side filetype so there's no syntax highlighting. What's the simplest/best way of solving this?
-
1what is the filetype of the left-hand file? can you find a syntax file for it at vim.org?michaelmichael– michaelmichael2010-07-24 14:41:50 +00:00Commented Jul 24, 2010 at 14:41
-
screenshot? or at least the file names?Dummy00001– Dummy000012010-07-24 23:50:41 +00:00Commented Jul 24, 2010 at 23:50
-
e.g: it's as if vimdiff was invoked with the following command vimdiff .svn/text-base/foo.php.svn-base foo.php so you see, vim fails to recognize the filetype of the lhs file as php because of the different extension.user400978– user4009782010-07-25 14:57:42 +00:00Commented Jul 25, 2010 at 14:57
6 Answers
Add the following code to ~/.vim/filetype.vim:
" only load filetypes once
if exists("did_load_filetypes")
finish
endif
augroup filetypedetect
" when BufRead or BufNewFile event is triggered, pop off the .svn-base extension and
" manually restart filetype autocommands
autocmd! BufRead *.svn-base execute 'doautocmd filetypedetect BufRead ' . expand('%:r')
autocmd! BufNewFile *.svn-base execute 'doautocmd filetypedetect BufNewFile ' . expand('%:r')
augroup END
As the comments state, this code will pick up .svn-base files and try to figure out what the filetype would be without that extension on the end.
:help new-filetype :help :doautocmd :help expand()
Comments
When I use restructured text within a file with the .txt suffix, by default, vim won't highlight the text. So I put this at the top of my file:
.. vim: set filetype=rst :
And then vim does all the cute syntax highlighting.
Try adding a modeline to the first line of your files and then see if vim does what you want when you do a diff.
Incidentally, that's my blog you linked to! Can I get a prize?
1 Comment
a quick search for "vim set filetype" gave me this link, which shows you how to make this work "forever" by changing .vimrc a bit. If you're just stuck with a file for once, and you know its "correct" filetype, you can try (from the left hand side file):
:set filetype=XXX
where XXX is the appropriate format (e.g. python, c, etc)
a look at
:help filteype
might also help a bit
2 Comments
it's as if vimdiff was invoked with the following command
vimdiff .svn/text-base/foo.php.svn-base foo.phpso you see, vim fails to recognize the filetype of the lhs file as php because of the different extension.
No proper solution is known to me. I use something like this in different scenario, but that only fixes the syntax highlighting:
au BufReadPost *.php.svn-base set syntax=php
You can also try that:
au FileType *.php.svn-base set filetype=php
but I can't test that as I have :filetype disabled. That should overwrite file type detection for the .php.svn-base files and map it to the desired php. I have checked that it overrides the 'filetype' and 'syntax': :set filetype? and :set syntax? produce expected output.
Comments
Building on too much php's answer, this works even when the file extension is not in the svn filename. Assuming you run vimdiff with svn-base as the first argument and the working copy as the second:
autocmd! BufRead *.svn-base if &diff && argc() == 2 |
\ execute 'doautocmd filetypedetect BufRead '.argv(1) |
\ endif
autocmd! BufNewFile *.svn-base if &diff && argc() == 2 |
\ execute 'doautocmd filetypedetect BufNewFile '.argv(1) |
\ endif
Comments
Using GVim 8.0 as the Diff Viewer for TortoiseSVN 1.9.7 on Windows 10, I found that the GVim recognises the php file extension correctly and displayed the syntax highlighting perfectly.