vim: configuration for ansible

This commit is contained in:
Meutel 2015-09-15 20:22:09 +02:00
parent 7880d842ce
commit ecc9f82512
7 changed files with 496 additions and 0 deletions

104
_vim/doc/ansible.txt Normal file
View File

@ -0,0 +1,104 @@
*ansible.txt* Configuration for editing Ansible (YAML) files
For |Vim version 7.3| and later. Last change: 2014 Nov 18
*ansible* *ansible.vim*
This plugin adds additional syntax highlighting and fixes indentation for
Ansible's dialect of YAML.
Ansible YAML files are detected based on the presence of a
structure following Ansible's Playbook Best Practices
(http://www.ansibleworks.com/docs/playbooks_best_practices.html#directory-layout).
For details, see the |ansible-detect| section below.
The functionality mentioned here is an ftplugin, see |ftplugins|.
This plugin is only available if 'compatible' is not set.
{Vi does not have any of this}
1. Installation |ansible-install|
==============================================================================
1. Installation *ansible-install*
1.1 Using Vundle (https://github.com/gmarik/vundle)
1. Add the following to your |vimrc| where other bundles are located: >
Bundle 'chase/vim-ansible-yaml'
~
2. Run from command line: >
$ vim +BundleInstall
1.2 Using Pathogen (https://github.com/tpope/vim-pathogen)
1. Check out the repository into your bundle path: >
$ cd ~/.vim/bundle
$ git clone git://github.com/chase/vim-ansible-yaml.git
~
2. Install this help file. (Repeat this step if you get an updated version.)
From inside vim, >
:Helptags
1.3 Normal
1. Check out the repository and copy the following to `.vim/` directory or
any other 'runtimepath', keeping their directory structure intact:
doc/ansible.txt
ftdetect/ansible.vim
syntax/ansible.vim
syntax/include/jinja.vim
syntax/include/yaml.vim
indent/ansible.vim
2. Install the help file. From inside vim, >
:helptags ~/.vim/doc
2. Detection *ansible-detection*
You can tell vim to recognize a file as Ansible by adding a |modeline| near
the top or bottom of the file: >
# vim:ft=ansible:
A file is recognized as an Ansible YAML file, and its filetype is set to
`ansible`, if
1. The extension is `.yml`
2. AND one of the following conditions holds:
1. The file is somewhere under a directory named `roles`.
2. The file is in the same directory as a directory (or file) named
`group_vars`, `host_vars`, or `roles`.
3. Configuration *ansible-configuration*
So far, there is only one option. Others may be added later.
If you define >
:let g:ansible_options = {'ignore_blank_lines': 0}
in your |vimrc| file, then the indent function will remove all indent after a
blank line. The default behavior is to ignore blank lines when calculating the
indent of the current line. This is helpful if your style is to insert blank
lines, as in >
tasks:
- name: Say hello.
command: echo Hello, world.
- name: Say good night, Dick.
command: echo Good night, Dick.
If `g:ansible_options` is not defined, or if the `ignore_blank_lines` key is
not present, or the value is not `0`, then the indent function uses the
default behavior.
4. Thanks *ansible-credits*
A huge thanks to Igor Vergeichik (mailto:iverg@mail.ru) and Nikolai Weibull
(https://github.com/now) for their work on the YAML syntax that this bundle
uses.
Also, thank you, Armin Ronacher (https://github.com/mitsuhiko), for the
simple and effective Jinja syntax file.
vim:tw=78:ts=8:ft=help:norl:

43
_vim/ftdetect/ansible.vim Normal file
View File

@ -0,0 +1,43 @@
" Determine if normal YAML or Ansible YAML
" Language: YAML (with Ansible)
" Maintainer: Benji Fisher, Ph.D. <benji@FisherFam.org>
" Author: Chase Colman <chase@colman.io>
" Version: 1.0
" Latest Revision: 2015-03-23
" URL: https://github.com/chase/vim-ansible-yaml
autocmd BufNewFile,BufRead *.yml,*.yaml,*/{group,host}_vars/* call s:SelectAnsible("ansible")
autocmd BufNewFile,BufRead hosts call s:SelectAnsible("ansible_hosts")
fun! s:SelectAnsible(fileType)
" Bail out if 'filetype' is already set to "ansible".
if index(split(&ft, '\.'), 'ansible') != -1
return
endif
let fp = expand("<afile>:p")
let dir = expand("<afile>:p:h")
" Check if buffer is file under any directory of a 'roles' directory
" or under any *_vars directory
if fp =~ '/roles/.*\.y\(a\)\?ml$' || fp =~ '/\(group\|host\)_vars/'
execute "set filetype=" . a:fileType
return
endif
" Check if subdirectories in buffer's directory match Ansible best practices
if v:version < 704
let directories=split(glob(fnameescape(dir) . '/{,.}*/', 1), '\n')
else
let directories=glob(fnameescape(dir) . '/{,.}*/', 1, 1)
endif
call map(directories, 'fnamemodify(v:val, ":h:t")')
for dir in directories
if dir =~ '\v^%(group_vars|host_vars|roles)$'
execute "set filetype=" . a:fileType
return
endif
endfor
endfun

52
_vim/indent/ansible.vim Normal file
View File

@ -0,0 +1,52 @@
" Vim indent file
" Language: YAML (with Ansible)
" Maintainer: Benji Fisher, Ph.D. <benji@FisherFam.org>
" Author: Chase Colman <chase@colman.io>
" Version: 1.0
" Latest Revision: 2014-11-18
" URL: https://github.com/chase/vim-ansible-yaml
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
setlocal sw=2 ts=2 sts=2 et
setlocal indentexpr=GetAnsibleIndent(v:lnum)
setlocal indentkeys=!^Fo,O,0#,<:>,-
setlocal nosmartindent
" Only define the function once.
if exists('*GetAnsibleIndent')
finish
endif
function GetAnsibleIndent(lnum)
" Check whether the user has set g:ansible_options["ignore_blank_lines"].
let ignore_blanks = !exists('g:ansible_options["ignore_blank_lines"]')
\ || g:ansible_options["ignore_blank_lines"]
let prevlnum = ignore_blanks ? prevnonblank(a:lnum - 1) : a:lnum - 1
if prevlnum == 0
return 0
endif
let prevline = getline(prevlnum)
let indent = indent(prevlnum)
let increase = indent + &sw
" Do not adjust indentation for comments
if prevline =~ '\%(^\|\s\)#'
return indent
elseif prevline =~ ':\s*[>|]?$'
return increase
elseif prevline =~ '^\s*-\s*$'
return increase
elseif prevline =~ '^\s*-\s\+[^:]\+:\s*\S'
return increase
else
return indent
endif
endfunction

50
_vim/syntax/ansible.vim Normal file
View File

@ -0,0 +1,50 @@
" Vim syntax file
" Language: YAML (with Ansible)
" Maintainer: Benji Fisher, Ph.D. <benji@FisherFam.org>
" Author: Chase Colman <chase@colman.io>
" Version: 1.0
" Latest Revision: 2014-06-28
" URL: https://github.com/chase/vim-ansible-yaml
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'ansible'
endif
" Load YAML syntax
source <sfile>:p:h/include/yaml.vim
unlet b:current_syntax
source <sfile>:p:h/include/jinja.vim
unlet b:current_syntax
syn case match
syn match ansibleRepeat '\<with_\w\+\>' contained containedin=yamlKey
syn keyword ansibleConditional when changed_when contained containedin=yamlKey
syn region ansibleString start='"' end='"' skip='\\"' display contains=jinjaVarBlock
if version >= 508 || !exist("did_ansible_syn")
if version < 508
let did_ansible_syn = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink ansibleConditional Statement
HiLink ansibleRepeat Repeat
HiLink ansibleString String
delcommand HiLink
endif
let b:current_syntax = 'ansible'
if main_syntax == 'ansible'
unlet main_syntax
endif

View File

@ -0,0 +1,102 @@
" jinja syntax file
" Language: Jinja YAML Template
" Maintainer: Benji Fisher, Ph.D. <benji@FisherFam.org>
" Author: Chase Colman <chase@colman.io>
" Author: Armin Ronacher <armin.ronacher@active-4.com>
" Version: 1.0
" Latest Revision: 2013-12-10
" URL: https://github.com/chase/vim-ansible-yaml
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'jinja'
endif
syntax case match
" Jinja template built-in tags and parameters (without filter, macro, is and raw, they
" have special threatment)
syn keyword jinjaStatement containedin=jinjaVarBlock,jinjaNested contained and if else elif is in not or recursive as import
syn keyword jinjaStatement containedin=jinjaVarBlock,jinjaNested contained is filter skipwhite nextgroup=jinjaFilter
syn keyword jinjaStatement containedin=jinjaTagBlock contained macro skipwhite nextgroup=jinjaFunction
syn keyword jinjaStatement containedin=jinjaTagBlock contained block skipwhite nextgroup=jinjaBlockName
" Variable Names
syn match jinjaVariable containedin=jinjaVarBlock,jinjaNested contained /[a-zA-Z_][a-zA-Z0-9_]*/
syn keyword jinjaSpecial containedin=jinjaVarBlock,jinjaNested contained false true none False True None loop super caller varargs kwargs
" Filters
syn match jinjaOperator "|" containedin=jinjaVarBlock,jinjaNested contained skipwhite nextgroup=jinjaFilter
syn keyword jinjaFilter contained abs attr batch capitalize center default
syn keyword jinjaFilter contained dictsort escape filesizeformat first
syn keyword jinjaFilter contained float forceescape format groupby indent
syn keyword jinjaFilter contained int join last length list lower pprint
syn keyword jinjaFilter contained random replace reverse round safe slice
syn keyword jinjaFilter contained sort string striptags sum
syn keyword jinjaFilter contained title trim truncate upper urlize
syn keyword jinjaFilter contained wordcount wordwrap
syn match jinjaBlockName contained /[a-zA-Z_][a-zA-Z0-9_]*/
" Jinja template constants
syn region jinjaString containedin=jinjaVarBlock,jinjaNested contained start=/"/ skip=/\\"/ end=/"/
syn region jinjaString containedin=jinjaVarBlock,jinjaNested contained start=/'/ skip=/\\'/ end=/'/
syn match jinjaNumber containedin=jinjaVarBlock,jinjaNested contained /[0-9]\+\(\.[0-9]\+\)\?/
" Operators
syn match jinjaOperator containedin=jinjaVarBlock,jinjaNested contained /[+\-*\/<>=!,:]/
syn match jinjaPunctuation containedin=jinjaVarBlock,jinjaNested contained /[()\[\]]/
syn match jinjaOperator containedin=jinjaVarBlock,jinjaNested contained /\./ nextgroup=jinjaAttribute
syn match jinjaAttribute contained /[a-zA-Z_][a-zA-Z0-9_]*/
" Jinja template tag and variable blocks
syn region jinjaNested matchgroup=jinjaDelimiter start="(" end=")" transparent display containedin=jinjaVarBlock,jinjaNested contained
syn region jinjaNested matchgroup=jinjaOperator start="\[" end="\]" transparent display containedin=jinjaVarBlock,jinjaNested contained
syn region jinjaNested matchgroup=jinjaDelimiter start="{" end="}" transparent display containedin=jinjaVarBlock,jinjaNested contained
syn region jinjaVarBlock matchgroup=jinjaVarDelim start=/{{-\?/ end=/-\?}}/ containedin=ALLBUT,jinjaVarBlock,jinjaRaw,jinjaString,jinjaNested
" Jinja template 'raw' tag
syn region jinjaRaw matchgroup=jinjaRawDelim start="{%\s*raw\s*%}" end="{%\s*endraw\s*%}" containedin=ALLBUT,jinjaVarBlock,jinjaString
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_jinja_syn_inits")
if version < 508
let did_jinja_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jinjaPunctuation jinjaOperator
HiLink jinjaAttribute Identifier
HiLink jinjaFunction jinjaFilter
HiLink jinjaVarDelim PreProc
HiLink jinjaRawDelim jinjaVarDelim
HiLink jinjaSpecial Special
HiLink jinjaOperator Operator
HiLink jinjaRaw Normal
HiLink jinjaStatement Statement
HiLink jinjaDelimiter Delimiter
HiLink jinjaFilter Function
HiLink jinjaBlockName Function
HiLink jinjaVariable Normal
HiLink jinjaString Constant
HiLink jinjaNumber Constant
delcommand HiLink
endif
let b:current_syntax = "jinja"
if main_syntax == 'jinja'
unlet main_syntax
endif

View File

@ -0,0 +1,142 @@
" Vim syntax file
" Language: YAML (YAML Ain't Markup Language)
" Maintainer: Benji Fisher, Ph.D. <benji@FisherFam.org>
" Author: Chase Colman <chase@colman.io>
" Author: Igor Vergeichik <iverg@mail.ru>
" Author: Nikolai Weibull <now@bitwi.se>
" Sponsor: Tom Sawyer <transfire@gmail.com>
" Latest Revision: 2014-12-08
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'yaml'
endif
let s:cpo_save = &cpo
set cpo&vim
" Allows keyword matches containing -
setl iskeyword+=-
syn keyword yamlTodo contained TODO FIXME XXX NOTE
syn region yamlDocumentHeader start='---' end='$' contains=yamlDirective
syn match yamlDocumentEnd '\.\.\.'
syn match yamlDirective contained '%[^:]\+:.\+'
syn region yamlComment display oneline start='\%(^\|\s\)#' end='$'
\ contains=yamlTodo,@Spell
syn match yamlNodeProperty "!\%(![^\\^% ]\+\|[^!][^:/ ]*\)"
syn match yamlAnchor "&.\+"
syn match yamlAlias "\*.\+"
syn match yamlDelimiter "[-,:]\s*" contained
syn match yamlBlock "[\[\]\{\}>|]"
syn match yamlOperator '[?+-]'
" - yamlBlock is contained here in the mapping because having the mapping end
" at $ clobbers detecting yamlBlock endings.
" - Without re-writing quite a bit of this logic this seems like the cleanest
" way to fix this
syn region yamlMapping start='\w\+\%(\s\+\w\+\)*\s*\ze:' end='$' keepend oneline contains=yamlKey,yamlScalar,yamlBlock
syn match yamlScalar '\%(\W*\w\+\)\{2,}' contained contains=yamlTimestamp,yamlString,@yamlTypes,yamlBlock
syn cluster yamlTypes contains=yamlInteger,yamlFloating,yamlNumber,yamlBoolean,yamlConstant,yamlNull,yamlTime
syn match yamlKey '\w\+\%(\s\+\w\+\)*\s*:' contained nextgroup=@yamlTypes contains=yamlDelimiter
" Predefined data types
" Yaml Integer type
syn match yamlInteger "\<[-+]\?\(0\|[1-9][0-9,]*\)\s*$" contained
syn match yamlInteger "\<[-+]\?0[xX][0-9a-fA-F,]\+\s*$" contained
" floating point number
syn match yamlFloating "\<\d\+\.\d*\(e[-+]\=\d\+\)\=[fl]\=\s*$" contained
syn match yamlFloating "\.\d\+\(e[-+]\=\d\+\)\=[fl]\=\s*$" contained
syn match yamlFloating "\<\d\+e[-+]\=\d\+[fl]\=\s*$" contained
syn match yamlFloating "\<\(([+-]\?inf)\).*$\|\((NaN)\)\s*$" contained
syn match yamlNumber '\<[+-]\=\d\+\%(\.\d\+\%([eE][+-]\=\d\+\)\=\)\=\s*$' contained
syn match yamlNumber '\<0\o\+\s*$' contained
syn match yamlNumber '\<0x\x\+\s*$' contained
syn match yamlNumber '\<([+-]\=[iI]nf)\s*$' contained
" Boolean
syn keyword yamlBoolean true True TRUE false False FALSE yes Yes YES no No NO on On ON off Off OFF contained
syn match yamlBoolean ":.*\zs\W[+-]\(\W\|$\)" contained
syn match yamlConstant '\<[~yn]\s*$' contained
" Null
syn keyword yamlNull null Null NULL nil Nil NIL contained
syn match yamlNull "\W[~]\(\W\|$\)" contained
syn match yamlTimestamp '\d\d\d\d-\%(1[0-2]\|\d\)-\%(3[0-2]\|2\d\|1\d\|\d\)\%( \%([01]\d\|2[0-3]\):[0-5]\d:[0-5]\d.\d\d [+-]\%([01]\d\|2[0-3]\):[0-5]\d\|t\%([01]\d\|2[0-3]\):[0-5]\d:[0-5]\d.\d\d[+-]\%([01]\d\|2[0-3]\):[0-5]\d\|T\%([01]\d\|2[0-3]\):[0-5]\d:[0-5]\d.\dZ\)\=' contained
" Single and double quoted scalars
syn region yamlString oneline start="'" end="'" skip="\\'"
\ contains=yamlSingleEscape
syn region yamlString oneline start='"' end='"' skip='\\"'
\ contains=yamlEscape
" Escaped symbols
" every charater preceeded with slash is escaped one
syn match yamlEscape "\\."
" 2,4 and 8-digit escapes
syn match yamlEscape "\\\(x\x\{2\}\|u\x\{4\}\|U\x\{8\}\)"
syn match yamlEscape contained display +\\[\\"abefnrtv^0_ NLP]+
syn match yamlEscape contained display '\\x\x\{2}'
syn match yamlEscape contained display '\\u\x\{4}'
syn match yamlEscape contained display '\\U\x\{8}'
syn match yamlEscape display '\\\%(\r\n\|[\r\n]\)'
syn match yamlSingleEscape contained display +''+
syn match yamlAnchor "&\S\+"
syn match yamlAlias "*\S\+"
syn match yamlType "![^\s]\+\s\@="
if version >= 508 || !exist("did_yaml_syn")
if version < 508
let did_yaml_syn = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink yamlKey Identifier
HiLink yamlType Type
HiLink yamlInteger Number
HiLink yamlFloating Float
HiLink yamlNumber Number
HiLink yamlEscape Special
HiLink yamlSingleEscape SpecialChar
HiLink yamlComment Comment
HiLink yamlBlock Operator
HiLink yamlDelimiter Delimiter
HiLink yamlString String
HiLink yamlBoolean Boolean
HiLink yamlNull Boolean
HiLink yamlTodo Todo
HiLink yamlDocumentHeader PreProc
HiLink yamlDocumentEnd PreProc
HiLink yamlDirective Keyword
HiLink yamlNodeProperty Type
HiLink yamlAnchor Type
HiLink yamlAlias Type
HiLink yamlOperator Operator
HiLink yamlScalar String
HiLink yamlConstant Constant
HiLink yamlTimestamp Number
delcommand HiLink
endif
let b:current_syntax = "yaml"
let &cpo = s:cpo_save
unlet s:cpo_save
if main_syntax == "yaml"
unlet main_syntax
endif

3
_vimrc
View File

@ -32,3 +32,6 @@ filetype plugin indent on
inoremap jk <ESC>
" leader key
let mapleader = ","
" modeline
set modeline