Skip to content
Vim plugin for shfmt (https://github.com/mvdan/sh)
Branch: master
Clone or download
alpayne and z0mbix Fix for issue 5 (Shfmt silently eats changes) (#6)
- changed call to shfmt to treat it as a filter instead a system call
  - process the current buffer instead of the saved file
  - write changed to STDOUT and use to replace buffer
- trap error if shfmt fails and undo change to prevent leaving the
  user staring at a blank buffer
- added restore of the cursor postion when filter returns (default
  leaves the user with cursor at top of file)
- reverted to using triggering autocmd on BufWritePre because we want
  filter to run before the file is written
  - note: if shfmt exits with an error on save the user isn't notified,
          but the file is saved as is without changes
Latest commit ab1d5eb Jan 26, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
plugin Fix for issue 5 (Shfmt silently eats changes) (#6) Jan 25, 2019
LICENSE Initial commit Mar 26, 2017
README.md Correct dos for setting default extra args Sep 27, 2017

README.md

Vim plugin for shfmt

The Vim shfmt plugin runs shfmt to auto format the current buffer

Requirements

You just need the shfmt command

Installation

Obtain a copy of this plugin and place shfmt.vim in your Vim plugin directory or be sensible and use something like Plug:

Plug 'z0mbix/vim-shfmt', { 'for': 'sh' }

Usage

You can use the :Shfmt command to run shfmt and automatically format the current buffer

You can also use the :Shfmt command together with options. For example,

:Shfmt -p
:Shfmt -i 2

Configuration

shfmt uses tabs by default for auto formatting, so if you prefer to use 2 spaces, you can set the following in your .vimrc file:

let g:shfmt_extra_args = '-i 2'

Auto format on save

If you would like to auto format shell scripts on save, you can add the following to your vim config:

let g:shfmt_fmt_on_save = 1

License

The Vim shfmt plugin is open-sourced software licensed under the MIT license.

You can’t perform that action at this time.