如何在Emacs中缩进n个空格?

28

我想将整个区域向右或向左缩进n个空格。在某些模式下,我可以使用C-c >和C-c <来实现这一点,但我希望能够在普通模式下也能做到。

如果我需要更改我的.emacs文件以使其更高效(例如通过像上面那样的快捷键),那也没问题。


6
紧急投票是怎么回事?Emacs的“How-do-I-do-foo”问题在SO上已经存在很长时间了,有些人需要把一切都改变,但这并不会改变什么。 - Jack Kelly
1
已经在 Meta 上反复讨论过(尽管没有得出结论)。其中最好的可能是“我们能否就 SO 上的 Emacs 问题做出裁决?”(http://meta.stackexchange.com/q/79659/2509),其中主要答案得出结论认为它们应该属于 Stack Overflow。 - dmckee --- ex-moderator kitten
2
是的,这有点令人沮丧,但我怀疑我们不会看到任何关于这个问题的政策。我看到的建议是,因为没有明确的单一网站可供所有人使用(似乎Emacs特定的SE网站的想法被拒绝了),它们通常应该留在原始用户放置的位置。 - phils
6个回答

32

我使用的方法是:先选择一个区域,然后按下C-u <number of spaces> C-x TAB

回答Eric的问题,我们可以定义一个函数并绑定到一个键盘快捷键上,例如C-x C-TAB。尝试将此添加到您的Emacs配置中。

(defun insert-tabs (n)
  "Inserts N number of tabs"
  (interactive "nNumber of tabs: ")
  (dotimes (i n)
    (indent-for-tab-command)))

(global-set-key [?\C-x \C-tab] 'insert-tabs)

这个很好用,但我记不住它。有什么提示吗?(除了收藏此页面。) - Eric Wilson
@EricWilson 你好Eric,我更新了答案并提供了另一个建议。 - armandino
不错的函数,但我真正想要的是对执行命令的语义提示。 C-u 本身是否有意义? - Eric Wilson
1
@EricWilson 我明白了。是的,当给定一个整数N时,C-uuniversal-argument)会重复执行后面的命令N次。这里是文档链接:http://www.gnu.org/software/emacs/manual/html_node/emacs/Arguments.html - armandino

14

Sandro的回答中关键的部分是对indent-rigidly函数的调用。

C-x TAB (translated from C-x <tab>) runs the command indent-rigidly,
which is an interactive compiled Lisp function in `indent.el'.

It is bound to C-x TAB.

(indent-rigidly start end arg)

Indent all lines starting in the region sideways by arg columns.
Called from a program, takes three arguments, start, end and arg.
You can remove all indentation from a region by giving a large negative arg.

所以,标记该区域,输入数字参数,然后按下Ctrl+XTAB


11
我认为以下代码片段可以帮助你:

我认为以下代码片段可以帮助你:

;; Shift the selected region right if distance is positive, left if
;; negative

(defun shift-region (distance)
  (let ((mark (mark)))
    (save-excursion
      (indent-rigidly (region-beginning) (region-end) distance)
      (push-mark mark t t)
      ;; Tell the command loop not to deactivate the mark
      ;; for transient mark mode
      (setq deactivate-mark nil))))

(defun shift-right ()
  (interactive)
  (shift-region 1))

(defun shift-left ()
  (interactive)
  (shift-region -1))

;; Bind (shift-right) and (shift-left) function to your favorite keys. I use
;; the following so that Ctrl-Shift-Right Arrow moves selected text one
;; column to the right, Ctrl-Shift-Left Arrow moves selected text one
;; column to the left:

(global-set-key [C-S-right] 'shift-right)
(global-set-key [C-S-left] 'shift-left)

您可以将 (shift-region 1)(shift-region 1) 替换为您想要的值。
正如您所看到的,我的函数包装了 indent-rigidly:

indent-rigidly 是一个交互式的编译 Lisp 函数,位于 `indent.el'。

它绑定在 C-x TAB 上。

(indent-rigidly START END ARG)

将区域中以侧面开始的所有行向右缩进 ARG 列。从程序调用时,需要三个参数:START、END 和 ARG。您可以通过给出一个大的负数 ARG 来删除区域中的所有缩进。


9
同样有用的是操作文本矩形而不是文本行的“矩形命令”。例如,标记一个矩形区域后,C-x r o 插入空格以填充矩形区域(有效地将代码向右移动),C-x r k 则删除矩形区域(有效地将代码向左移动)。请参考矩形命令

2

这基本上是一个仅包含链接的答案。你能否详细说明一下?(但是不要包括“编辑:”,“更新:”或类似的内容 - 答案应该看起来像是今天写的。) - Peter Mortensen

0

使用Cua-mode

M-x cua-mode激活,C-RET选择整个区域的第一列,然后输入空格。最后,按Esc三次以退出,完成。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接