help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Move line


From: rock69
Subject: Re: Move line
Date: Sat, 31 May 2008 09:53:35 -0700 (PDT)
User-agent: G2/1.0

On May 31, 5:57 pm, rock69 <rocco.ro...@gmail.com> wrote:
> I'm rather new to emacs, but I'm addicted already. I used to use
> Eclipse a lot before, and there is one thing I'm missing though I'm
> pretty sure that it must be available somewhere in Emacs. I was
> wondering if it's possible to move (shift) a line of text up or down
> like you would do in Eclipse pressing Alt-up (or down).

I did some research within this very same newsgroup, and here's what I
discovered (works great!):

(defun move-line (n)
   "Move the current line up or down by N lines."
   (interactive "p")
   (let ((col (current-column))
         start
         end)
     (beginning-of-line)
     (setq start (point))
     (end-of-line)
     (forward-char)
     (setq end (point))
     (let ((line-text (delete-and-extract-region start end)))
       (forward-line n)
       (insert line-text)
       ;; restore point to original column in moved line
       (forward-line -1)
       (forward-char col))))

(defun move-line-up (n)
   "Move the current line up by N lines."
   (interactive "p")
   (move-line (if (null n) -1 (- n))))

(defun move-line-down (n)
   "Move the current line down by N lines."
   (interactive "p")
   (move-line (if (null n) 1 n)))

(global-set-key (kbd "M-<up>") 'move-line-up)
(global-set-key (kbd "M-<down>") 'move-line-down)

Thanks all.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]