subramaniank / dotfiles

favourite dotfiles across platforms and programs
MIT License
0 stars 0 forks source link

Pep8 #1

Open ScriptDevil opened 9 years ago

ScriptDevil commented 9 years ago
  1. Install autopep8 pip install autopep8
  2. *Add this to your .emacs
    (setq autopep8-comand "~/env/bin/autopep8")

    (defun my-pep8 ()
      (interactive)

      ;; run autopep8 on file
      (shell-command (concat autopep8-path " -i " f) nil t)
      ;; load back the changes
      (revert-buffer t t))

    (if (file-exists-p autopep8-path)
        (add-hook 'after-save-hook
              (lambda ()
            (let ((f (buffer-file-name)))
              (if (string-suffix-p ".py" f)
                  (my-pep8))))))

When you want to stylize the file you are currently editing, M-x my-pep8. Bind it to a key if you will. Else, if you want to run it each time on any python file is saved.

Note: I have used --ignore errors to ensure it doesn't remove erroneous lines just in case you have a habit of saving every time. It might still crap out. Use the optional part with care, though. Of course undo will still work.

subramaniank commented 8 years ago

@ScriptDevil Dei! i get stuck on the autopep8-path. how do you manage that ? MELPA ? and is it py-autopep8 ?

ScriptDevil commented 8 years ago

You told me you use virtualenv, right?

  $ pip install autopep8

Use /full/path/to/virtualenv/bin/

subramaniank commented 8 years ago

@ScriptDevil no that's all fine.. for elisp to understand "autopep8-path" and "autopep8-command" don't i need something ?

ScriptDevil commented 8 years ago

Oh, that was a typo. The first line should also be (setq autopep8-path ..)

It is just a variable I created on the fly. Emacs doesn't really "understand" it. I understand where your confusion stems from. Mysterious action at a distance...