[Maas-devel] Formatting Python imports in Emacs

Newell Jensen newell.jensen at canonical.com
Thu Nov 20 16:23:29 UTC 2014


Installed and like it.  Thanks Gavin :)

On Thu, Nov 20, 2014 at 6:35 AM, Gavin Panella <gavin.panella at canonical.com>
wrote:

> If you're an Emacs user, here's a convenient function to reformat Python
> imports in the current buffer:
>
> (defun python-format-imports ()
>   "Reformats Python module-level imports in the current buffer."
>   (interactive)
>   (save-excursion
>     (save-restriction
>       (let ((temp-file (make-temp-file ".python-format-imports.")))
>         (unwind-protect
>             (progn
>               (write-region nil nil temp-file nil 0)
>               (call-process "format-imports" nil nil nil temp-file)
>               (insert-file-contents temp-file nil nil nil t))
>           (delete-file temp-file))))))
>
> I already have format-imports on my PATH; you might want to do that, or
> change the call-process line above.
>
> I've bound it to C-c C-f when in python-mode:
>
> (add-hook
>  'python-mode-hook
>  '(lambda () "Configure python-mode."
>     (define-key
>       python-mode-map (kbd "C-c C-f")
>       'python-format-imports)
>     ))
>
> After formatting imports you must save the buffer; it doesn't
> automatically save for you, by design. However, if no imports need
> formatting your buffer won't be marked dirty.
>
> --
> Mailing list: https://launchpad.net/~maas-devel
> Post to     : maas-devel at lists.launchpad.net
> Unsubscribe : https://launchpad.net/~maas-devel
> More help   : https://help.launchpad.net/ListHelp
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/maas-devel/attachments/20141120/a49151e9/attachment.html>


More information about the Maas-devel mailing list