4

I'm having trouble configuring Vim on Manjaro:

After setting syntax on at .vimrc file, I make a Prolog program (bug.pl) which shows no syntax highlight:

enter image description here

But after I append /**/ to top of file (then quit and restart vim), highlighting somehow works:

(/**/ is comment in Prolog, same like that of C)

enter image description here

So how do I have highlight without that useless /**/?

DISTRIB_ID=ManjaroLinux

DISTRIB_RELEASE=17.0.1

DISTRIB_CODENAME=Gellivara

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 6 2017 14:13:23)

Update:

Suggested by grodzik, it's about filetype conflict.

I fix it by adding au FileType perl set filetype=prolog to my .vimrc. (Make sure it's AFTER syntax on)

Now I see highlight.

2

This is because by default a file with extension .pl is for Perl files. If you open your file, when there is no /**/ on first line and run command :set filetype?, it will show perl, not prolog.

However, if you add /**/ on the first line, than there is probably some code inside Vim's standard distribution, that detects that and sets file type as prolog - again, check it with :set filetype?

If you know that you won't need perl syntax highlighting, than you can add to .vimrc:

au FileType perl set filetype=prolog

but rember, that this will disable perl file type for good. I think that better way is to prepend a file with that /**/, and let Vim detect the correct file type.

  • I do add au FileType perl set filetype=prolog to my .vimrc, and set filetype? return filetype=prolog. Still I see plain text without highlight. How do I do? – Rahn May 12 '17 at 8:19
  • Do you still have syntax on in your .vimrc? – grodzik May 12 '17 at 8:30
  • 1
    I copy syntax on to the line before au FileType ... then it works – Rahn May 12 '17 at 8:36
1

grodzik has already explained why this occurs, but I disagree with his suggested solution. As he explains, using his autocommand means that it will be impossible ever to use the perl file type. Even if you believe you will never want to view a Perl file, this still seems unnecessarily final.

A gentler solution is to just set the filetype directly for files with .pl extensions. You can then still set the perl filetype manually with the command :set filetype=perl if necessary.

The Vim documentation describes how to do this in :help new-filetype, section C. In short:

Create a file ~/.vim/filetype.vim with the contents:

" my filetype file
if exists("did_load_filetypes")
  finish
endif
augroup filetypedetect
  au! BufRead,BufNewFile *.pl setfiletype prolog
augroup END

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.