Last active
June 17, 2022 11:16
-
-
Save kracekumar/77d29c7410199fd2cda4 to your computer and use it in GitHub Desktop.
Emacs auto remove unused import statements in current python file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; auto-remove.el --- Auto remove unused functions in python | |
;;; Commentary: | |
;; Uses external tool autoflake to remove unused imports from a Python file. | |
;;; Code: | |
(defcustom python-autoflake-path (executable-find "autoflake") | |
"Autoflake executable path. | |
Allows working with a virtualenv without actually adding support | |
for it." | |
:group 'python | |
:type 'string) | |
(defun python-remove-unused-imports () | |
"Use Autoflake to remove unused function. | |
$ autoflake --remove-all-unused-imports -i unused_imports.py" | |
(interactive) | |
(when (eq major-mode 'python-mode) | |
(shell-command (format "%s --remove-all-unused-imports -i %s" | |
python-autoflake-path | |
(shell-quote-argument (buffer-file-name)))) | |
(revert-buffer t t t)) | |
nil) | |
(eval-after-load 'python | |
'(if python-autoflake-path | |
(add-hook 'before-save-hook 'python-remove-unused-imports) | |
(message "Unable to find autoflake. Configure `python-autoflake-path`"))) | |
(provide 'auto-remove) | |
;;; auto-remove.el ends here |
Guys - this would be more useful if you could provide the full path to autoflake script in the comments of auto-remove.el . I am right now struggling to find autoflake, though I guess I wrote the original version of it :)
@pythonhacker You can do (setq python-autoflake-path "/this/is/where/autoflake/is.py")
PS: We made a repo https://github.com/kracekumar/missing-python-mode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See patch 4d6871d here https://gist.github.com/jaseemabid/3def1e104cbff09ad803