vim: enabling Linux coding style per file - vim

I enabled Linux coding style in $HOME/.vimrc as:
let g:linuxsty_patterns = [ '/home/mark/sample1', '/home/mark/sample2', '/home/mark/sample3' ]
But is it possible to enable it per file? As I know, it is possible to pass vim settings in comments in C files. Also, I tried to manually set linuxsty_patterns while editing opened file in vim, but it didn't take any effect.
What am I doing wrong?

You should have stated that your question relates to a specific plugin.
Those questions are better addressed directly to the plugin maintainer.
Now, reading its source code, it seems you just need to specific a list of regexes that match the source files to which you which to apply the plugin setting. It seems this is the only control you could expect with this plugin.

I don't see file types in your example, but I use autogroup like this.
augroup go
autocmd!
" Write file before :make, :GoBuild, :GoTest|Func, :GoRun
autocmd FileType go set autowrite
" go fmt uses tabs for indentation, ts and sw only affect viewer
autocmd FileType go setlocal noexpandtab tabstop=4 shiftwidth=4
" Enable Vim syntax highlighting for Go template files
......
augroup END

Related

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

VIM: set filetype=txt for every new file [No Name]

I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work.
This is p.e. what I've read on the web a few times:
au BufRead,BufNewFile *.txt setlocal ft=txt
(to put in _vimrc)
However it doesn't work.
Can anyone help me?
The following line, added to your .vimrc, will set the filetype to text if it is not already set.
autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif
All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set the file-type to any old text, so are you absolutely sure it is not working?
:set filetype=banana
:set filetype?
filetype=banana
Setting the filetype is not going to have any noticable effect unless there is a corresponding file in the ftplugin Vim directory and Vim does not ship with a txt.vim file-type file. You could, of couse, add a txt.vim here but I am not sure what this will gain you over default settings — what special behaviour would you want for text files that you would not want for the default behaviour?
(If you want syntax highlighting (whatever that may mean for text file!) then you will also have to create a txt.vim file in the syntax Vim directory.)
What effect are you trying to achieve?
It's actually way simpler than all this. Just put this as one of the first lines in your .vimrc.
set ft=txt
Whenever opening a new file, the filetype will be set to txt. But if you open a file with an known existing type it will still be overridden no problem.

Vim - indent like Emacs

I use vim (primary so that I can work on plain ssh terminal - still uncomfortable with Emacs non-gui version) but most of my colleagues in the organization use emacs. So using CVS, we face indentation inconsistency issues (tabs becoming spaces, number of tabs/spaces, code layout etc).
Is there a way I can make VIM indent EXACTLY as EMACS. (similar to the default emacs profile my colleagues use).
(Most importantly, I want vim's C++ and TCL indentation schemes to match that of emacs).
regards,
JP
I don't know if there's a way to directly import Emacs indentation settings into vim, but you can probably configure the same behavior in vim itself:
set expandtab will convert tabs to spaces
set autoindent will keep indentation level from previous line
set shiftwidth=4 will affect block indentation with >> and <<
set softtabstop=4 sets the length of soft tab in spaces
set tabstop=8 sets the width of tab character
This is properly explained in vim wiki.
When you need filetype-specific indentation you have two options:
Set autocmd to change indentation on file read and file creation:
au BufRead,BufNewFile *.py,*pyw,*.html,*.js set shiftwidth=4 will set shiftwidth for *.py files.
Configure filetype plugin, create name.vim scripts inside .vim/ftplugin folder for specific file types and set the described variables there. This is also described in proper detail in vim wiki.
Regarding specialized indenting for c++ and TCL there is some special stuff that applies in aditon to all the other setting info that's been suggested. Vim has special indenting rules defined in code for different languages. Some of this is found is found in the /indent directory of the vim installation, where there is a separate file for each filetype. For more information on how this works read the help for 'indentexpr'.
The c indenting -- and I think also the indenting for c++ -- is mostly defined in Vim source-code, and has a zillion options that you can set, plus is specially configurable in c.vim or c++.vim indent file. Read help for 'cindent' and 'c-indenting' for more help on that.
In short, the tcl.vim file controls special indenting for tcl files. If you want to revise how indenting works with tcl you would want to alter the main function in that file. The c/c++ indenting is largely controlled by Vim internals but with lots of different option flags. You can control c/c++ indenting by configuring the options the way you want them and/or by writing a function for the indent file in /indent directory. (I believe there is no c++ file in /indent directory, not sure if c.vim is file to edit there, or whether you need to create new c++.vim file. I think it's the c.vim file that would be used. which is basically an empty shell in standard Vim install, but you can read other *.vim indent files to get the idea of how they work.
Here's as extract of some options concerning indentation from .vimrc:
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
All options are nicely described in vim help:
:help smartindent
:help autoindent
UPD: also for C-like languages you may consider :help C-indenting

Vim: How to change text from within an indent script

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.

Resources