unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Keybinding to transpose current line with next line
@ 2020-09-22 14:31 Christopher Dimech
  2020-09-22 14:53 ` Yuri Khan
  2020-09-22 19:39 ` Francis Belliveau
  0 siblings, 2 replies; 12+ messages in thread
From: Christopher Dimech @ 2020-09-22 14:31 UTC (permalink / raw)
  To: Help Gnu Emacs


I have written the following keybinding to swap lines. If I have
the cursor on one line, the command swaps the current line with
the previous line.   What can I do for the keybinding to swap
the current line with the next line, rather than with the previous one.

  (global-set-key (kbd "C-t l")   'transpose-lines)





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

* Re: Keybinding to transpose current line with next line
  2020-09-22 14:31 Keybinding to transpose current line with next line Christopher Dimech
@ 2020-09-22 14:53 ` Yuri Khan
  2020-09-22 20:05   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-22 19:39 ` Francis Belliveau
  1 sibling, 1 reply; 12+ messages in thread
From: Yuri Khan @ 2020-09-22 14:53 UTC (permalink / raw)
  To: Christopher Dimech; +Cc: Help Gnu Emacs

On Tue, 22 Sep 2020 at 21:33, Christopher Dimech <dimech@gmx.com> wrote:

> I have written the following keybinding to swap lines. If I have
> the cursor on one line, the command swaps the current line with
> the previous line.   What can I do for the keybinding to swap
> the current line with the next line, rather than with the previous one.
>
>   (global-set-key (kbd "C-t l")   'transpose-lines)

Firstly, ‘transpose-lines’ is already bound to C-x C-t.

Secondly, transposing lines is a useful low-level primitive, but IMO a
much more handy UI metaphor is dragging the line at point up or down
through the surrounding lines while preserving the point’s position in
the line. That is implemented in ‘org-drag-line-forward’ and
‘org-drag-line-backward’. Pretty much nothing in their implementations
depends on org-mode, so they can be bound and used in any mode.



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

* Re: Keybinding to transpose current line with next line
  2020-09-22 14:31 Keybinding to transpose current line with next line Christopher Dimech
  2020-09-22 14:53 ` Yuri Khan
@ 2020-09-22 19:39 ` Francis Belliveau
  2020-09-22 19:55   ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 12+ messages in thread
From: Francis Belliveau @ 2020-09-22 19:39 UTC (permalink / raw)
  To: Christopher Dimech; +Cc: Help Gnu Emacs

You could either bind a macro or create a simple elisp function that moves to the next line first.
It could also move to previous afterward if you feel that it appropriate.

C-n C-t or C-n C-t C-p

> On Sep 22, 2020, at 10:31, Christopher Dimech <dimech@gmx.com> wrote:
> 
> 
> I have written the following keybinding to swap lines. If I have
> the cursor on one line, the command swaps the current line with
> the previous line.   What can I do for the keybinding to swap
> the current line with the next line, rather than with the previous one.
> 
>  (global-set-key (kbd "C-t l")   'transpose-lines)
> 
> 
> 




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

* Re: Keybinding to transpose current line with next line
  2020-09-22 19:39 ` Francis Belliveau
@ 2020-09-22 19:55   ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-22 19:55 UTC (permalink / raw)
  To: help-gnu-emacs

Francis Belliveau wrote:

> You could either bind a macro or create a simple
> elisp function

You mean like this?

(defun transpose-next-line ()
  (interactive)
  (beginning-of-line)
  (kill-line)
  (yank)
  (kill-line)
  (forward-line -1)
  (yank) )
(defalias 'tnl #'transpose-next-line) ; [1]


[1] https://dataswamp.org/~incal/emacs-init/geh.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Keybinding to transpose current line with next line
  2020-09-22 14:53 ` Yuri Khan
@ 2020-09-22 20:05   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23  4:29     ` Yuri Khan
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-22 20:05 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

> Secondly, transposing lines is a useful low-level
> primitive, but IMO a much more handy UI metaphor is
> dragging the line at point up or down through the
> surrounding lines while preserving the point’s
> position in the line.

hm ...?

You mean like this?

(defun transpose-next-line ()
  (interactive)
  (let ((beg (point))
        (lin (line-number-at-pos) ))
    (beginning-of-line)
    (kill-line)
    (yank)
    (kill-line)
    (forward-line -1)
    (yank)
    (goto-char beg)
    (unless (= lin (line-number-at-pos))
      (goto-char (point-min))
      (forward-line (1- lin))
      (end-of-line) )))
(defalias 'tnl #'transpose-next-line) ; [1]


[1] https://dataswamp.org/~incal/emacs-init/geh.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Keybinding to transpose current line with next line
  2020-09-22 20:05   ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-23  4:29     ` Yuri Khan
  2020-09-23  5:02       ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Yuri Khan @ 2020-09-23  4:29 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Wed, 23 Sep 2020 at 03:06, Emanuel Berg via Users list for the GNU
Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> > Secondly, transposing lines is a useful low-level
> > primitive, but IMO a much more handy UI metaphor is
> > dragging the line at point up or down through the
> > surrounding lines while preserving the point’s
> > position in the line.
>
> You mean like this?
>
> (defun transpose-next-line ()
>   (interactive)
>   (let ((beg (point))
>         (lin (line-number-at-pos) ))
>     (beginning-of-line)
>     (kill-line)
>     (yank)
>     (kill-line)
>     (forward-line -1)
>     (yank)
>     (goto-char beg)
>     (unless (= lin (line-number-at-pos))
>       (goto-char (point-min))
>       (forward-line (1- lin))
>       (end-of-line) )))
> (defalias 'tnl #'transpose-next-line) ; [1]

This might be good for you but org-drag-line-* have a couple
advantages over this:

* They do not pollute the kill ring.
* As you drag the line, point stays on the same character where you started.

   Lorem
   ips|um
   dolor
   ↓ (org-drag-line-down)
   Lorem
   dolor
   ips|um



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

* Re: Keybinding to transpose current line with next line
  2020-09-23  4:29     ` Yuri Khan
@ 2020-09-23  5:02       ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23  8:09         ` Marcin Borkowski
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-23  5:02 UTC (permalink / raw)
  To: help-gnu-emacs

Yuri Khan wrote:

> This might be good for you

...

> but org-drag-line-* have a couple advantages over
> this:
>
> * They do not pollute the kill ring.

OK, so use `let', `thing-at-point' (with 'line) and
`insert' instead.

> * As you drag the line, point stays on the same
> character where you started.

If you want to edit the line, why start with moving
it? It is more likely you wish to edit the
other line.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Keybinding to transpose current line with next line
  2020-09-23  5:02       ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-23  8:09         ` Marcin Borkowski
  2020-09-23 22:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23 23:27           ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 12+ messages in thread
From: Marcin Borkowski @ 2020-09-23  8:09 UTC (permalink / raw)
  To: Emanuel Berg; +Cc: help-gnu-emacs


On 2020-09-23, at 07:02, Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:

> Yuri Khan wrote:
>
>> This might be good for you
>
> ...
>
>> but org-drag-line-* have a couple advantages over
>> this:
>>
>> * They do not pollute the kill ring.
>
> OK, so use `let', `thing-at-point' (with 'line) and
> `insert' instead.
>
>> * As you drag the line, point stays on the same
>> character where you started.
>
> If you want to edit the line, why start with moving
> it? It is more likely you wish to edit the
> other line.

Why?  Imagine working on a book, and in the file, every sentence has its
own line.  You may want to move some sentence and then reword it
(e.g., change "above" to "below" etc.).

And a similar case can be made for code, JSON, whatever.

Best,

--
Marcin Borkowski
http://mbork.pl



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

* Re: Keybinding to transpose current line with next line
  2020-09-23  8:09         ` Marcin Borkowski
@ 2020-09-23 22:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23 23:27           ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-23 22:48 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski wrote:

>> If you want to edit the line, why start with
>> moving it? It is more likely you wish to edit the
>> other line.
>
> Why? Imagine working on a book, and in the file,
> every sentence has its own line. You may want to
> move some sentence and then reword it (e.g., change
> "above" to "below" etc.).
>
> And a similar case can be made for code,
> JSON, whatever.

It is much more natural to first correct the line,
rather than to move an incorrect line around. A good
looking, functional object can be put on a table,
a shelf, even a chair. But if it looks bad and
doesn't work it doesn't matter where you put it.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Keybinding to transpose current line with next line
  2020-09-23  8:09         ` Marcin Borkowski
  2020-09-23 22:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-23 23:27           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23 23:32             ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23 23:41             ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 2 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-23 23:27 UTC (permalink / raw)
  To: help-gnu-emacs

OK, so how about this?

- does not use the kill ring

- point remains in the same (textual) line, at the
  same column

- move forward or backward (with lines, e.g., -1 to
  move backward one line)

- move forward or backward any number of lines (with
  lines by any number of lines)

- set up keys and aliases as fit...

(defun move-line (&optional lines)
  (interactive "*p")
  (let ((lin (line-number-at-pos))
        (col (current-column))
        (num-lines (or lines 1)) )
    (beginning-of-line)
    (let ((line (thing-at-point 'line)))
      (delete-region (point-at-bol) (point-at-eol))
      (delete-char 1)
      (goto-char (point-min))
      (forward-line (+ lin num-lines -1))
      (insert line)
      (forward-line -1)
      (beginning-of-line)
      (forward-char col) )))
(defalias 'ml #'move-line)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Keybinding to transpose current line with next line
  2020-09-23 23:27           ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-23 23:32             ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23 23:41             ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-23 23:32 UTC (permalink / raw)
  To: help-gnu-emacs

> (defun move-line (&optional lines)
>   (interactive "*p")
>   (let ((lin (line-number-at-pos))
>         (col (current-column))
>         (num-lines (or lines 1)) )
>     (beginning-of-line)
>     (let ((line (thing-at-point 'line)))
>       (delete-region (point-at-bol) (point-at-eol))
>       (delete-char 1)
>       (goto-char (point-min))
>       (forward-line (+ lin num-lines -1))
>       (insert line)
>       (forward-line -1)
>       (beginning-of-line)
>       (forward-char col) )))
> (defalias 'ml #'move-line)

But I still think just killing the line manually,
move point, and yank it, is preferable...

KISS (keep it simple software)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

* Re: Keybinding to transpose current line with next line
  2020-09-23 23:27           ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-09-23 23:32             ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-09-23 23:41             ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-09-23 23:41 UTC (permalink / raw)
  To: help-gnu-emacs

> set up [...] aliases as fit...

Sure!

(defun move-line (&optional lines)
  (interactive "*p")
  (let ((lin (line-number-at-pos))
        (col (current-column))
        (num-lines (or lines 1)) )
    (beginning-of-line)
    (let ((line (thing-at-point 'line)))
      (delete-region (point-at-bol) (point-at-eol))
      (delete-char 1)
      (goto-char (point-min))
      (forward-line (+ lin num-lines -1))
      (insert line)
      (forward-line -1)
      (beginning-of-line)
      (forward-char col) )))
(defalias 'ml #'move-line)
(defalias 'mlb (lambda () (interactive) (move-line -1)))

I.e.,:

  Forward  1: M-x    ml  RET
  Backward 1: M-x    mlb RET
  Forward  X: C-u  X ml  RET
  Backward X: C-u -X ml  RET

Also the usual:

  Forward  4: C-u     ml RET
  Forward 16: C-u C-u ml RET

etc

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




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

end of thread, other threads:[~2020-09-23 23:41 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-09-22 14:31 Keybinding to transpose current line with next line Christopher Dimech
2020-09-22 14:53 ` Yuri Khan
2020-09-22 20:05   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-23  4:29     ` Yuri Khan
2020-09-23  5:02       ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-23  8:09         ` Marcin Borkowski
2020-09-23 22:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-23 23:27           ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-23 23:32             ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-23 23:41             ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-09-22 19:39 ` Francis Belliveau
2020-09-22 19:55   ` Emanuel Berg via Users list for the GNU Emacs text editor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).