all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Move line
@ 2008-05-31 15:57 rock69
  2008-05-31 16:53 ` rock69
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: rock69 @ 2008-05-31 15:57 UTC (permalink / raw)
  To: help-gnu-emacs

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).


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 15:57 rock69
@ 2008-05-31 16:53 ` rock69
  2008-05-31 21:15   ` Eli Zaretskii
       [not found]   ` <mailman.12500.1212268508.18990.help-gnu-emacs@gnu.org>
  2008-05-31 21:51 ` Peter Dyballa
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 13+ messages in thread
From: rock69 @ 2008-05-31 16:53 UTC (permalink / raw)
  To: help-gnu-emacs

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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 16:53 ` rock69
@ 2008-05-31 21:15   ` Eli Zaretskii
  2008-05-31 21:22     ` Lennart Borgman (gmail)
       [not found]   ` <mailman.12500.1212268508.18990.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2008-05-31 21:15 UTC (permalink / raw)
  To: help-gnu-emacs

> From: rock69 <rocco.rossi@gmail.com>
> Date: Sat, 31 May 2008 09:53:35 -0700 (PDT)
> 
> (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.

I have this in my ~/.emacs, which I think does what you want with much
less fuss:

 (global-set-key "\M-z" (function (lambda () (interactive) (scroll-down 1))))
 (global-set-key "\C-z" (function (lambda () (interactive) (scroll-up 1))))

Of course, I'm used to different keybindings, but the point is that
the code is much simpler and shorter.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 21:15   ` Eli Zaretskii
@ 2008-05-31 21:22     ` Lennart Borgman (gmail)
  2008-06-01  3:14       ` Eli Zaretskii
  0 siblings, 1 reply; 13+ messages in thread
From: Lennart Borgman (gmail) @ 2008-05-31 21:22 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs

Eli Zaretskii wrote:
>> From: rock69 <rocco.rossi@gmail.com>
>> Date: Sat, 31 May 2008 09:53:35 -0700 (PDT)
>>
>> (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.
> 
> I have this in my ~/.emacs, which I think does what you want with much
> less fuss:
> 
>  (global-set-key "\M-z" (function (lambda () (interactive) (scroll-down 1))))
>  (global-set-key "\C-z" (function (lambda () (interactive) (scroll-up 1))))
> 
> Of course, I'm used to different keybindings, but the point is that
> the code is much simpler and shorter.

But it moves the point instead of the line ...




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 15:57 rock69
  2008-05-31 16:53 ` rock69
@ 2008-05-31 21:51 ` Peter Dyballa
  2008-06-01  5:39 ` William Xu
  2008-06-02 12:34 ` Ken Goldman
  3 siblings, 0 replies; 13+ messages in thread
From: Peter Dyballa @ 2008-05-31 21:51 UTC (permalink / raw)
  To: rock69; +Cc: help-gnu-emacs


Am 31.05.2008 um 17:57 schrieb rock69:

> I was wondering if it's possible to move (shift) a line of text up  
> or down


(defun scroll-down-in-place (n)
   (interactive "p")
   (previous-line n)
   (scroll-down n))

(defun scroll-up-in-place (n)
   (interactive "p")
   (next-line n)
   (scroll-up n))


--
Greetings

   Pete

When people run around and around in circles we say they are crazy.  
When planets do it we say they are orbiting.






^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
       [not found]   ` <mailman.12500.1212268508.18990.help-gnu-emacs@gnu.org>
@ 2008-05-31 23:03     ` Joost Kremers
  2008-06-01  5:48       ` Thien-Thi Nguyen
  0 siblings, 1 reply; 13+ messages in thread
From: Joost Kremers @ 2008-05-31 23:03 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii wrote:
>  (global-set-key "\M-z" (function (lambda () (interactive) (scroll-down 1))))
>  (global-set-key "\C-z" (function (lambda () (interactive) (scroll-up 1))))
>
> Of course, I'm used to different keybindings, but the point is that
> the code is much simpler and shorter.

it's much simpler and shorter, and does something completely different...


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 21:22     ` Lennart Borgman (gmail)
@ 2008-06-01  3:14       ` Eli Zaretskii
  0 siblings, 0 replies; 13+ messages in thread
From: Eli Zaretskii @ 2008-06-01  3:14 UTC (permalink / raw)
  To: help-gnu-emacs

> Date: Sat, 31 May 2008 23:22:57 +0200
> From: "Lennart Borgman (gmail)" <lennart.borgman@gmail.com>
> CC: help-gnu-emacs@gnu.org
> 
> >  (global-set-key "\M-z" (function (lambda () (interactive) (scroll-down 1))))
> >  (global-set-key "\C-z" (function (lambda () (interactive) (scroll-up 1))))
> > 
> > Of course, I'm used to different keybindings, but the point is that
> > the code is much simpler and shorter.
> 
> But it moves the point instead of the line ...

Please try again, and you will see that point stays put.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 15:57 rock69
  2008-05-31 16:53 ` rock69
  2008-05-31 21:51 ` Peter Dyballa
@ 2008-06-01  5:39 ` William Xu
  2008-06-01  5:57   ` Bastien
  2008-06-02 12:34 ` Ken Goldman
  3 siblings, 1 reply; 13+ messages in thread
From: William Xu @ 2008-06-01  5:39 UTC (permalink / raw)
  To: help-gnu-emacs

rock69 <rocco.rossi@gmail.com> writes:

> 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 don't see where it could be useful...  

-- 
William

http://williamxu.net9.org





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 23:03     ` Joost Kremers
@ 2008-06-01  5:48       ` Thien-Thi Nguyen
  0 siblings, 0 replies; 13+ messages in thread
From: Thien-Thi Nguyen @ 2008-06-01  5:48 UTC (permalink / raw)
  To: help-gnu-emacs

⎛⎞ Joost Kremers <joostkremers@yahoo.com>
⎝⎠ 31 May 2008 23:03:20 GMT

   it's much simpler and shorter, and does something completely different...

I think OP wants some fusion of transpose-lines plus "save-excursion",
something like:

(defun transpose-lines/stay-put ()
  (let ((col (current-column)))
    (forward-line 1)
    (transpose-lines 1)
    (forward-line -1)
    (forward-char col)))

thi




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-06-01  5:39 ` William Xu
@ 2008-06-01  5:57   ` Bastien
  0 siblings, 0 replies; 13+ messages in thread
From: Bastien @ 2008-06-01  5:57 UTC (permalink / raw)
  To: help-gnu-emacs

William Xu <william.xwl@gmail.com> writes:

> rock69 <rocco.rossi@gmail.com> writes:
>
>> 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 don't see where it could be useful...  

org-mode implants something near what the OP has in mind.  Not only for
moving list items, but also for any *line* in the buffer.

Please edit a buffer in org-mode and check what M-up/down does there.

-- 
Bastien




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
@ 2008-06-01  7:39 martin rudalics
  2008-06-01  7:54 ` Lennart Borgman (gmail)
  0 siblings, 1 reply; 13+ messages in thread
From: martin rudalics @ 2008-06-01  7:39 UTC (permalink / raw)
  To: eliz; +Cc: help-gnu-emacs

 > > But it moves the point instead of the line ...
 >
 > Please try again, and you will see that point stays put.

Maybe you have to set (or customize) `scroll-preserve-screen-position'
first.





^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-06-01  7:39 Move line martin rudalics
@ 2008-06-01  7:54 ` Lennart Borgman (gmail)
  0 siblings, 0 replies; 13+ messages in thread
From: Lennart Borgman (gmail) @ 2008-06-01  7:54 UTC (permalink / raw)
  Cc: help-gnu-emacs

>  > > But it moves the point instead of the line ...
>  >
>  > Please try again, and you will see that point stays put.
> 
> Maybe you have to set (or customize) `scroll-preserve-screen-position'
> first.


And that will of course preserve screen position ...

I am sorry that I did not write more clearly, but it is interesting to 
see how this little simple example could be so misunderstood. The 
initial impression is clearly a bit difficult to erase in once mind.




^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Move line
  2008-05-31 15:57 rock69
                   ` (2 preceding siblings ...)
  2008-06-01  5:39 ` William Xu
@ 2008-06-02 12:34 ` Ken Goldman
  3 siblings, 0 replies; 13+ messages in thread
From: Ken Goldman @ 2008-06-02 12:34 UTC (permalink / raw)
  To: help-gnu-emacs

The elisp experts have given you a function to do what you want.

As a novice, what I do is define a keyboard macro to do what I want,
save the macro, and assign the saved macro to a key.

rock69 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).


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2008-06-02 12:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-01  7:39 Move line martin rudalics
2008-06-01  7:54 ` Lennart Borgman (gmail)
  -- strict thread matches above, loose matches on Subject: below --
2008-05-31 15:57 rock69
2008-05-31 16:53 ` rock69
2008-05-31 21:15   ` Eli Zaretskii
2008-05-31 21:22     ` Lennart Borgman (gmail)
2008-06-01  3:14       ` Eli Zaretskii
     [not found]   ` <mailman.12500.1212268508.18990.help-gnu-emacs@gnu.org>
2008-05-31 23:03     ` Joost Kremers
2008-06-01  5:48       ` Thien-Thi Nguyen
2008-05-31 21:51 ` Peter Dyballa
2008-06-01  5:39 ` William Xu
2008-06-01  5:57   ` Bastien
2008-06-02 12:34 ` Ken Goldman

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.