如何让emacs转换为将所选文本向左移动4个空格?
答案 0 :(得分:49)
为此,我使用绑定到indent-rigidly
的命令C-x TAB
。给它参数-4将选定区域向左移动四个空格:C-u -4 C-x TAB
。
http://www.gnu.org/s/emacs/manual/html_node/elisp/Region-Indent.html
答案 1 :(得分:12)
这将从当前行的前面删除4个空格(如果存在空格)。
(global-set-key (kbd "<S-tab>") 'un-indent-by-removing-4-spaces)
(defun un-indent-by-removing-4-spaces ()
"remove 4 spaces from beginning of of line"
(interactive)
(save-excursion
(save-match-data
(beginning-of-line)
;; get rid of tabs at beginning of line
(when (looking-at "^\\s-+")
(untabify (match-beginning 0) (match-end 0)))
(when (looking-at "^ ")
(replace-match "")))))
如果你的变量tab-width
碰巧是4(这就是你要“撤消”的那个),那么你可以用(looking-at "^ ")
这样更通用的东西替换(concat "^" (make-string tab-width ?\ ))
。
此外,代码会使用untabify将行前面的标签转换为空格。
答案 2 :(得分:8)
我制作了一些功能,用于向左或向右四个空格标记区域,具体取决于您是使用制表符还是Shift +制表符:
(defun indent-region-custom(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 untab-region (N)
(interactive "p")
(indent-region-custom -4)
)
(defun tab-region (N)
(interactive "p")
(if (active-minibuffer-window)
(minibuffer-complete) ; tab is pressed in minibuffer window -> do completion
; else
(if (string= (buffer-name) "*shell*")
(comint-dynamic-complete) ; in a shell, use tab completion
; else
(if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion
(indent-region-custom 4) ; region was selected, call indent-region
(insert " ") ; else insert four spaces as expected
)))
)
(global-set-key (kbd "<backtab>") 'untab-region)
(global-set-key (kbd "<tab>") 'tab-region)
编辑:添加了Maven的代码以检查是否在迷你缓冲区中,以及特定缓冲区中的标签完成情况(例如 shell )。
答案 3 :(得分:1)
我对Stanley Bak的回答有所改进。 对我来说,这个全局密钥绑定搞乱了迷你缓冲区的完成。
所以我也为迷你缓冲区提供了一个案例。
修改:重命名indent-region
- &gt; indent-region-custom
。
indent-region
与现有命令发生冲突,并为save(保存前的hook)和其他一些组合键提供了缩进错误。
(defun indent-region-custom(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 untab-region (N)
(interactive "p")
(indent-region-custom -4)
)
(defun tab-region (N)
(interactive "p")
(if (active-minibuffer-window)
(minibuffer-complete) ; tab is pressed in minibuffer window -> do completion
(if (use-region-p) ; tab is pressed is any other buffer -> execute with space insertion
(indent-region-custom 4) ; region was selected, call indent-region-custom
(insert " ") ; else insert four spaces as expected
)
)
)
(global-set-key (kbd "<backtab>") 'untab-region)
(global-set-key (kbd "<tab>") 'tab-region)