开发者

How do I indent n spaces in Emacs?

I want to indent an entire region n spaces to the right or left. I can do this in some modes with C-c > and C-c <, but I'd like to do this in general.

If I need to cha开发者_高级运维nge my .emacs file in order to make this efficient (with a keyboard shortcut like that above), that's fine.


What works for me is: select a region and then C-u <number of spaces> C-x TAB

Re Eric, we could define a function and bind to a keyboard shortcut, e.g., C-x C-TAB. Try adding this to your Emacs configuration.

(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)


The key part of Sandro's answer is the call to 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.

So, mark the region, enter a numeric argument, and press Ctrl + X, TAB.


I think that the following piece of code can help you:

;; 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)

You can replace the (shift-region 1) and the (shift-region 1) by the value you want.

As you can see, my function wraps indent-rigidly:

indent-rigidly 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.


Also useful are the rectangle commands that operate on rectangles of text instead of lines of text.

For example, after marking a rectangular region,

C-x r o inserts blank space to fill the rectangular region (effectively shifting code to the right) C-x r k kills the rectangular region (effectively shifting code to the left)


You inform yourself about Rectangles.


Use Cua-mode.

M-x cua-mode to activate, C-RET to select the first column of the whole region, and type space. Finally, Esc three times to escape, and done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜