As mentioned in the comments be aware of the consequences this modification has to you work environment (know exactly which eslint rules you use to "fix" things).
To answer your question: adding the option to the eslint execution with syntactic is straight forward. Simply add this to your .vimrc
:
" execute eslint with --fix flag
let g:syntastic_javascript_eslint_args = ['--fix']
But as eslint is executed on the file after vim writes its buffer to the file, vim doesn't know about the fixes eslint does. So somehow vim has to load the file again (the manual way would be to simply type :e
in normal/command mode). To do that, we can use the autoread option together with checktime:
" enable autoread to reload any files from files when checktime is called and
" the file is changed
set autoread
To call checktime we can use an autocmd which gets executed every time a buffer is written to a file. But as syntactic is using its own autocmd on same event BufWritePost
we have to add our autocmd after syntactic adds its own. I found that this is the case when we add our autocmd on VimEnter
:
" add an autocmd after vim started to execute checktime for *.js files on write
au VimEnter *.js au BufWritePost *.js checktime
So after adding all three parts to your .vimrc
vim should behave as expected (lint and autocorrect on file save :w
).
map <C-j> call Function()<CR>
But this would be a very custom tailored function that you would need to write to suit your needs. And romainl's point is that if you do something wrong you can easily end up doing more harm than good. Caution is advisedvimdiff
against the original and the patched versions, rather than replace the original.