6

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?

  • 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
7

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.

| improve this answer | |
4

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
| improve this answer | |
  • 2
    That will set the syntax to be html, but won't set other filetype-related stuff (vim won't know how to indent, plugins won't know the current file uses tags, etc). Instead, you should setfiletype html. That should change the syntax and setup other filetype stuff. It works to add it to your vimrc, but that can cause weird bugs if you :source your vimrc because you'll set up multiple autocmds. Instead, wrap it in a group or put it in ~/.vim/ftdetect/rhtml.vim. (See :help new-filetype for a walkthrough.) – idbrii Mar 10 at 17:46
  • Thanks for the helpful advice and comments @idbrii! I've updated my answer. – Cory Klein Mar 10 at 17:49
  • 1
    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 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.