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

时间:2011-08-02 20:26:00

标签: emacs

我想向右或向左缩进整个区域n个空格。我可以在某些模式下使用C-c>和C-c<,但我想这样做。

如果我需要更改我的.emacs以便提高效率(使用上面的键盘快捷键),那就没问题。

6 个答案:

答案 0 :(得分:30)

对我有用的是:选择区域,然后选择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)

答案 1 :(得分:13)

桑德罗答案的关键部分是对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.

因此,标记区域,输入数字arg,按C-x TAB

答案 2 :(得分:9)

我认为以下代码可以帮助您:

;; 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是一个交互式编译的Lisp函数   `indent.el”。

     

它与C-x TAB绑定。

     

(缩进刚刚开始ARG)

     

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

答案 3 :(得分:8)

对于文本的矩形而不是文本行进行操作的rectangle commands也很有用。

例如,在标记矩形区域后,

C-x r o插入空白区域以填充矩形区域(有效地将代码移到右侧) C-x r k杀死矩形区域(有效地将代码移到左侧)

答案 4 :(得分:2)

你知道Rectangles,对吧?

答案 5 :(得分:0)

使用cua-mode

M-x cua-mode激活,C-RET选择整个区域的第一列,然后输入空格,最后,esc三次逃脱,完成〜