Vim: How to change text from within an indent script - vim

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!

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.

Related

What is the correct way in Vim to autocommand ":highlight" after a buffer is loaded?

Due to various reasons, I run Vim at sixteen-colors, synced w/ my terminal's colors. In a recent Vim update, I've had to rework my "~/.vimrc" completely to get it back into running order on Linux.
Initially I was shocked to find that this simple line did not work (even w/ "syntax on" preceding it):
:highlight Comment ctermfg=White
I'm also using a "LineNr" ctermfg. No matter where I placed/stacked the "Comment" ctermfg, it didn't work, or interfered w/ everything else sourcing correctly (ie, placed in the same line w/ "LineNr"). However, I found that calling "Comment" after a buffer had loaded would make the comments appear as intended.
I am new to autocmd in Vim (and want to know how it works, anyways). Is there an "autocmd" call that I can have in my "~/.vimrc" that will run the aforementioned line of code immediately after a buffer has loaded?
I have tried several iterations (BufWritePre, BufWritePost, etc.) and been unsuccessful. This was my previous attempt:
autocmd BufWinPost * :highlight Comment ctermfg=white
Don't resort to :autocmd without reason; search harder for the root cause!
Your description lacks specifics; I guess your chosen colorscheme (or a plugin, but no sane plugin should interfere with the default highlightings) overrides your custom one for Comment. You can check who defined this via
:verbose highlight Comment
If this points to your colorscheme, you simply need to execute your :highlight command after it. For this, you need to understand :help initialization, and maybe check the output of :scriptnames. If you have a :colorscheme foo command in your ~/.vimrc, it should be as simple as putting the :highlight command after it.
You do need an :autocmd if you switch colorschemes on the fly, as most colorschemes override the basic Comment definition. The correct event and pattern for that would be ColorScheme *
If I do a quick :h autocmd-events, I find the the event BufWinPost does not exist. I think, you want BufWinEnter instead. The autocmd you wrote should work, except for the :. HTH

Disable autoindent in vim

I use vim since almost 20 years and recently someone told me about the usage of = key for indenting a block of code. Sometimes I try a new vim key but stop using it because it isn't doing exactly what I want or I just don't need the feature. But in this case I find its a nice feature.
So I downloaded vim script 1120 (PHP-correct-Indenting) and installed it. My .vimrc contains:
filetype indent on
set smartindent
Now I can use = to indent a visually marked code block.
But I do NOT want vim to automatically indent code while I am typing. This is just irritating me as I am usually doing indentation myself and I am very much used to it ...
So how can I stop vim from automatically indenting my code while typing but can still continue to use = for indenting a visually marked block of text.
PS: Use hjkl for moving around in vim. It will make you about 1.5 times faster :)
My complete vimrc:
syntax on
set tabstop=3
set shiftwidth=3
execute pathogen#infect()
filetype indent on
set smartindent
Commenting the last two lines stops autoindenting but also using "=" does not use the mentioned vim script anymore
The plugin sets 'indentexpr', which controls both explicit reindenting via = as well as indenting-as-you-type. Fortunately, you can control the triggering of the latter via the 'indentkeys' option, so clearing that should work.
Put the following into ~/.vim/after/indent/php.vim; this way, it'll apply after the plugin:
setlocal indentkeys=
The effect I want can get achieved using:
:set paste
This is exactly(?) the mode I wanted to switch into. No autoindenting at all. But the "=" key works to indent a marked block of text.
Maybe I will add "set paste" to my .vimrc :)
Thanks for your support anyways.

Trying to set .html files with htmljinja filetype and get correct indentation

I have just this configuration below. When I uncomment the last line, I dont get the correct indentation: the lines are put in the first column when I indent.
syntax on
autocmd FileType html setlocal shiftwidth=2 tabstop=2
"au BufRead,BufNewFile *.html set filetype=htmljinja
I need the htmljinja filetype because my html has twig code (twig's syntax is like htmljinja)
I have vim 7.3.547
In the meantime i circumvent vim's indentation capabilities and rather rely on specific indenters/reformatter wherever i can (by the way if anybody is aware of a tex/latex reformatter please let me know).
As a result the following lines found their way into my .vimrc:
au FileType c,cpp let &l:equalprg="indent -br -l62 -nce -blf -bbb -i4 -nbfde -nbfda -bad -bap -cli4 -nut"
au FileType perl let &l:equalprg="perltidy"
noremap <leader>i mzgg=G`z
A drawback of this approach is that you can't enjoy automatic indentation while writing. Nevertheless I get used to it very fast and I think of it as a very solid and suitable solution to the indentation-problem. Moreover the indented code looks great and i doubt that those fancy IDE's keep up with it.
Now, what has this got to do with your problem?
Following my approach i searched for an html-reformatter and found tidy. tidy seems to be a monster, that not only can pretty-print your html files but also can validate and correct them.
In order to cope with tidy you have to write a config file for your indentation purposes. The following one works for me:
~/.tidyrc_indent:
indent: auto
indent-spaces: 3
show-warnings: no
show-errors: 0
quiet: yes
indent-cdata: yes
output-html: yes
wrap: 80
But make sure to read the man pages of tidy because the options seems to be endless.
Now you can add
au FileType htmljinja let &equalprg="tidy -config ~/.tidyrc"
to your .vimrc and start indenting your html file with gg=G or a derived mapping that needs less keystrokes.
Please take care as tidy like any other reformatter may mess up your code. Therefore you should back up your code regularly (maybe with git or something similar). Although i never experienced this by myself wiht perltidy and indent there are some reports on the internet of angry users that complain about such events.

How to enable go.vim by default (automatically)?

The instructions on the Vim site says to just put the file in the /syntax folder. This works all right and well. But, for me to use the syntax I must set the following
:set syntax=go
Every single time. So, I know I am doing something wrong. I just don't know what.
Here are some things from looking around,
My HTML5 syntax set is from Rodrigo's HTML5 omnicomplete function and syntax vimball file. Though this uses some installation script to get it going.
As far as I can tell this would be my first manual adding of syntax file.
Also, my VIMRUNTIME is not set, well because there is no syntax.vim file, so from reading the documentation I see it checks for files via synload.vim
I even read the "Making Your Own Syntax Files" section, which says that same as above with the syntax=go option. Am I supposed to be detecting the .go filetype as described in new filetype section?
How can I enable syntax highlighting for GO by default?
This is for Mac Snow Leopard.
I don't think it is this complicated but I decided to leave all the different documentation I skimmed. GO and Vim say to just add the file. But it is definitely not detecting it automatically
If you are using filetype detection in your ~/.vimrc file with the following line:
filetype plugin indent on
then you can place the file in the following folder:
~/.vim/after/ftplugin/go.vim
or for windows
~/vimfiles/...
For the filetype detection to work, would would want the autocmd in a file in the ftdetect folder:
~/.vim/ftdetect/go.vim
and the contents would be:
autocmd BufNewFile,BufReadPost *.go set filetype=go
Use autocmd:
au BufRead,BufNewFile *.go setlocal filetype=go

How can I disable code folding in vim with vim-latex?

I have tried the usual approaches, and have read :help tex.vim
(see : http://vimdoc.sourceforge.net/htmldoc/syntax.html )
I've taken a brief look at syntax/tex.vim, but can't see how to disable it without rebuilding vim without folding. I'm sick of hitting 'zE'.
Lines I've tried in my .vimrc:
set foldlevel=manual
set foldlevelstart=99
let g:tex_fold_enabled=0
Just noticed that there are variables to control folding in vim-latex-suite, at least as of v1.6 of the plugin. The functionality is documented here:
http://vim-latex.sourceforge.net/documentation/latex-suite.html#latex-folding
In short you should be able to change three global variables to get rid of all folding:
:let Tex_FoldedSections=""
:let Tex_FoldedEnvironments=""
:let Tex_FoldedMisc=""
That should get rid of all folding. If you want to disable some folding but not all then you can control things by setting the appropriate values for each variable, as described in the documentation link above. Hope that helps.
What about
autocmd Filetype tex setlocal nofoldenable
The folding functionality all seems to located in folding.vim file of latex-suite distribution. This file is referenced in line 825 of my main.vim file in the latex-suite folder of the ftplugin folder. That line reads:
exe 'source '.fnameescape(s:path.'/folding.vim')
Comment out that line and, as far as I can tell, it strips out all the folding in latex-suite plugin. I don't think it affects anything else, but I haven't checked.

Resources