3

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?

3
  • 1
    what is the filetype of the left-hand file? can you find a syntax file for it at vim.org? Commented Jul 24, 2010 at 14:41
  • screenshot? or at least the file names? Commented 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. Commented Jul 25, 2010 at 14:57

6 Answers 6

8

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()
Sign up to request clarification or add additional context in comments.

Comments

3

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

This works too, thanks Matthew. The solution given by "too much php" works of course, but I think this is a good practice and is independent of the file extension.
0

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

I'm afraid not. The filetype of the left-hand-side file will <rhs-filename.svn-base>, i.e. it is dependent on the right-hand-side filename.
I see what you mean, but I suggested you "force vim's hand" into understanding the left hand side is a different filetype. I did this: echo "import time" > 1.py echo "import time" > 2.cpp now on left handside typing :set filetype shows: filetype=python on rhs it shows: filetype=cpp but I can force the rhs by using: :set filetype=python hope that helps.
0

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.

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

0

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.