本文主要记录spacemacs配置c-c++编辑器,Python编辑器,web前端编辑器;以备以后翻阅
c-c++配置
使用c-c++
layer及company mode
是必须的,不需要再说了
company-c-headers
在安装c-c++
layer时,会自动安装该package,我们只需要配置下头文件目录,如果大型工程,涉及上百万行代码的,使用semantic可能会比较卡,这时候其实只要使用company-c-headers
package,就已经能实现较为不错的函数自动补全功能
(with-eval-after-load 'company-c-headers
(add-to-list 'company-c-headers-path-system "/usr/include/c++/4.2.1")
)
(add-hook 'c++-mode-hook
(lambda ()
(set (make-local-variable 'company-backends) '(company-semantic company-c-headers company-dabbrev-code company-gtags company-c-headers company-keywords company-files company-dabbrev))
)
)
ggtags
- 当代码量比较大时,推荐使用ggtags进行静态代码补全即可,需要安装一个
gnu global
(brew install global),再配置一下更方便的快捷键(我一般绑定s-g *
)和一些个人设置
(setq
helm-gtags-ignore-case t
helm-gtags-auto-update t
helm-gtags-use-input-at-cursor t
helm-gtags-pulse-at-cursor t
helm-gtags-prefix-key "\C-cg"
helm-gtags-suggested-key-mapping t
)
(set 'helm-gtags-mode t)
(with-eval-after-load 'helm-gtags
(define-key helm-gtags-mode-map (kbd "s-g s") 'helm-gtags-select)
(define-key helm-gtags-mode-map (kbd "s-g r") 'helm-gtags-find-rtag)
(define-key helm-gtags-mode-map (kbd "s-g f") 'helm-gtags-find-symbol)
(define-key helm-gtags-mode-map (kbd "s-<") 'helm-gtags-previous-history)
(define-key helm-gtags-mode-map (kbd "s->") 'helm-gtags-next-history)
(define-key helm-gtags-mode-map (kbd "s-,") 'helm-gtags-pop-stack)
(define-key helm-gtags-mode-map (kbd "s-g c") 'helm-gtags-create-tags)
(define-key helm-gtags-mode-map (kbd "s-g D") 'helm-gtags-dwim-other-window)
(define-key helm-gtags-mode-map (kbd "s-g d") 'helm-gtags-dwim)
(define-key helm-gtags-mode-map (kbd "s-g t") 'helm-gtags-tags-in-this-function)
(define-key helm-gtags-mode-map (kbd "s-g k") 'helm-gtags-show-stack)
(define-key helm-gtags-mode-map (kbd "s-g a") 'helm-gtags-clear-stack)
)
- 全工程搜索关键字,需要安装
ag
(brew install ag)搜索引擎,再配置一下快捷键(我一般绑定s-h *
)
(with-eval-after-load 'helm-gtags
(define-key c++-mode-map (kbd "s-h d") 'helm-ag)
(define-key c++-mode-map (kbd "s-h t") 'helm-ag-this-file)
(define-key c++-mode-map (kbd "s-h b") 'helm-ag-buffers)
(define-key c++-mode-map (kbd "s-h p") 'helm-ag-project-root)
(define-key c++-mode-map (kbd "s-h D") 'helm-do-ag)
(define-key c++-mode-map (kbd "s-h T") 'helm-do-ag-this-file)
(define-key c++-mode-map (kbd "s-h B") 'helm-do-ag-buffers)
(define-key c++-mode-map (kbd "s-h P") 'helm-do-ag-project-root)
(define-key c++-mode-map (kbd "s-h c") 'helm-ag-clear-stack)
)
clang-format
配置clang-format自动排版
-
首先在自己的代码工程根目录创建
.clang-format
文件 -
配置clang-format快捷键
(defun clang-format-bindings-cpp ()
(define-key c++-mode-map (kbd "s-c f") 'clang-format-region)
(define-key c++-mode-map (kbd "s-c b") 'clang-format-buffer)
)
(add-hook 'c++-mode-hook 'clang-format-bindings-cpp)
- 配置save保存时自动调用clang-format对代码进行排版再保存
;; 调用该函数在自动保存c-c++文件时,会检测.clang-format函数,有则自动排版,没有则不
(defun pan/clang-format-buffer-smart ()
"Reformat buffer if .clang-format exists in the projectile root."
(interactive)
(when (f-exists? (expand-file-name ".clang-format" (projectile-project-root)))
(clang-format-buffer)))
;; 保存时自动排版文件
(defun my-c++-mode-before-save-hook ()
(when (or (eq major-mode 'c-mode) (eq major-mode 'c++-mode) )
(message "Begin to format buffer by .clang-format!")
(pan/clang-format-buffer-smart)))
(add-hook 'before-save-hook #'my-c++-mode-before-save-hook)
hideshow
使用hideshow折叠代码
(with-eval-after-load 'hideshow
(define-key c-mode-map (kbd "s-c h") 'hs-toggle-hiding)
(define-key c-mode-map (kbd "s-c a") 'hs-hide-all)
(define-key c-mode-map (kbd "s-c l") 'hs-hide-level)
)
c-c++ package###
使用c-c++ package自带的clang-support补全代码,包括头文件函数,结构体成员变量自动提示,c++函数、成员变量等自动提示,基本和IDE提供的自动提示功能无异
(c-c++ :variables
c-c++-default-mode-for-headers 'c++-mode
c-c++-enable-clang-support t)
(auto-completion :variables
auto-completion-enable-snippets-in-popup t
auto-completion-enable-help-tooltip t
auto-completion-enable-sort-by-usage t
)
(auto-completion :variables
auto-completion-enable-snippets-in-popup t
auto-completion-enable-help-tooltip t
auto-completion-enable-sort-by-usage t
)
python
anaconda mode
python layer
安装就不说了,主要记录下(anaconda mode)[https://github.com/proofit404/anaconda-mode],代码提示就靠他了
如果安装anaconda-mode
代码提示有问题,注意以下几点
cli
和emacs
识别到的系统默认python版本可能不一致,emacs
貌似是python3
优先的,具体原因还没找到- 如果代码提示的时候发现一些系统自带的库可以提示,但是自己通过
pip
或者其他途径安装的包无法提示,可以参考下(emacs-china)[https://emacs-china.org/t/topic/6104/15] - 使用
virtual environment
有好处,但是我还是不太会用
(add-hook 'python-mode-hook
(lambda ()
(set (make-local-variable 'company-backends) '(company-anaconda company-dabbrev))
(local-set-key (kbd "c-r") 'anaconda-mode-find-references)
(local-set-key (kbd "c-d") 'anaconda-mode-find-definitions)
(local-set-key (kbd "c-u") 'anaconda-mode-find-assignments)
(local-set-key (kbd "c-y") 'anaconda-mode-show-doc)
(local-set-key (kbd "s-i") 'python-start-or-switch-repl)
(local-set-key (kbd "s-n") 'python-shell-send-buffer)
(local-set-key (kbd "s-N") 'python-shell-send-buffer-switch)
(local-set-key (kbd "s-m") 'python-shell-send-defun)
(local-set-key (kbd "s-M") 'python-shell-send-defun-switch)
(local-set-key (kbd "s-r") 'python-shell-send-region)
(local-set-key (kbd "s-R") 'python-shell-send-region-switch)
(company-quickhelp-mode)
))
(add-hook 'python-mode-hook 'anaconda-mode)
(add-hook 'python-mode-hook 'eldoc-mode)
go
- 通过
brew install go
安装go编译环境 - 安装go layer,官方有详细文档
但是在安装一些go工具包(例如guru,gorename,goimports等)时,可能会遇到一点问题,即使我用了科学上网,依旧无法成功安装,但我我们可以把包下载了,自己手动安装,步骤如下
-
确定你的
gopath
路径,例如我的是/Users/pan/go
,将如下语句加入~/.bashrc_profile
export PATH=$PATH:/usr/local/opt/go/libexec/bin export GOPATH=/Users/pan/go PATH="$GOPATH/bin:$PATH"
-
再在bash中执行以下命令
git clone https://github.com/golang/tools.git tools
mkdir -p /Users/pan/go/golang.org/x/tools
cp -r tools/* /Users/pan/go/golang.org/x/tools
go install golang.org/x/tools/cmd/guru
go install golang.org/x/tools/cmd/gorename
go install golang.org/x/tools/cmd/goimports