z0mbix / vim-shfmt

Vim plugin for shfmt (https://github.com/mvdan/sh)
MIT License
132 stars 11 forks source link

Auto fmt on save? #1

Closed soulshake closed 6 years ago

soulshake commented 7 years ago

Thanks, this is a useful plugin!

It would be super awesome if this could (optionally) auto format when a file is saved, similar to vim-terraform's g:terraform_fmt_on_save.

z0mbix commented 7 years ago

I did get this working but never committed/documented it. I'll take another look in to it when I can find some free time.

z0mbix commented 6 years ago

@soulshake sorry it took a while, can you please let me know if it works OK for you?

let g:shfmt_fmt_on_save = 1

Works on my machine

I've also standardised the variable naming so if you were usingvimshfmt_extra_args, please update to just shfmt_extra_args.

soulshake commented 6 years ago

It works! :tada:

I was using let g:shfmt_switches = ['-i 2'] in my .vimrc. Once I replaced this with let g:shfmt_extra_args = '-i 2' it worked as expected.

I'll use it for a while and let you know if I notice any unexpected behavior.

Thank you!

z0mbix commented 6 years ago

Excellent!

z0mbix commented 6 years ago

I’ll fix the README tomorrow, can’t belive I missed that!

alecandido commented 3 years ago

Sorry to necrobump: for some reason here it's not working.

Maybe I'm stupid and I made some trivial mistake, but I simply put in my .vim/ftplugin/sh.vim:

let g:shfmt_fmt_on_save = 1

augroup LocalShell
    autocmd!

    autocmd BufWritePre * Shfmt
augroup END

The first one alone it's not working, the second one yes (of course even alone).

Tested with both vim and nvim, the plugin has been installed with vim-plug. If I'll try to fix I'll update as soon as I find something useful.