Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect comments set for php in vim

Tags:

php

vim

My vim used to auto-continue comments in php. For example:

/* |  <- cursor here

Then, pressing Enter gives me:

/*
 * |  <- cursor here

and again, gives me:

/*
 *
 * |  <- cursor here

etc...

As far as I understand it, this is controlled by the comments and formatoptions options. However, whenever I open a php file now, comments is set to:

s:<!--,m: ,e:-->

I've looked all over my ~/.vim folder, as well as the $VIMRUNTIME folder, and I can't find out where/why this changed, and why the comments option is being set incorrectly.

Here's a link to my .vimrc

http://pastebin.com/f1509ce65

like image 413
rossipedia Avatar asked Jan 14 '10 00:01

rossipedia


3 Answers

Note.

Note that this issue can also arise if one have filetype indent on and plugin on at separate lines in .vimrc:

filetype indent on
filetype plugin on

This causes $VIMRUNTIME/indent/php.vim to be processed before $VIMRUNTIME/ftplugin/php.vim.

The indent/php.vim file resets 'comments', but ftplugin/php.vim does not.


Order of mess:

indent/php.vim get sourced and comments correctly set:

setlocal comments=s1:/*,mb:*,ex:*/,://,:#

Then ftplugin/php.vim get sourced. It again sources ftplugin/html.vim by:

runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim

which result in ftplugin/html.vim being processed and setting:

setlocal commentstring=<!--%s-->
setlocal comments=s:<!--,m:\ \ \ \ ,e:-->

Later on in ftplugin/php.vim the commentstring is reset but not comments:

setlocal commentstring=/*%s*/

Fix:

filetype indent plugin on

" Or
filetype plugin indent on

" Or with correct order:
filetype plugin on
filetype indent on

PS.

In any case plugin should be processed before indent.

To check order of inclusion/processing have a look at :scriptnames.

like image 163
user13500 Avatar answered Nov 03 '22 01:11

user13500


With the default settiings of version 7.3 (patchset 754), I observe the same bug as in your original post:

/**<ENTER>

Expected result:

/**
 * <cursor>

Actual result:

/**
<cursor>

The solution consists of two steps:

  • Add a command that makes vim detect comments (see Issue 75 for vim on Google Code)
  • Add a command that makes vim automatically insert comment continuations (apply the reverse of Vim: can I disable continuation of comments to the next line?)

The modification to my vimrc that takes these two steps into account:

au FileType php setlocal comments=s1:/*,mb:*,ex:*/,://,:#
au FileType php setlocal formatoptions+=cro

Hurrah!

like image 20
Lekensteyn Avatar answered Nov 03 '22 03:11

Lekensteyn


Found it. I had a funky .vim/indent/php.vim file that somehow was screwing it up.

Deleted it, and the functionality I was looking for returned. Thanks tho!

like image 38
rossipedia Avatar answered Nov 03 '22 01:11

rossipedia