Autopep8 reformat region to emacs / spacemacs

I recently converted from vim to emacs (spacemacs). Spacemacs comes with yapfas standard text conversion tool for python. I believe autopep8 works better on Python code when the code breaks. I cannot figure out how to make autopep8 reformat the selected region, not the entire buffer. In vim, this is equivalent to running a function gqon a select or object. How do we do this in emacs / spacemacs?

+4
source share
1 answer

I don’t know how you call autopep8, but this particular shell already works with the region or marks the current function: https://gist.github.com/whirm/6122031

Keep the point wherever you save your elisp personal code, for example ~/elisp/autopep8.el.

In .emacsmake sure that your catalog is in lisp load path, loads the file and override key binding:

(add-to-list 'load-path "~/elisp") ; or wherever you saved the elisp file
(require 'autopep8)
(define-key evil-normal-state-map "gq" 'autopep8)

The default version in gist is for formatting the current function if no areas are active. By default, for the entire buffer, rewrite the autopep8 function to a file as follows:

(defun autopep8 (begin end)
  "Beautify a region of python using autopep8"
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (list (point-min) (point-max))))
  (save-excursion
    (shell-command-on-region begin end
                             (concat "python "
                                     autopep8-path
                                     autopep8-args)
                             nil t))))

, autopep8 Emacs. autopep8 Emacs , , , , , , , . C-h f autopep8, .

, autopep8 , , .

(define-key evil-normal-state-map "gq" 'autopep8-x)
(defun autopep8-x (begin end)
  "Wraps autopep8 from ??? to format the region or the whole buffer."
  (interactive
   (if mark-active
       (list (region-beginning) (region-end))
     (list (point-min) (point-max))))
  (autopep8 begin end)) ; assuming an existing autopep8 function taking
                        ; region arguments but not defaulting to the
                        ; whole buffer itself

.emacs .

0

Source: https://habr.com/ru/post/1618002/


All Articles