6

I like the TextMate that can select a line to move it with a simple keystroke.

Command-shift L (select a line) 
Control-Command Arrow(up and down) (move the selected line)

How can I do the same thing with emacs?

prosseek
  • 182,215
  • 215
  • 566
  • 871

5 Answers5

7

SO user, Sanityinc developed move-text.el (an extract of from basic-edit-toolkit.el - by emakswiki regular, Andy Stewart (lazycat))

See: Move line/region up and down in emacs

It does this task very nicely...

M-Up or M-Down to move the current line (or marked region's whole lines.)

I've added it to http://www.emacswiki.org/emacs/MoveText

Edit the bindings at the bottom to suit your preference.

Community
  • 1
  • 1
ocodo
  • 29,401
  • 18
  • 105
  • 117
6

I just delete the line, then yank it back in the new location. C-a C-k C-k (move to new location) C-y

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • +1. I do the same, except I have kill-whole-line set. One less key-stroke! – Bahbar Oct 04 '10 at 16:20
  • 2
    I really don't understand how a, "no, don't do that, I do this" answer get's +5 up. – ocodo Oct 04 '10 at 22:26
  • 1
    @Bahbar - C-S-Backspace is bound to kill-whole-line by default. – ocodo Oct 04 '10 at 22:37
  • 1
    slomojo: because sometimes the canonical way to do something is a better answer than the precise answer sought. – Paul McMillan Oct 04 '10 at 23:04
  • 2
    Not in this case it isn't, Emacs is by definition, a platform for customized editing. Not to mention that C-S-Backspace - Move to target - C-Y ... would be "canonical", for this operation. – ocodo Oct 05 '10 at 00:37
  • @slomojo: my answer didn't start with "no, don't do that...". The point of my answer was to say there are simple built-in ways to get the same effect without having to learn any new keystrokes. Why would you find that to not be useful? – Bryan Oakley Oct 05 '10 at 02:21
  • @Bryan, I didn't vote you down and the content of your answer is fine, for what it's worth. However it doesn't actually provide the answer the OP was looking for, you just posted what you'd do instead. So I asked, other users in general, why it was so positively up-voted. I hope you see what I'm getting at. – ocodo Oct 05 '10 at 04:03
4

move-line does that, except for the highlighting, which should be reasonably easy to add.

Thomas Kappler
  • 3,795
  • 1
  • 22
  • 21
4

The standard command C-x C-t is bound to transpose-lines. This is useful for transporting lines via interchange in a few ways.

The most obvious is that exchanges the line the point is in with the previous line.

With an argument of 0 (i.e. M-0 C-x C-t), it swaps the line that has the mark in it with the line that has the point in it.

R. P. Dillon
  • 2,780
  • 1
  • 18
  • 20
  • @user377089 : How do you set mark in emacs? I use C-SPACE and C-g for setting a mark. – prosseek Oct 04 '10 at 17:38
  • 1
    @prosseek- `C-SPACE C-SPACE`. Basically, hit `C-SPACE` twice as if toggling. The first time the mark is set and activated on the current line. Then the second time the mark is deactivated but still set on the current line. – Ray Oct 04 '10 at 20:42
  • That's useful. I underutilize transpose lines, and didn't realize that it worked with the mark in that fashion. – Paul McMillan Oct 04 '10 at 23:06
0

bbatsov's prelude (a kind of Emacs starter kit) includes this functionality (see this section of code):

(defun prelude-move-line-up ()
  "Move the current line up."
  (interactive)
  (transpose-lines 1)
  (forward-line -2)
  (indent-according-to-mode))

and binds it to M-S-up and M-S-down.

Dillon Kearns
  • 3,675
  • 2
  • 19
  • 9