Background:
I’m using the (fantastic) Vim plugin python-mode, which includes the pep8 linter. The :PyLint command runs all linters and opens errors in a QuickFix window.
Problem:
Now, let’s assume I’m only using the pep8 linter, and I have a QuickFix window full of errors. I’d like to step through each of these errors and apply an automatic fix (with something like autopep8). The autopep8 tool is fantastic, but it makes mistakes. Ideally, I’d like to be able to supervise each fix in Vim (apply fix, check, move to next fix).
My current approach is to run autopep8 on my Python file, diff the results, then repair any bad changes:
$ autopep8 --in-place spam.py
$ git difftool spam.py # check edits in gVim, write to file
$ git commit spam.py -m "Fix bad PEP8 formatting"
However, this approach ruins my undo history, and seems needlessly complex. Is there a better way?
Question:
Is there any way to automatically apply pep8 fixes (when available) to pep8 errors within the QuickFix window?
Options
There are two simple answers that won’t wipe out your undo history.
1. Diff with the saved file in Vim
I found this
DiffWithSavedfunction online quite a while ago and it has been very useful. In this case, you can just run autopep8 in the terminal, and whenGvimasks to reload the file, choose no and then run this function, which will pop up a scratch buffer with your new file and allow you to change things around.Once you run that, you can use the vim copy-diff and other diff commands to quickly go through and accept/not accept changes. Plus, all will be stored in undo history.
2. Diff with the git
difftooland reload the fileIf you want to diff with the file in the git index (and using git’s difftool), you could do the following:
Advantages/Disadvantages
Option 1
Advantages:
Disadvantages:
difffunctions.Option 2
Advantages:
Disadvantages: