Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: How to change text from within an indent script

Tags:

vim

editor

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 have 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!

like image 212
andrew Avatar asked Nov 21 '10 01:11

andrew


People also ask

How do I change the indentation in Vim?

Fix indentation in the whole fileStart in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.

How do I turn off auto-indent in Vim?

To turn off autoindent when you paste code, there's a special "paste" mode. Then paste your code. Note that the text in the tooltip now says -- INSERT (paste) -- . After you pasted your code, turn off the paste-mode, so that auto-indenting when you type works correctly again.

What is smart indent in Vim?

autoindent essentially tells vim to apply the indentation of the current line to the next (created by pressing enter in insert mode or with O or o in normal mode. smartindent reacts to the syntax/style of the code you are editing (especially for C). When having it on you also should have autoindent on.

What is paste mode Vim?

In Python, code blocks like loops are denoted using text indentation. To avoid this from happening, you can use Vim's paste mode. When you enable paste mode, Vim will not auto-indent any text that you paste. To enable paste mode, follow this process: In Vim, ensure you are command mode by hitting the Esc key.


1 Answers

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
like image 96
Johnsyweb Avatar answered Oct 23 '22 08:10

Johnsyweb