tags:

views:

150

answers:

4

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?

+5  A: 

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
+1. I do the same, except I have kill-whole-line set. One less key-stroke!
Bahbar
I really don't understand how a, "no, don't do that, I do this" answer get's +5 up.
slomojo
@Bahbar - C-S-Backspace is bound to kill-whole-line by default.
slomojo
slomojo: because sometimes the canonical way to do something is a better answer than the precise answer sought.
Paul McMillan
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.
slomojo
@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
@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.
slomojo
+4  A: 

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

Thomas Kappler
+4  A: 

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
@user377089 : How do you set mark in emacs? I use C-SPACE and C-g for setting a mark.
prosseek
@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 Vega
That's useful. I underutilize transpose lines, and didn't realize that it worked with the mark in that fashion.
Paul McMillan
If you're using `transient-mark-mode`, which is now the default, Ray Vega's response is correct. I disable `transient-mark-mode`, so I simply set the mark with `C-SPACE`.
R. P. Dillon
+4  A: 

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

See: http://stackoverflow.com/questions/2423834/move-line-region-up-and-down-in-emacs/2426347#2426347

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.

slomojo