Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I indent n spaces in emacs?

Tags:

emacs

I want to indent and 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 change my .emacs in order to make this efficient (with a keyboard shortcut like that above) that's fine.

like image 623
wn- Avatar asked Aug 02 '11 20:08

wn-


People also ask

How do I indent multiple lines in Emacs?

Select multiply lines, then type C-u 8 C-x Tab , it will indent the region by 8 spaces.

How do I change the tab space in Emacs?

Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.


2 Answers

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

Update

@Eric We could define a function and bind to a keyboard short cut, e.g. C-x C-TAB. Try adding this to your emacs config.

(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) 
like image 127
armandino Avatar answered Oct 17 '22 13:10

armandino


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 region, enter numeric arg, press C-x TAB.

like image 32
Jack Kelly Avatar answered Oct 17 '22 11:10

Jack Kelly