开发者

Vim: How to change text from within an indent script

开发者 https://www.devze.com 2023-01-25 22:25 出处:网络
I recently switched from Eclipse to Vim. I\'m loving it. There are a few hangups I\'m working on, but one of the ones I\'m having lots of trouble with is the PHP doc comments. In eclipse I could type:

I recently switched from Eclipse to Vim. I'm loving it. There are a few hangups I'm working on, but one of the ones I'm having lots of trouble with is the PHP doc comments. In eclipse I could type:

/** [enter]

and the next line would auto fill with

 * 

So I'd have:

/**
 * [comment goes here]

I'm wondering if there's anything like this for vim. It seems there are some plugins to autogenerate doc comments by running a command, but I'd love to hav开发者_C百科e it do them as I'm typing.

I was playing around with the PHP indent script (http://www.vim.org/scripts/script.php?script_id=1120) and I got it to recognize when it's inside of a doc comment block, but I can't figure out how to get it to actually change the text and add a " * " after hitting enter when inside the block.

I've tried what I've seen other plugins do:

let @z = ' * '
put! z

tried this too:

exe 'normal!' '"zgp'

but no luck. Is this not possible from an indent script, and if not, how do I actually get Vim to recognize a doc comment block and act accordingly while I'm typing?

Any help would be greatly appreciated!


No need to mess around with the indentation files. Vim's formatoptions will do this for you and in a variety of languages (not just PHP).

Ensure you have r included in your formatoptions:

:setlocal fo+=r "to set
:set fo? "to query

You can include this in your .vimrc or in .vim/ftplugin/php.vim (if you just want to activate this for PHP).

For more information on formatoptions and file-type plugins, see:

  • :help 'formatoptions'
  • :help fo-table
  • :help ftplugins


Would adding the below code to your vimrc do something similar to what you want?

autocmd BufNewFile,BufRead *.php setlocal formatoptions+=r formatoptions+=o
autocmd BufNewFile,BufRead *.php setlocal comments=s1:/*,mb:*,ex:*/,://,:#

I currently can't quite figure out how to make it work without overriding the <!-- ---> commenting, which this does. I.e. this will break auto-indenting with <!-- --> comments.

Edit. Added ://,:# to comments as Johnsyweb's distribution does.


Try adding this to your vimrc:

let g:PHP_autoformatcomment=1

I'm on a Mac and it seems to be enabled by default. Functions exactly how you stated.

0

精彩评论

暂无评论...
验证码 换一张
取 消