21

Is there some way I can mark text in emacs and shift it left (removing starting spaces) by space/Tab granularity?

Same way I would do on some other editor with Shift+Tab.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
sramij
  • 4,775
  • 5
  • 33
  • 55
  • I am not sure I get what you mean. Could you please elaborate your explanation? – N.N. Mar 14 '12 at 20:43
  • Many emacs modes have automatic indentation - when you hit [tab] anywhere on the line it'll indent the line to where it should be. I rarely have to worry about manually indenting anything - I just let the editor do the work for me. – Rob Hruska Mar 14 '12 at 20:44
  • I would like to be able do the opposite of what "Tab" does; i need that usually when i copy-past code positioned differently from another file. – sramij Mar 14 '12 at 21:06
  • What is the opposite of "indent this line/region to where it should be" ? – phils Mar 14 '12 at 23:53
  • Possible duplicate of [Shift a region or line in emacs](http://stackoverflow.com/questions/3156450/shift-a-region-or-line-in-emacs) – cic Aug 08 '16 at 08:07

4 Answers4

23
  1. Select your region;
  2. Type C-u followed by the number of spaces you want to indent (negative number if you want to decrease indentation);
  3. Use C-x TAB (by default bound to indent-rigidly) to apply the indentation to the region.

This is much more cumbersome than S-TAB, but it is IMHO some kind of last resort in case Emacs formatting doesn't solve your problem.

EDIT: much better solution: Shift a region or line in emacs (accepted answer). This is what I'm currently using in Emacs for changing indentation. WARNING: involves some Emacs Lisp.

Community
  • 1
  • 1
Miron Brezuleanu
  • 2,989
  • 2
  • 22
  • 33
  • Thanks. What if I want to increase or decrease number of tabs (instead of spaces) in indentation of a couple of lines? – Tim Apr 29 '16 at 15:29
  • Just multiply the value after C-u with tab width. – Miron Brezuleanu May 02 '16 at 05:56
  • @Tim a better way is described here - http://stackoverflow.com/questions/3156450/shift-a-region-or-line-in-emacs (accepted answer) – Miron Brezuleanu May 02 '16 at 06:00
  • Thanks. I mean an actual tab character, not a certain number and sequence of space characters. Do you mean the same as I? – Tim May 02 '16 at 06:09
  • @Tim - no, I don't use tab characters at all. I guess if you configure Emacs to use them (mine has had an 'only spaces' setting for years) it will compress spaces into tabs. – Miron Brezuleanu May 02 '16 at 19:43
4

This might be simpler and more visually intuitive: first make sure cua-mode is enabled (M-x cua-mode toggles it). Then go to the start of the line and press C-return. A red rectangle appears. Now move your cursor down and right to grow the rectangle as needed. Then press C-d to delete it. That's it.

I come across this problem often when the major-mode doesn't dictate any automatic indentation (or when it messes up).

There is a lot more you can do with cua-mode's rectangles, see http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html

joao
  • 3,517
  • 1
  • 31
  • 43
3

Generally emacs places things where the current style dictates when you hit <TAB>, so naturally it's a little different here. The closest thing that comes to mind is M-\ which collapses horizontal whitespace around point. If you want to remove a "rectangle" of space before the lines, then delete-rectangle might be more appropriate, which you can do by setting mark and moving point to select the rectangle and then using C-x r d.

wprl
  • 24,489
  • 11
  • 55
  • 70
FatalError
  • 52,695
  • 14
  • 99
  • 116
1

It sounds like the problem you're trying to solve is incorrect indentation of code when you're cutting/pasting. You can solve that by automatically re-indenting the text with something like the following.

Note: Using a prefix argument forces no re-indentation (C-u C-y), plus there's the size threshold variable.

;; automatically indenting yanked text if in programming-modes
(defvar yank-indent-modes '(emacs-lisp-mode
                            c-mode c++-mode
                            tcl-mode sql-mode
                            perl-mode cperl-mode
                            java-mode jde-mode
                            lisp-interaction-mode
                            LaTeX-mode TeX-mode)
  "Modes in which to indent regions that are yanked (or yank-popped)")

(defvar yank-advised-indent-threshold 1000
  "Threshold (# chars) over which indentation does not automatically occur.")

(defun yank-advised-indent-function (beg end)
  "Do indentation, as long as the region isn't too large."
  (if (<= (- end beg) yank-advised-indent-threshold)
      (indent-region beg end nil)))

(defadvice yank (after yank-indent activate)
  "If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
  (if (and (not (ad-get-arg 0))
           (member major-mode yank-indent-modes))
      (let ((transient-mark-mode nil))
        (yank-advised-indent-function (region-beginning) (region-end)))))
N.N.
  • 8,336
  • 12
  • 54
  • 94
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • I'm sure this code works well, but [auto-indent-mode.el](https://github.com/mlf176f2/auto-indent-mode.el/) is actively maintained and has more functionality. Although the threshhold is a very good idea. – event_jr Mar 15 '12 at 01:04
  • @event_jr I don't understand the point about something being actively maintained. I wrote the above code over 3 years ago and haven't needed to "maintain" it, it does the single thing it is supposed to do pretty well... – Trey Jackson Mar 15 '12 at 15:04
  • It does this -- and lots of other things to make sure code is indented correctly automatically. More features mean more buts and more possible bad interactions with other modes. Hence it's important that the author is actively working on it. – event_jr Mar 15 '12 at 15:29