11

I have html settings set up in my ~/.vim/ftplugin/html.vim file. I would like these settings to apply to Rhtml files as well. I made a symbolic link with

ln -s ~/.vim/ftplugin/html.vim ~/.vim/ftplugin/Rhtml.vim

Why doesn't this load my html settings when I open a .Rhtml file?

8
  • 2
    I don't know why a symlink doesn't work, but the usual approach for this is to create a rhtml.vim file which uses runtime! ftplugin/html.vim. Here's an example. – Martin Tournoij Sep 19 '17 at 15:33
  • @Carpetsmoker I like the runtime! ftplugin/html.vim idea, but I can't get that to work either. I copied your example to ftplugin/rhtml.vim but it doesn't work. When I open a .Rhtml file, the default vim settings apply. When I try set ft=html, the html settings load. What could I be doing wrong? – ichbinallen Sep 19 '17 at 15:54
  • 1
    Are you sure that the filetype is set correct? What does verbose set filetype? say? – Martin Tournoij Sep 19 '17 at 15:56
  • You mean enter :verbose set filetype as a command from within vim? I get filetype=. This means there is no filetype set for my .Rhtml file, right? – ichbinallen Sep 19 '17 at 16:02
  • Yeah, I meant as a command. How are you setting the filetype? Is it possible you forgot to add a ftdetect script? (like this one?) – Martin Tournoij Sep 19 '17 at 16:04
12

The symlink you've created at ~/.vim/ftplugin/Rhtml.vim tells Vim how to handle files with the RHTML filetype, but you haven't told Vim how to recognise which files should have this filetype.

Instructions for how to do so can be found in the documentation by running the command :help new-filetype.

You can use either the technique described in section A or the one from section C for your purposes. (The former is arguably simpler, but has a couple of disadvantages compared to the latter, which are described in the docs.)

8

Create ~/.vim/ftdetect/SOME_EXTENSION.vim and add this line to it:

autocmd BufNewFile,BufRead *.SOME_EXTENSION set filetype=SOME_FILETYPE

So if you wanted vim to use the filetype html for all files with an .Rhtml extension you would add this to ~/.vim/ftdetect/rhtml.vim:

autocmd BufNewFile,BufRead *.Rhtml set filetype=html
2
  • Thanks for the helpful advice and comments @idbrii! I've updated my answer. – Cory Klein Mar 10 '20 at 17:49
  • 2
    I'm glad to see an answer that spells out how to actually accomplish what the OP wanted! Welcome to Vi and Vim! – D. Ben Knoble Mar 11 '20 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.