十个高效的spacemacs配置

使用emacs一段时间了,总结出自己实践的一些快捷配置,我用的版本是自己编译的24.5。

快速切换至上个buffer

切换缓冲区C-x b RET还是有点繁琐,当我在两个缓冲区间快速编辑的时候,我希望一个动作就能完成切换,所以我设置Shift+TAB切换至上个buffer

(global-set-key (kbd "<backtab>") #‘(lambda ()
                                      (interactive)
                                      (switch-to-buffer (other-buffer (current-buffer) 1))))

键盘宏的录制和回放

默认F3是录制宏,F4是停止录制,C-x x回放宏

(global-set-key (kbd "C-x x") ‘call-last-kbd-macro)

逗号后自动加空格

(global-set-key (kbd ",")
                #‘(lambda ()
                    (interactive)
                    (insert ", ")))

使用yasnippet复制当前行的编辑模式

在多数编程语言中,相邻行的代码的冗余度是非常高的,下面的C-c TAB将当前行转化为临时的snippet模板,非常好用(依赖:yasnippet)

你可以尝试一下,编辑go代码是,在fmt.Prinln("hello world")上按C-c TAB是什么效果

(defun spacemacs/wsk-yasnippet-current-line ();; C-c TAB
  (interactive)
  (let ((current-line (string-trim-right (thing-at-point ‘line t))))
    (end-of-line)
    (newline-and-indent)
    (yas-expand-snippet (spacemacs//wsk-yasnippet-string-to-template (string-trim current-line)))))

(defun spacemacs//wsk-yasnippet-string-to-template (string)
  (let ((count 1))
    (labels ((rep (text)
                  (let ((replace (format "${%d:%s}" count text)))
                    (incf count)
                    replace)))
      (replace-regexp-in-string "[a-zA-Z0-9]+" #‘rep string))))
      
(global-set-key (kbd "C-c TAB") ‘spacemacs/wsk-yasnippet-current-line)

显示行号,括号匹配,高亮括号配对

;; 显示行号
(global-linum-mode)
(setq column-number-mode t)

;; 高亮括号配对
(electric-pair-mode)

;; 高亮括号配对
(show-paren-mode t)
(setq show-paren-style ‘parenthesis)

undo-tree默认撤消一次

undo-tree非常棒,但是大多数时候我们只需要撤消一次,你需要按C-x u p RET,下面的绑定使得undo-tree默认处于撤消一次的状态上C-x u RET,少按一次p。我感觉对于撤消一次的情形应该单独设置一个快捷键,但好在我用撤消的次数并不多。

(require ‘undo-tree)
(define-key undo-tree-map (kbd "C-x u") #‘(lambda ()
                                            (interactive)
                                            (undo-tree-visualize)
                                            (undo-tree-visualize-undo)))

speedbar

老实说,spacemacs内置了helm后,我不怎么用speedbar了。speedbar的问题是他创建了一个缓冲区,而这个缓冲区对于C-x b是可见的,我从来都不想切换至speedbar缓冲区,所以我写了下面的代码,按F2切换speedbar的开启状态,并且自动刷新,当你不希望显示speedbar的时候,干脆把buffer给kill掉。

注:我用了sr-speedbar

(require ‘sr-speedbar)
(global-set-key (kbd "<f2>") (lambda()
                               (interactive)
                               (sr-speedbar-refresh)
                               (sr-speedbar-toggle)
                               (unless (sr-speedbar-exist-p)
                                 (kill-buffer "*SPEEDBAR*"))))

s键在speedbar中搜索

(require ‘speedbar)
(define-key speedbar-mode-map (kbd "s")
  #‘(lambda ()
      (interactive)
      (beginning-of-buffer)
      (isearch-forward)))
(define-key speedbar-mode-map (kbd "u")
  #‘(lambda ()
      (interactive)
      (speedbar-up-directory)))

dot-spacemacs

配置elpa

把下面的代码放进spacemacs/init中

  (setq package-archives ‘(("gnu" . "http://elpa.gnu.org/packages/")
                           ("marmalade" . "https://marmalade-repo.org/packages/")
                           ("melpa" . "http://melpa.org/packages/")))

jazz颜色主题

jazz是一个暗色的主题,leuven则是亮版的代表,对Org Mode支持出奇的好,可惜在spacemacs下表现不佳。这些代码放在set-default里面

   dotspacemacs-themes ‘(jazz
                         solarized-dark
                         leuven
                         solarized-light)

字体

我这里字体设置的有点大了,没办法,打游戏眼有点花,字体需要在spacemacs项目上找下载地址自行安装,话说我也喜欢exvim,但自从有了spacemacs,你懂的。

   dotspacemacs-default-font ‘("Source Code Pro"
                               :size 24
                               :weight normal
                               :width normal
                               :powerline-scale 1.1)

将private-layer挪出来

默认在~/.emacs.d/private目录下,那太傻,我根本不动spacemacs的源码,都是自己创建init-package-layer

configuration-layer-private-directory "~/dot-spacemacs/"

设置专属yasnippet模板

默认spacemacs带了一堆snippet模板,老实说我都不敢按TAB,谁知道会冒出些什么,于是我干脆自定义了snippet模板路径,所有的模板都改为大写,比如在golang里面fmt.Println,模板缩写是PLN。全部改为大写后,误操作少了很多。

      (require ‘yasnippet)
      (setq yas-snippet-dirs "~/dot-spacemacs/snippets")
      (yas-global-mode 1)
      (yas-reload-all)

Org Mode

老实说外行用emacs第一动力应该是org mode,但org mode范围很广,强烈推荐看官方manual,下面是官方推荐的设置,我觉得太赞了。

(global-set-key "\C-cl" ‘org-store-link)
(global-set-key "\C-cc" ‘org-capture)
(global-set-key "\C-ca" ‘org-agenda)
(global-set-key "\C-cb" ‘org-iswitchb)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。