Emacs Shift Tab向左移动块

Emacs Shift Tab向左移动块,emacs,Emacs,如何让Emacs移动到ShiftTab以将所选文本向左移动4个空格?这将从当前行的前面删除4个空格(前提是存在空格) (全局设置键(kbd“”)“取消缩进-删除-4-空格) (通过删除4个空格() “从行首删除4个空格” (互动) (省去远足 (保存匹配数据 (行首) 去掉行首的制表符 (当(查看“^\\s-+”)时) (不匹配(匹配开始0)(匹配结束0))) (当(查看“^”)时) (替换匹配项“”);) 如果变量制表符宽度恰好为4(这就是您要“撤消”的内容),则可以将(查看“^”)替换为更

如何让Emacs移动到ShiftTab以将所选文本向左移动4个空格?

这将从当前行的前面删除4个空格(前提是存在空格)

(全局设置键(kbd“”)“取消缩进-删除-4-空格)
(通过删除4个空格()
“从行首删除4个空格”
(互动)
(省去远足
(保存匹配数据
(行首)
去掉行首的制表符
(当(查看“^\\s-+”)时)
(不匹配(匹配开始0)(匹配结束0)))
(当(查看“^”)时)
(替换匹配项“”);)
如果变量
制表符宽度
恰好为4(这就是您要“撤消”的内容),则可以将
(查看“^”)
替换为更一般的变量,如
(concat“^”(使字符串制表符宽度?\)


此外,代码将使用将行前面的制表符转换为空格。

为此,我使用绑定到
C-x制表符的命令
indent righly
。给它一个参数-4,将所选区域向左移动四个空格:
C-u-4 C-x TAB


我制作了一些函数,根据您是使用tab还是shift+tab,将一个区域左右移动四个空格:

(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)
(定义缩进区域自定义(numSpaces)
(项目
;默认为当前行的开始和结束
(setq区域开始(线路起始位置))
(setq区域(线路末端位置))
;如果有选择,请使用该选项,而不是当前行
(何时(使用-region-p)
(setq区域开始(区域开始))
(setq区域ND(区域结束))
)
(保存偏移;之后恢复位置
(转到char regionStart);转到区域的开头
(setq start(行起始位置));保存行的起始位置
(转到char regionEnd);转到区域的末尾
(setq end(行结束位置));保存行结束
(严格缩进起始-结束numSpaces);在起始和结束之间缩进
(setq停用标记nil);恢复选定区域
)
)
)
(联布权力机构地区(北)
(交互式“p”)
(缩进区域自定义-4)
)
(定义选项卡区域(N)
(交互式“p”)
(如果(活动小缓冲区窗口)
(微型缓冲区完成);在微型缓冲区窗口->执行完成中按tab键
其他的
(如果(字符串=(缓冲区名称)“*shell*”)
(comint dynamic complete);在shell中,使用制表符完成
其他的
(如果(use-region-p);按下tab键的是任何其他缓冲区->使用空格插入执行)
(缩进区域自定义4);已选择区域,请调用缩进区域
(插入“”);否则按预期插入四个空格
)))
)
(全局设置键(kbd“”)untab区域)
(全局设置键(kbd“”)选项卡区域)

编辑:添加了Maven的代码,以检查是否在minibuffer中,以及特定缓冲区(例如shell)中的制表符是否完成。

我改进了Stanley Bak的答案。 对我来说,这个全局密钥绑定破坏了微缓冲区的完成

所以我也包括了一个minibuffer的案例

编辑:重命名
缩进区域
->
缩进区域自定义

缩进区域
与现有命令冲突,并给出保存时缩进(保存前的挂钩)和其他一些组合键的错误

(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)
(定义缩进区域自定义(numSpaces)
(项目
;默认为当前行的开始和结束
(setq区域开始(线路起始位置))
(setq区域(线路末端位置))
;如果有选择,请使用该选项而不是当前行
(何时(使用-region-p)
(setq区域开始(区域开始))
(setq区域ND(区域结束))
)
(保存偏移;之后恢复位置
(转到char regionStart);转到区域的开头
(setq start(行起始位置));保存行的起始位置
(转到char regionEnd);转到区域的末尾
(setq end(行结束位置));保存行结束
(严格缩进起始-结束numSpaces);在起始和结束之间缩进
(setq停用标记nil);恢复选定区域
)
)
)
(联布权力机构地区(北)
(交互式“p”)
(缩进区域自定义-4)
)
(定义选项卡区域(N)
(交互式“p”)
(如果(活动小缓冲区窗口)
(微型缓冲区完成);在微型缓冲区窗口->执行完成中按tab键
(如果(use-region-p);按下tab键的是任何其他缓冲区->使用空格插入执行)
(缩进区域自定义4);已选择区域,请调用缩进区域自定义
(插入“”);否则按预期插入四个空格
)
)
)
(全局设置键(kbd“”)untab区域)
(全局设置键(kbd“”)选项卡区域)

不错!但是我会使用WHEN而不是IF,因为没有else-子句。对我来说,它不适用于选定的区域。只有第一行:-/@Mauricio A.Cinelli——绑定到
(kbd“”)
而不是
(kbd“”)
对我有效。根据相同的手动输入,您可能更喜欢
严格缩进代码,因为它会留下注释和多行字符串,公认的答案只对1行有效,而不是对regionsPerfect。适用于区域和线。复制其他常用编辑器的功能。
(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)