我最近从vim转到了emacs (Emacs)。Spacemacs附带了yapf作为python的标准代码重新格式化工具。当python代码崩溃时,我发现autopep8在python代码上工作得更好。我不知道如何让autopep8重新格式化选定的区域,而不是整个缓冲区。在vim中,这相当于在选择或对象上运行gq函数。我们如何在emacs/spacemacs中做到这一点?
发布于 2015-12-10 01:58:14
我不知道您是如何调用autopep8的,但是这个特定的包装器已经对区域起作用了,或者标记了当前函数:https://gist.github.com/whirm/6122031
将gist保存在保存personal elisp代码的地方,例如~/elisp/autopep8.el。
在.emacs中,确保lisp目录位于加载路径上,加载文件,然后覆盖键绑定:
(add-to-list 'load-path "~/elisp") ; or wherever you saved the elisp file
(require 'autopep8)
(define-key evil-normal-state-map "gq" 'autopep8)如果没有活动的区域,则gist中的版本默认为格式化当前函数。要缺省为整个缓冲区,请重写文件中的autopep8函数,如下所示:
(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))))上面的设置假设您是在Emacs中从头开始使用autopep8。如果你已经在Emacs中有了来自其他包的autopep8,那么如何定制它的最终答案将取决于代码的来源以及它支持的参数和变量。键入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中,也可以放在保存自定义的任何地方。
https://stackoverflow.com/questions/34002435
复制相似问题