Lazarus

Using the Lazarus IDE => Editor => Topic started by: coliv_aja on February 18, 2017, 03:17:36 am

Title: [solved] IDE Shortcuts to move and duplicate lines?
Post by: coliv_aja on February 18, 2017, 03:17:36 am
Are there any shortcuts to move lines like Notepad++?
In Notepad++ we can move lines up or down with Ctrl+Up or Ctrl+Down.
Also, we can duplicate text/lines with Ctrl+D. This 2 feature that keeps me back to Notepad++ if I need to quick edit my pascal files.

Edit:
Solved with editor macros and assign keys to them.
Here are my editor macros to mimic the Notepad++ behavior.

duplicate line/seltext
Code: Pascal  [Select][+][-]
  1. var
  2.   s: String;
  3.   b1, b2: TPoint;
  4. begin
  5.   with Caller do
  6.     if SelAvail then
  7.     begin
  8.       b1 := BlockBegin;
  9.       b2 := BlockEnd;
  10.       s := TextBetweenPoints[b1, b2];
  11.       TextBetweenPoints[b1, b2] := s + s;
  12.       BlockBegin := b1;
  13.       BlockEnd := b2;
  14.     end
  15.     else
  16.     begin
  17.       s := Lines[CaretY - 1];
  18.       b1.x := 1;
  19.       b1.y := CaretY;
  20.       TextBetweenPoints[b1, b1] := s + #10;
  21.     end;
  22. end.                
  23.  

move line/seltext up
Code: Pascal  [Select][+][-]
  1. var
  2.   b1, b2, b3, b4: TPoint;
  3. begin
  4.   with Caller do
  5.   begin
  6.     b1 := BlockBegin;
  7.     if b1.y = 1 then Exit;
  8.     b1.x := 1;
  9.     b2 := BlockEnd;
  10.     b2.x := Length(Lines[b2.y-1]) + 1;
  11.     b3.x := b1.x;
  12.     b3.y := b1.y - 1;
  13.     b4.y := b3.y;
  14.     b4.x := Length(Lines[b4.y-1]) + 1;
  15.     TextBetweenPoints[b3, b2] := TextBetweenPoints[b1, b2] + TextBetweenPoints[b4, b1] + TextBetweenPoints[b3, b4];
  16.     CaretY := CaretY-1;
  17.     Dec(b1.y);
  18.     Dec(b2.y);
  19.     BlockBegin:= b1;
  20.     BlockEnd:= b2;
  21.   end;
  22. end.    
  23.  

move line/seltext down
Code: Pascal  [Select][+][-]
  1. var
  2.   b1, b2, b3, b4: TPoint;
  3. begin
  4.   with Caller do
  5.   begin
  6.     b1 := BlockBegin;
  7.     b2 := BlockEnd;
  8.     BlockEnd := Point(b2.x, b2.y + 1);
  9.     b3 := BlockEnd;
  10.     if b3.y = b2.y then
  11.     begin
  12.       BlockBegin := b1;
  13.       BlockEnd := b2;
  14.       exit;
  15.     end;
  16.     b1.x := 1;
  17.     b2.x := Length(Lines[b2.y - 1]) + 1;
  18.     b3.x := 1;
  19.     b3.y := b2.y + 1;
  20.     b4.y := b3.y;
  21.     b4.x := Length(Lines[b4.y - 1]) + 1;
  22.     TextBetweenPoints[b1, b4] := TextBetweenPoints[b3, b4] + TextBetweenPoints[b2, b3] + TextBetweenPoints[b1, b2];
  23.     CaretY := CaretY + 1;
  24.     Inc(b1.y);
  25.     Inc(b2.y);
  26.     BlockBegin:= b1;
  27.     BlockEnd:= b2;
  28.   end;
  29. end.
  30.  
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: Martin_fr on February 18, 2017, 03:23:18 am
Not by default.
But it is easy to create some macros, and assign them keys.
http://wiki.lazarus.freepascal.org/IDE_Window:_Editor_Macros

without pas script, use copy paste....

Using pas script, you can do it without clipboard.
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: Handoko on February 18, 2017, 03:30:41 am
@coliv_aja

If you use shortcuts often, you may interested to custom the shortcut keys:
Lazarus main menu > Tools > Options > Editor > Keymappings
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: Thaddy on February 18, 2017, 08:28:16 am
With the mouse it is easy to move lines.
Select the line(s) you want to move and ctrl+drag the line(s) to where you want them. This copies the line. w/o ctrl it moves the line.
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: coliv_aja on February 25, 2017, 05:32:09 pm
Not by default.
But it is easy to create some macros, and assign them keys.
http://wiki.lazarus.freepascal.org/IDE_Window:_Editor_Macros

without pas script, use copy paste....

Using pas script, you can do it without clipboard.
Thanks, yes I ended up with Editor Macros and assign the keys to them.

With the mouse it is easy to move lines.
Select the line(s) you want to move and ctrl+drag the line(s) to where you want them. This copies the line. w/o ctrl it moves the line.
For me, selecting a lines with mouse is slower than using keyboard. Selecting using keyboard then copy paste is much faster.
Problem solved with Editor Macros.
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: eny on February 25, 2017, 08:57:26 pm
Shift-CuDn
Ctrl-C (or Ctrl-X)
Ctrl-V
Ctrl-V

Only works on Window$ though.
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: jacmoe on February 25, 2017, 09:04:03 pm
It works on Linux too - KDE at least.
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: coliv_aja on February 26, 2017, 05:15:33 am
Shift-CuDn
Ctrl-C (or Ctrl-X)
Ctrl-V
Ctrl-V

Only works on Window$ though.
I previously using various keyboard like yours.
It's solved now with editor macros.
Thanks for the reply.
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: eny on February 28, 2017, 12:10:30 am
It works on Linux too - KDE at least.
Never worked and still does not work with the numpad keys.
IOW it's not possible to mimic the Window$ keypad behavior.
(Tested on Linux Mint 18.1 KDE)
Title: Re: IDE Shortcuts to move and duplicate lines?
Post by: dogriz on February 28, 2017, 08:08:47 am
I don't know about KDE, but in Xfce, you can remap keyboard shortcuts as you like.
Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: Trenatos on June 15, 2018, 07:50:17 pm
Thanks @coliv_aja, these macros help!
Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: Martin_fr on November 04, 2019, 12:47:26 pm
Lazarus trunk 2.1 now has line-up/down/duplicate keys.
Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: coliv_aja on January 18, 2020, 04:03:06 pm
Lazarus trunk 2.1 now has line-up/down/duplicate keys.

Thanks Martin.
Can you please add duplicate selection also? In npp, ctrl+d will duplicate selection or line if nothing selected.
But, since you already added the duplicate line(s), duplicate selection with different key would be nice too.
Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: Martin_fr on January 18, 2020, 04:48:19 pm
Can you please add duplicate selection also? In npp, ctrl+d will duplicate selection or line if nothing selected.

I'll have a look. Currently in the middle of another project....
Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: argb32 on January 18, 2020, 10:49:46 pm
It's better to move whole statements when possible. Like in IntelliJ.
There is little sense to move a part of statement or declaration.
Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: Martin_fr on January 19, 2020, 01:08:29 am
It's better to move whole statements when possible. Like in IntelliJ.
There is little sense to move a part of statement or declaration.
I can see that people may want to move part of an expression/calculation. Or change order of arguments passed to a function.

Anyway, if there is move-line an move-selection, then it would be a matter of selecting to statement boundaries (which is not on my current list, sorry). Most people will not have more than one statement per line. So moving lines will do this in that case.

Title: Re: [solved] IDE Shortcuts to move and duplicate lines?
Post by: Martin_fr on January 21, 2020, 08:29:45 pm
Added DuplicateSelection,
Also added MoveSelection... left/right/up/down

They do nothing, if there is no selection.



If you want the "duplicate line" to act on selection (if a selection is available), then you can create an editor macro "if Caller.SelAvail then ecDuplicateSelection else ecDuplicateLine;"
https://wiki.lazarus.freepascal.org/Editor_Macros_PascalScript
TinyPortal © 2005-2018