我最近从vim转换为emacs(spacemacs)。 Spacemacs附带yapf
作为python的标准代码reformatter工具。当代码被破坏时,我发现autopep8能更好地处理python代码。我不知道如何使autopep8重新格式化选定的区域,而不是整个缓冲区。在vim中,这相当于在选择或对象上运行gq
函数。我们如何在emacs/spacemacs中做到这一点?autopep8重新格式化emacs/spacemacs中的一个区域
4
A
回答
0
我不知道你是如何调用autopep8,但这个特殊的包装已经与该地区工作或标志着当前功能:https://gist.github.com/whirm/6122031
保存要点任何你保留个人elisp的代码,如~/elisp/autopep8.el
。
在.emacs
确保口齿不清目录是负载路径上,加载该文件,并覆盖键绑定:在主旨默认
(add-to-list 'load-path "~/elisp") ; or wherever you saved the elisp file
(require 'autopep8)
(define-key evil-normal-state-map "gq" 'autopep8)
的版本,如果没有区域是活跃的格式化当前功能。默认为整个缓冲区,改写这样的文件中的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或任何您保留自定义设置的位置。
相关问题
- 1. 格式化JSON日期格式化的日期基于区域
- 2. 保持文本区域格式化
- 3. 时刻格式化区域设置
- 4. Visual Studio中重新格式化我的预格式化HTML
- 5. 表单LoginUrl - 如何格式化此区域以使用区域?
- 6. NSDate格式导致重新格式化
- 7. 重新格式化输出中的Bash
- 8. 如何从(.net)应用程序中重新格式化分区?
- 9. MonoDevelop:html重新格式化?
- 10. 与重新格式化
- 11. 重新格式化日期
- 12. 重新格式化日期
- 13. 重新格式化查询
- 14. 重新格式化vb.net
- 15. 重新格式化数据
- 16. 重新格式化HTML
- 17. 重新格式化PHP
- 18. SimpleXML重新格式化
- 19. REGEX重新格式化
- 20. 重新格式化在MATLAB
- 21. 重新格式化JSON
- 22. JSON重新格式化
- 23. 重新格式化knockoutjs JSON
- 24. 如何将一个区域重定向到另一个区域
- 25. 重新格式化iPhone上的一个简单的HTML页面
- 26. 如何申请格式化多个div的文本区域。
- 27. 所有区域格式的NSDate格式
- 28. 将货币格式化为区域设置特定格式
- 29. 更新基于一个因子,价格区域在另一个
- 30. C#/。NET:重新格式化一个很长的字符串
请问您可以扩展您的答案,以包括这个函数应该放在.emacs.d /下的位置,以及它应该如何替换格式区域现有的键绑定邪恶''gq''。 – Meitham
最简单的事情是把它放在〜/ .emacs.d/init.el(或者〜/ .emacs,如果你使用的话)。如果你想把它放在一个单独的文件中,建议将它放在〜/ .emacs.d/lisp /或其他一些子目录中,而不是直接放在〜/ .emacs.d /中(不要忘记将它添加到负载路径)。至于rebinding,我不知道邪恶模式,但这看起来很有希望:http://stackoverflow.com/questions/19483278/bind-emacs-evil-window-commands-to-g-prefix。 – jpkotta