Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vim: How to run `standard --format` on `save` #195

Closed
daviddias opened this issue Jul 19, 2015 · 21 comments

Comments

@daviddias
Copy link

commented Jul 19, 2015

Following this thread, one of the features I've enjoyed a lot using vim-go was its ability to run gofmt on each save, saving me a ton of work. Would love to have this functionality for JavaScript with Standard codestyle, but I'm not sure how to do it or if there is an option available out there already. Thoughts?

Thank you.

@feross

This comment has been minimized.

Copy link
Member

commented Jul 23, 2015

As far as I know, Syntastic doesn't support formatters, so someone needs to write a vim plugin to do this.

@feross

This comment has been minimized.

Copy link
Member

commented Jul 23, 2015

@diasdavid There is a suggestion here for how to make a plugin. https://twitter.com/feross/status/624033217016930304

Looks like it might be a one-liner!

@caspervonb

This comment has been minimized.

Copy link

commented Jul 23, 2015

Minor variation, primarily calling it after the file has finished writing and making it a silent operation.

autocmd bufwritepost *.js silent !standard % --format

Tested in Vim 7.3, just append it to your .vimrc file and go.

@daviddias

This comment has been minimized.

Copy link
Author

commented Jul 23, 2015

image

This is very good stuff! Thank you!

We are almost there. Comparing to vim-go, the only thing missing is to do formatting without that confirmation. Would that be possible?

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Jul 23, 2015

Haven't verified if it works yet, but vim-autoformat was updated recently to support standard (and other linters).

@caspervonb

This comment has been minimized.

Copy link

commented Jul 23, 2015

That's with the post write event? was getting it with bufwrite but not with bufwritepost

@caspervonb

This comment has been minimized.

Copy link

commented Jul 23, 2015

If thats still the case, :set autoread should do the trick and disable the W11 warning, always re-reeading the file.

@daviddias

This comment has been minimized.

Copy link
Author

commented Jul 23, 2015

rad! it works :D

autocmd bufwritepost *.js silent !standard % --format
set autoread
@caspervonb

This comment has been minimized.

Copy link

commented Jul 23, 2015

Just a side note, : is referring to when used as a command (like you'd denote shell commands with $ ...) In a vim script (like .vimrc) it should just be set autoread ;)

@feross

This comment has been minimized.

Copy link
Member

commented Jul 24, 2015

Sweet! Glad you guys got it figured out. Thanks for the help @caspervonb!

I'll add this snippet to the readme. Should someone publish this as a standalone vim plugin? I'm not sure if the community does small modules like npm does ;)

@feross feross closed this Jul 24, 2015

@feross

This comment has been minimized.

Copy link
Member

commented Jul 24, 2015

There was a suggestion on Twitter to persist cursor position as well: https://twitter.com/oObsi/status/624533766434910208

I haven't tested the snippet out yet. Is this actually necessary?

@cryptix

This comment has been minimized.

Copy link

commented Jul 24, 2015

IIRC from some buffer replacement hacking, your cursor gets reset to line 0 but i might be or did things wrong.

@daviddias

This comment has been minimized.

Copy link
Author

commented Jul 24, 2015

If there are 'errors' that --format can't fix, like non used vars, the autoformat gets weird:

image

@caspervonb

This comment has been minimized.

Copy link

commented Jul 25, 2015

That's "expected", but i did not expect standard to lint and format at the same time.
@feross any way to suppress errors and just format?

@feross

This comment has been minimized.

Copy link
Member

commented Jul 26, 2015

@caspervonb Can't you just ignore stdout? The formatting happens in place to the file on disk, so it's safe to just throw away that output if you don't want to show it in the UI.

I just published 5.0.0-6 which makes sure that all expected linter output goes to stdout.

@feross

This comment has been minimized.

Copy link
Member

commented Jul 26, 2015

@caspervonb Also, if it's helpful, you can format a file by piping it to stdin using standard --format --stdin. The transformed code is written to stdout. Lint errors are written to stderr which can be safely ignored.

@caspervonb

This comment has been minimized.

Copy link

commented Jul 26, 2015

@caspervonb Can't you just ignore stdout? The formatting happens in place to the file on disk, so it's safe to just throw away that output if you don't want to show it in the UI.

@feross true you could just muffle it by redirection.

autocmd bufwritepost *.js silent !standard % --format > /dev/null

I just published 5.0.0-6 which makes sure that all expected linter output goes to stdout.

@feross did you mean stderr here? (because of below)

@caspervonb Also, if it's helpful, you can format a file by piping it to stdin using standard --format --stdin. The transformed code is written to stdout. Lint errors are written to stderr which can be safely ignored.

For vim, I think it's better to just replace do it externally and reload the buffer, don't want to mess up the command history too much.

@don-smith

This comment has been minimized.

Copy link

commented Feb 12, 2016

Now that the formatting feature of standard has been moved into standard-format, has anyone worked out how to apply this in vim?

@caspervonb

This comment has been minimized.

Copy link

commented Feb 12, 2016

@locksmithdon What is the difference? just invoke it as you would from a shell basically.

@don-smith

This comment has been minimized.

Copy link

commented Feb 12, 2016

@caspervonb I was missing -w and it wasn't working. I know, RTFM 😉 This works:

autocmd bufwritepost *.js silent !standard-format -w %
set autoread
@tim-phillips

This comment has been minimized.

Copy link

commented Oct 31, 2017

Just to close the loop since I got here from google, this is explained in the docs:

https://standardjs.com/#vim

@feross feross added enhancement and removed enhancement labels May 10, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Aug 8, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
7 participants
You can’t perform that action at this time.