Apply automatic pep8 fixes from QuickFix window Apply automatic pep8 fixes from QuickFix window python python

Apply automatic pep8 fixes from 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 DiffWithSaved function online quite a while ago and it has been very useful. In this case, you can just run autopep8 in the terminal, and when Gvim asks 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.

" copy this to your vimrc or source it" tells vim not to automatically reload changed filesset noautoread function! DiffWithSaved()  let filetype=&ft  diffthis  vnew | r # | normal! 1Gdd  diffthis  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetypeendfunction" sets up mappings to functioncom! DiffSaved call DiffWithSaved()map <Leader>ds :DiffSaved<CR>

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.

" run these commands after sourcing the above function" % expands to filename (also %:h to head, %:t to tail)" if it throws an error, just do :cd %:h first:!autopep8 --in-place %:DiffSaved

2. Diff with the git difftool and reload the file

If you want to diff with the file in the git index (and using git's difftool), you could do the following:

  1. leave gvim open,
  2. run your commands in the terminal and let the program open up a new instance of gvim (or vim) to handle the diffing.
  3. Save it all.
  4. Go back to your original gvim, let vim reload the file and (at least to my knowledge) your undo history should remain.

Advantages/Disadvantages

Option 1

Advantages:

  • each change will be saved in your undo history
  • graphical diffs in vim are easy to read

Disadvantages:

  • Won't be using git's difftool
  • relies upon vim's diff functions.

Option 2

Advantages:

  • uses git's difftool
  • cleaner undo history (single undo from pre and post autopep8--very dependent on what you want)

Disadvantages:

  • seems more awkward
  • less granular undo history