因为 Emacs 的每个 major mode 所使用的缩进配置都不一样,有时候我们只想像其他编辑器一样使用 TAB 来缩进,Shift+TAB 向后缩进。
往你的配置中添加以下代码就可以啦:
;; my fix for tab indent
(defun zilongshanren/indent-region(numSpaces)
(progn
; default to start and end of current line
(setq regionStart (line-beginning-position))
(setq regionEnd (line-end-position))
; if there's a selection, use that instead of the current line
(when (use-region-p)
(setq regionStart (region-beginning))
(setq regionEnd (region-end))
)
(save-excursion ; restore the position afterwards
(goto-char regionStart) ; go to the start of region
(setq start (line-beginning-position)) ; save the start of the line
(goto-char regionEnd) ; go to the end of region
(setq end (line-end-position)) ; save the end of the line
(indent-rigidly start end numSpaces) ; indent between start and end
(setq deactivate-mark nil) ; restore the selected region
)
)
)
(defun zilongshanren/tab-region ()
(interactive "p")
(if (use-region-p)
(zilongshanren/indent-region 4) ; region was selected, call indent-region
(insert " ") ; else insert four spaces as expected
))
(defun zilongshanren/untab-region ()
(interactive "p")
(zilongshanren/indent-region -4))
(defun zilongshanren/hack-tab-key ()
(interactive)
(local-set-key (kbd "<tab>") 'zilongshanren/tab-region)
(local-set-key (kbd "<S-tab>") 'zilongshanren/untab-region)
)
(add-hook 'prog-mode-hook 'zilongshanren/hack-tab-key)
Ref: Emacs Shift-Tab to left shift the block - Stack Overflow