Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Linting your code, the vanilla way

Linting your code, the vanilla way

You may want a lint plugin to lint your code in Vim but you probably don't need it. At least try the built-in way before jumping on the plugin bandwagon.

Defining makeprg

autocmd FileType <filetype> setlocal makeprg=<external command>

This autocommand tells Vim to use <external command> when invoking :make % in a <filetype> buffer. You can add as many similar lines as needed for other languages.

Example:

autocmd FileType python setlocal makeprg=pylint\ --output-format=parseable
autocmd FileType javascript setlocal makeprg=eslint\ --format\ compact

Automatic execution on :write

autocmd BufWritePost <pattern> silent make! <afile> | silent redraw!

This autocommand tells Vim to run :make on the current file matching <pattern> whenever you :write it. You can add patterns if you want that to happen with other filetypes.

Example:

autocmd BufWritePost *.py,*.js silent make! <afile> | silent redraw!

Automatic opening of the quickfix window

This autocommand tells Vim to open the quickfix window whenever a quickfix command is executed (like :make) AND there are valid errors to display.

autocmd QuickFixCmdPost [^l]* cwindow
augroup Linting
autocmd!
autocmd FileType python setlocal makeprg=pylint\ --output-format=parseable
autocmd BufWritePost *.py silent make! <afile> | silent redraw!
autocmd QuickFixCmdPost [^l]* cwindow
augroup END
@w0rp

This comment has been minimized.

Copy link

w0rp commented Nov 15, 2017

Now try using this to check your code with tsserver or flow.

@lewis6991

This comment has been minimized.

Copy link

lewis6991 commented Dec 11, 2017

Any tips for populating the signs column without a plugin?

@iovis

This comment has been minimized.

Copy link

iovis commented Dec 12, 2017

TIL about [^l]*, very interesting gist!

@alcarney

This comment has been minimized.

Copy link

alcarney commented Dec 19, 2017

Using this approach how would you go about using multiple linting programs per file type, say flake8 and mypy for python for example?

@romainl

This comment has been minimized.

Copy link
Owner Author

romainl commented Jan 5, 2018

@lewis6991, the "interface" for managing signs is way too messy. Defining, placing, and removing a sign is not particularly hard but you'll have to manage them and sync them with the content of the quickfix list and/or the content of any location list. You really need some non-trivial scripting for getting it to work.

See :help sign-support.

@romainl

This comment has been minimized.

Copy link
Owner Author

romainl commented Jan 5, 2018

@alcarney, I would offset as much of the responsibility as possible to external tools. In this case, I would write a short shell script that aggregates the output of those tools and point Vim at it.

@bfrg

This comment has been minimized.

Copy link

bfrg commented Feb 17, 2018

@lewis6991, maybe you can use this as a starting point: https://github.com/mh21/errormarker.vim
It's a very simple plugin under 200 lines.

@Konfekt

This comment has been minimized.

Copy link

Konfekt commented Jun 25, 2018

@alcarney, see :help compiler. You add for every compiler cmp a configuration file ~/.vim/compilers/cmp.vim, and invoke them by :Compiler cmp.

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