Vim似乎没有正确地对YAML文件中的破折号进行反应,因此打破了格式.
例如,我有一个块应该是这样的:
handlers:
- name: restart exim4
service: name=exim4 state=restarted
Run Code Online (Sandbox Code Playgroud)
当我完成输入restart exim4并输入service:Vim reindents时,我的最后service一行:
handlers:
- name: restart exim4
service: name=exim4 state=restarted
Run Code Online (Sandbox Code Playgroud)
很明显,Vim尝试按列排列句子,但这不是YAML所需要的.我想创建一个包含两个值的数组.
如何解决?
kim*_*noa 119
为了在冒号后回到回车符时将默认的2空间YAML作为默认值,我将其添加到我的.vimrc:
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
Run Code Online (Sandbox Code Playgroud)
对于OP的预期结果,这也很好用.
K. *_*ert 24
安装此插件:
https://github.com/chase/vim-ansible-yaml
它是用Ansible制作的,但理论上它可以用于各种YAML文件.如果您不使用ansible相关文件,您将不得不:设置filetype = ansible.
Eug*_*ash 18
你可以使用这个自动命令让 Vim 正确缩进 YAML 文件(把它放到你的.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
Run Code Online (Sandbox Code Playgroud)
基本上,对于 YAML 文件,它指示 Vim:
#在行首或冒号插入注释字符 ( )后跳过重新缩进的行。kev*_*kev 14
键入:键时可以禁用reindent :
:set indentkeys-=<:>
Run Code Online (Sandbox Code Playgroud)
请编辑~/.vimrc文件,并添加以下行:
filetype plugin indent on
autocmd FileType yaml setl indentkeys-=<:>
Run Code Online (Sandbox Code Playgroud)
注意: autocmd来之后filetype.
您可以通过键入触发重新缩进CTRL-F在INSERT模式,例如:
hello: world
foo: bar<C-F>
Run Code Online (Sandbox Code Playgroud)