Vim does not seem to correctly react at a dash symbol in YAML files therefore breaking the format.
For example I have a block which should look like this:
handlers:
- name: restart exim4
service: name=exim4 state=restarted
When I finish typing restart exim4 and type service: Vim reindents my final service line:
handlers:
- name: restart exim4
service: name=exim4 state=restarted
So clearly Vim tries to align sentences column-wise but that's not what is needed in YAML. I want to create an array with two values.
How to fix that?
In order to get the nice 2-space YAML as the default when I hit carriage return after the colon, I added this to my .vimrc:
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
This also plays nice with the dash for the OP's expected result.
Install this plugin:
https://github.com/chase/vim-ansible-yaml
It is made with Ansible in mind, but in theory it will work with all kinds of YAML files. You will have to :set filetype=ansible unfortunately, if you are not working with ansible related files.
You can use this autocommand to make Vim properly indent YAML files (put it to your .vimrc):
" Fix auto-indentation for YAML files
augroup yaml_fix
autocmd!
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab indentkeys-=0# indentkeys-=<:>
augroup END
Basically, for YAML file it instructs Vim to:
Use 2 spaces for indentation.
Use spaces instead of tabs.
Skip re-indenting lines after inserting a comment character (#) at the beginning of a line, or a colon.
You can disable reindent when you type : key:
:set indentkeys-=<:>
Please edit ~/.vimrc file, and add these lines:
filetype plugin indent on
autocmd FileType yaml setl indentkeys-=<:>
Note: autocmd comes after filetype.
You can trigger reindent by typing CTRL-F in INSERT mode, for example:
hello: world
foo: bar<C-F>
You can add a comment in your YAML to tell Vim special config for this file. For example:
# vim: set shiftwidth=2 tabstop=2 softtabstop=-1 expandtab:
foo:
bar:
- a
- b
Then everyone, who use this file with a default vim, can share the file creator's configuration. It works well especially when cooperating.
I've found https://github.com/stephpy/vim-yaml to work great. It's a vim plugin that does indentation (and syntax highlighting) of yaml files. Installing it solves the specific problem that you've asked about too.
You need to install the plugin (see the doco in the GitHub repo) and as long as your filetype=yaml it'll correct your indenting. It'll help you both
as you're typing the snippet you've provided, or
if you already have some yaml written, you can select it (with V for line-wise selection then use j or k to select more lines) then trigger the vim formatting with =
Here's the augroup I have for yaml:
augroup filetype_yaml
autocmd!
autocmd BufNewFile,BufReadPost *.{yaml,yml} set filetype=yaml foldmethod=indent
autocmd FileType yaml |
setlocal shiftwidth=2 |
setlocal softtabstop=2 |
setlocal tabstop=2
augroup END
I have found a very good answer instead of updating .vimrc.
here by Ignacio Vazquez-Abrams
Related
I'm trying to detect buck's TARGETS file as 'buck' files instead of 'python', which is the filetype vim's currently detecting for them.
I tried to add autocmd BufRead,BufNewFile **/TARGETS set filetype=buck to my .vimrc but it didn't seem to work.
What's the correct way to change a 'default' detected file type ?
Your autocmd is the way to go in principle; just omit the **/ in the pattern:
autocmd BufRead,BufNewFile TARGETS set filetype=buck
see :help autocmd-patterns
Ideally, you'd have this line in a .vim/ftdetect/buck.vim file
Vim automatically folds my code when I open a new file. How can I stop it from folding the text when I run git commit -a, since I don't want folding only in this specific case?
I currently have this line in my code setlocal foldmethod=syntax to fold all code automatically.
I tried to add this line before and after setting the foldmethod autocmd FileType gitcommit setlocal nofoldenable, but it did not changing anything.
Put this into after/ftplugin/gitcommit.vim in your runtime (~/.vim) or a plugin:
setlocal nofoldenable
Alternately, in your .vimrc,
autocmd FileType gitcommit setlocal nofoldenable
I use Vim 7.3, and have this in my .vimrc (and nothing else):
filetype plugin indent on
autocmd FileType java :setlocal sw=4 ts=4 sts=2 noet
autocmd BufNewFile,BufReadPost ~/testdir/* setlocal sw=2 ts=2 sts=2 et
I have a directory ~/testdir which only contains a subdirectory p and the file ~/testdir/p/A.java which contains this:
class A {
}
If I open A.java by saying :e ~/testdir/p/A.java then :set et? reports that expandtab is currently ON.
On the other hand, if I start a new instance of Vim and go :vim A ~/testdir/** then this search will open A.java, but now :set et? tells me expandtab is OFF.
How do I make my intended indentation settings always apply for files under ~/testdir/?
The event BufReadPost should be being fired. Check that writing a message. This is:
autocmd BufNewFile,BufReadPost $HOME/git/repo/* echom "Reading buffer " . expand("<afile>")
Look for the messages with:
:messages
If the messages are there, then you know you are setting those options for the buffer. So, if the options have changed when the buffer is displayed, perhaps a plugin is setting them to some other thing. Check who is the culprit by showing who last changed the option with
:verbose set sw?
If that doesn't lead you to a solution, try using BufWinEnter instead of BufReadPost. That would probably work.
Vim explicitly recommends using ~ for the user home.
The help at :h autocmd-patterns says that you can use environment variables in the event patterns ...
... [a]nd ~ can be used for the home directory (if $HOME is defined):
:autocmd BufWritePost ~/.vimrc so ~/.vimrc
:autocmd BufRead ~archive/* set readonly
The environment variable is expanded when the autocommand is defined, not when
the autocommand is executed. This is different from the command!
So, change $HOME to ~ in your autocommand.
Even if your problem turns out to be unrelated it's good to follow the practice recommended in the docs.
The autocmd does work but probably not the way you expect since you did not set softtabstop.
autocmd BufNewFile,BufReadPost ~/git/repo/* setlocal sw=2 ts=2 sts=2 et
If you do not set softtabstop it will keep its old value. If this value is not 0 (which seems to be the case) when you press tab you will get the number of softtabstop spaces inserted into the file. And it will look like its not working.
It is generally a good idea to set shiftwidth, tabstop, and softtabstop to the same value if you ever change it.
Its also probably a good idea to make the set command local to the buffer by using setlocal.
Take a look at :h softtabstop
You can see that the autocmd "works" when softtabstop is set to its default value (of 0) if you run the command like this. And then run your :vim command
vim -u NONE -N -c'autocmd BufNewFile,BufReadPost $HOME/git/repo/* set sw=2 ts=2 et'
I have installed Markdown syntax plugin.
When I'm on a new buffer not associated with any file, and paste the source portion of the site http://rosylilly.github.com/markdown.html into it, the colors appear nicely as it should for the markdown syntax.
However, after saving the file, the colors change. Any idea how I can fix this?
Can you, before and after saving do
:verbose set filetype
:verbose set
Tip: use :redir > file.txt to capture the output so you won't have to copy/paste which can be difficult with gvim - for command output
You could also look at the autocommands when saving:
:verbose au BufWrite
:verbose au BufWritePre
:verbose au BufWritePost
This would serve to discover what plugin/script is causing the highlighting to go haywire.
I turned on filetype plugin for some rails vim plugins I added, but a side effect of this seems to be that now autocommenting has been enabled in all filetypes (for instance, if I start a line with #, the next line, either by Enter in insert mode or O, etc. to enter insert mode, will also get a #).
I found a guide to disabling the auto-commenting formatoptions, and added the following to my .vimrc:
au FileType * setlocal formatoptions-=cro
However, I am still running into problems -- unless I explicitly :source .vimrc, (or enter the setlocal ... directly), it is not taking effect. I determined that this is the case because vim's ftplugins are overriding my options with their own.
I then found a second guide which talks about using an after ftplugin script to make changes after the ftplugin scripts have run, however their solution is to create symlinks for every single filetype in ~/.vim/after/ftplugin to a central file, and this seems to be kludgy to me.
Is there any way to create a generic after-ftplugin script or am I approaching this problem incorrectly? Any help would be appreciated.
How about an "after" plugin? Create a file in ~/.vim/after/plugin/ called noAutoComments.vim (or whatever) and place your autocmd in that?
Edit:
The reason this works? I'm only guessing here, but I have a feeling that the autocmd in the ~/.vimrc file is getting removed by some other file (but before the "after" files are getting sourced).
I ended up removing my ~/.vim directory and replaced my ~/.vimrc with the following 3 lines:
filetype plugin on
syntax on
au FileType * setlocal formatoptions-=cro
With only these lines in my ~/.vimrc and no ~/.vim/ directory, the autocmd seems to work as expected (Vim 7.1).
For any file that I edit:
:verbose set formatoptions?
formatoptions=ql
Last set from ~/.vimrc
I have yet to determine what file (plugin) is causing this issue however.
I've done some more investigation and it seems that the location of my autocmd within my .vimrc file determines if formatoptions will be overridden by vim's ftplugins or not. Using vim --noplugin to disable all external plugins, I found the following results:
If my vimrc looks like:
au FileType * setl fo-=cro
filetype plugin indent on
The result of :verbose set fo? is:
formatoptions=croql
Last set from /usr/share/vim/vim72/ftplugin/ruby.vim
However, if the lines in my vimrc are reversed:
filetype plugin indent on
au FileType * setl fo-=cro
The result of :verbose set fo? is:
formatoptions=ql
Last set from ~/.vimrc
... which is the desired result. So it seems that the autocmd needs to be specified after filetype plugins are enabled.
Another reason this might not be taking effect...
From :he :set-=:
When the option is a list of flags, {value} must be
exactly as they appear in the option. Remove flags
one by one to avoid problems.
I have
" Turn off auto-commenting
au FileType * setlocal formatoptions-=c
au FileType * setlocal formatoptions-=r
au FileType * setlocal formatoptions-=o
because I've run into this.
Using one of the various autocmd events to set the configuration option should work if you find the right one, but I'd start by running:
:verbose set formatoptions?
This will tell you where the option was set, which may make it easier to determine which autocmd to use. Alternatively, if you don't mind a bit of minor hacking, the way I'd probably do it is just to find out where it's set in the plugin and comment out that line (and make a note of it in case you ever upgrade the plugin). You could also contact the plugin's author and ask them to make it a configurable option.
For the available autocmd events, read this:
:help {event}
I have tried solutions proposed by many, but none of them worked for me, but I found one very simple workaround, namely, in your ~/.bash_aliases:
# vim without auto comment
alias vi="vi +'set fo-=cro'"
I was struggling with this issue and I finally works with the following lines:
syntax on
filetype on
filetype plugin on
au FileType * setlocal formatoptions-=cro
I think the key here is that the autocmd is place after the filetype plugin on.