├── README.md ├── autoload └── fmt.vim └── plugin └── hcl.vim /README.md: -------------------------------------------------------------------------------- 1 | # Archived project. No maintenance. 2 | 3 | This project is not maintained anymore and is archived. Feel free to fork and 4 | make your own changes if needed. For more detail read my blog post: [Taking an indefinite sabbatical from my projects](https://arslan.io/2018/10/09/taking-an-indefinite-sabbatical-from-my-projects/) 5 | 6 | Thanks to everyone for their valuable feedback and contributions. 7 | 8 | # vim-hclfmt 9 | 10 | Vim plugin to format Hashicorp Configuration Language (HCL) files, this 11 | format is used by a number of Hashicorp tools, such as Terraform as the 12 | language used for configuration. 13 | 14 | The plugin by default will format `*.hcl`, `*.tf` and `*.nomad` files on save. 15 | Under the hood it uses [hclfmt](https://github.com/fatih/hclfmt) to process the 16 | files. 17 | 18 | ![hclfmt](http://g.recordit.co/fIQfohsGPI.gif) 19 | 20 | ## Usage 21 | 22 | Save the file or call `:HclFmt`. 23 | 24 | By default vim-hclfmt automatically formats *.hcl, *.tf and *.nomad files. You can permanently set this configuration in your `~/.vimrc` as follows: 25 | 26 | let g:hcl_fmt_autosave = 0 27 | let g:tf_fmt_autosave = 0 28 | let g:nomad_fmt_autosave = 0 29 | 30 | 31 | ## Install 32 | 33 | Vim-hclfmt follows the standard runtime path structure. For Pathogen just clone 34 | the repo. For other plugin managers add the appropriate lines and execute the 35 | plugin's install command. 36 | 37 | * [Pathogen](https://github.com/tpope/vim-pathogen) 38 | * `git clone https://github.com/fatih/vim-hclfmt.git ~/.vim/bundle/vim-hclfmt` 39 | * [vim-plug](https://github.com/junegunn/vim-plug) 40 | * `Plug 'fatih/vim-hclfmt'` 41 | * [NeoBundle](https://github.com/Shougo/neobundle.vim) 42 | * `NeoBundle 'fatih/vim-hclfmt'` 43 | * [Vundle](https://github.com/gmarik/vundle) 44 | * `Plugin 'fatih/vim-hclfmt'` 45 | 46 | If [hclfmt](https://github.com/fatih/hclfmt) is not already installed: 47 | 48 | go get github.com/fatih/hclfmt 49 | -------------------------------------------------------------------------------- /autoload/fmt.vim: -------------------------------------------------------------------------------- 1 | " fmt.vim: Vim command to format HCL files with hclfmt. 2 | 3 | if !exists('g:hcl_fmt_fail_silently') 4 | let g:hcl_fmt_fail_silently = 0 5 | endif 6 | 7 | if !exists('g:hcl_fmt_options') 8 | let g:hcl_fmt_options = '' 9 | endif 10 | 11 | " Below function is copied from vim-go's fmt.vim file. 12 | function! fmt#Format() 13 | if !executable("hclfmt") 14 | echo "hclfmt: could not find hclfmt. Please install it from github.com/fatih/hclfmt" 15 | return "" 16 | endif 17 | 18 | " save cursor position and many other things 19 | let l:curw=winsaveview() 20 | 21 | " Write current unsaved buffer to a temp file 22 | let l:tmpname = tempname() 23 | call writefile(getline(1, '$'), l:tmpname) 24 | 25 | let fmt_command = "hclfmt" 26 | 27 | " populate the final command with user based fmt options 28 | let command = fmt_command . ' -w ' . g:hcl_fmt_options 29 | 30 | " execute our command... 31 | let out = system(command . " " . l:tmpname) 32 | 33 | "if there is no error on the temp file replace the output with the current 34 | "file (if this fails, we can always check the outputs first line with: 35 | "splitted =~ 'package \w\+') 36 | if v:shell_error == 0 37 | " remove undo point caused via BufWritePre 38 | try | silent undojoin | catch | endtry 39 | 40 | " Replace current file with temp file, then reload buffer 41 | call rename(l:tmpname, expand('%')) 42 | silent edit! 43 | let &syntax = &syntax 44 | elseif g:hcl_fmt_fail_silently == 0 45 | echo out 46 | " We didn't use the temp file, so clean up 47 | call delete(l:tmpname) 48 | endif 49 | 50 | " restore our cursor/windows positions 51 | call winrestview(l:curw) 52 | endfunction 53 | 54 | 55 | " vim:ts=4:sw=4:et 56 | -------------------------------------------------------------------------------- /plugin/hcl.vim: -------------------------------------------------------------------------------- 1 | if get(g:, "hcl_fmt_autosave", 1) 2 | autocmd BufWritePre *.hcl call fmt#Format() 3 | endif 4 | 5 | if get(g:, "tf_fmt_autosave", 1) 6 | autocmd BufWritePre *.tf call fmt#Format() 7 | endif 8 | 9 | if get(g:, "nomad_fmt_autosave", 1) 10 | autocmd BufWritePre *.nomad call fmt#Format() 11 | endif 12 | 13 | command! -nargs=0 HclFmt call fmt#Format() 14 | 15 | 16 | " vim:ts=4:sw=4:et 17 | --------------------------------------------------------------------------------