Re: VIM and Fortran Syntax Highlighting Error when Creating New File

941 views
Skip to first unread message

Ben Fritz

unread,
Sep 26, 2012, 10:34:17 AM9/26/12
to vim...@googlegroups.com
On Tuesday, September 25, 2012 7:12:41 PM UTC-5, Paul Lou wrote:
> Hello all,
>
> First I'd like to thank the VIM team for creating one of the best and most flexible editors in the world.
>
> I just started learning VIM with Fortran, and I realized a syntax coloring problem when creating a Fortran file through
>
> vim example.f
>
> As shown in shot.png, the syntax coloring is only working partially.
>
> However, if I reopen the file, the syntax coloring will be fully working, as shown in shot2.png.

As you guessed, it looks like somehow the wrong filetype/syntax is being loaded. What is the output of the following, before and after reopening?

:verbose set filetype?

What command did you use to open the file? What command did you use to "reopen" the file?

>
> I tried to add
>
> au BufNewFile,BufReadPost *.f,*.for,*.fpp,*f95,*f90 setf fortran
>
> into .vimrc but it doesn't seem to work.

:setfiletype is designed to NOT take any action if the filetype has already been set in a chain of autocmd events. As a workaround, try "setl filetype=fortran" instead of "setf fortran". But this is a workaround. You should figure out why Vim is detecting the wrong filetype, if this is actually the problem.

>
> I am currently running Debian Wheezy, VIM 7.3.
>
> Is it because Vim shipped in Wheezy is not up to date?
>

I don't know. What version of Vim ships with Wheezy? Paste the first few lines of output from the :version command in Vim, up to the "Huge/Big/Normal/Tiny version with/without GUI" line. But any version of 7.3 ought to work fine for what you're trying to do, so I doubt very much it's a version problem.

Paul Lou

unread,
Oct 3, 2012, 12:20:57 PM10/3/12
to vim...@googlegroups.com

Hello Ben,

Thanks for the reply.

Odd enough, when I "create" a file by the command

$ vim test.f

":verbose set filetype" gives

filetype=fortran
Last set from /usr/share/vim/vim73/filetype.vim

But the colorings are off just like snap1.png

The ":verbose" output is also the same if I "reopen" an already existing fortran file by "$ vim existed.f" but the colorings will be correct instead.

So I'd assume Vim chooses the right file type.

Here is the output from :version,

|VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Sep 16 2012 04:12:21)
|Included patches: 1-547
|Modified by pkg-vim-m...@lists.alioth.debian.org
|Compiled by jame...@debian.org

Since Vim does detect the correct file type, then what might be causing Vim to incorrectly render some of the codes (Such as "print *" is rendered correctly but not "program")? I'd assume Vim syntax files do not apply different colorings for newly created files and opening existing files right?

Thank you,
Paul.

Charles Campbell

unread,
Oct 3, 2012, 1:49:12 PM10/3/12
to vim...@googlegroups.com
Paul Lou wrote:
> Hello all,
>
> First I'd like to thank the VIM team for creating one of the best and most flexible editors in the world.
>
> I just started learning VIM with Fortran, and I realized a syntax coloring problem when creating a Fortran file through
>
> vim example.f
>
> As shown in shot.png, the syntax coloring is only working partially.
>
The problem: your code is beginning "too soon"; ie. the "p" in "program"
should be in column 8. Try inserting enough space and you'll see the
error highlighting go away.

The more interesting question is why does subsequent editing show no
error highlighting? That's because some dialects of fortran use
"fixed-source" (ie. code doesn't start until column 8) and some use
"free source" form. The syntax/fortran.vim file examines the first 500
lines for "signs of free source format". So, when you first began
editing, fortran.vim assumed f95 and fixed-source. Subsequent editing
of the file let fortran.vim determined you wanted "free source" form.

You might want to specify what dialect of fortran you're using, too
(choices are: F elf f77 f90 f95). See :help fortran.vim for more
information.

Regards,
C Campbell

Paul Lou

unread,
Oct 10, 2012, 2:35:50 AM10/10/12
to vim...@googlegroups.com, charles.e...@nasa.gov

Thank you for your spread of knowledge Mr. Campbell. I will look into more about this "fixed" and "free" forms and the reasons behind these forms.

Reply all
Reply to author
Forward
0 new messages