How to duplicate line in editor?

You have encountered a problem using Far Manager? Here you can get help.
Post Reply
User avatar
michaelang
Posts: 17
Joined: Sat 17 Jan, 2009 08:48

How to duplicate line in editor?

Post by michaelang »

Besides copy and paste, any quick keyboard keys to duplicate line in editor?
User avatar
chupakabra
Posts: 1023
Joined: Wed 27 Jul, 2005 19:36
Contact:

Re: How to duplicate line in editor?

Post by chupakabra »

michaelang
Record macro?
zgabi
Posts: 4
Joined: Tue 29 Sep, 2020 15:28

How to duplicate line in editor?

Post by zgabi »

I'm regularly using macros in FAR Manager, but copy and paste in a macro is not really reliable. Sometimes it is doing strange things... So every time when I can, I try to avoid it in a macro. This problem exists since I'm using it (almost 20 years)
I did not noticed similar problem with any other functions in a macro.
buniak_a_h
Posts: 2094
Joined: Sat 20 Apr, 2013 00:17
Location: Питер
Has thanked: 129 times
Been thanked: 207 times
Contact:

How to duplicate line in editor?

Post by buniak_a_h »

zgabi wrote: Tue 29 Sep, 2020 15:32 copy and paste in a macro is not really reliable
See Editor.GetStr, Editor.InsStr
citRiks
Posts: 895
Joined: Fri 25 Oct, 2019 18:18
Has thanked: 269 times
Been thanked: 46 times

How to duplicate line in editor?

Post by citRiks »

zgabi,
I dont know about problems with copy-paste.
in the end, you are able to do in FAR and its editor all basic operations, and even some advanced stuff.
And line duplication of course you are able to program in Lua,
or with recording sequence of keystrokes.
But, yes, the learning curve is not for faint-hearted.
To know "a bit" of FAR API programming will not help you to much,
to know "a lot" of FAR API programming is a hard way to go.

Hovewer, and i think it may be good for the starters,
there is an attempt to collect pre-programmed routine operations done in this script collection:
@SimSU macro-collection
viewtopic.php?t=7075

hint: use some online translate like in chrome browser
А кто не поверит в наше миролюбие, / Тот кровью умоется. / И будет такая борьба за мир, / Что камня на камне не останется.
zgabi
Posts: 4
Joined: Tue 29 Sep, 2020 15:28

How to duplicate line in editor?

Post by zgabi »

Here is the problem:

I'm using macro recording, never tried the API or any lua scripts.

Check the attached GIF video. (Sorry for the size and quality, 1MB limit is very small)

Steps:
- Create a file with numbers from 1 to 10000 in each line. All cumbers are concatenated 4 times in each line.
- Open the file, and put the cursor to the first character of the first line.
- Press Ctrl-. to start macro recording
- Press Shift-End to select the line
- Press Ctrl-C to copy it
- Press Home to go to the beginning of the current line
- Press Ctrl-V to insert
- Press Home to go to the beginning of the current line
- Press Down to go to the next line
(Do the same with the next line:)
- Press Shift-End to select the line
- Press Ctrl-C to copy it
- Press Home to go to the beginning of the current line
- Press Ctrl-V to insert
- Press Home to go to the beginning of the current line
- Press Down to go to the next line
- Press Ctrl-. to stop recording
- Press Ctrl-A to assign a hotkey. (In the video I had a previous macro for this key, but it is not relevant)
- Press and hold Ctrl-A

Sometimes the line content is not duplicated, at line 153, 357, 667

Of course this sample is just to reproduce the bug, I'm using it for more "complex" scenarios where regex replace is not easily possible. Usually in different files, so the macro is opening the file editor and closing it and selecting the next file.

You can see in the steps to reproduced that I did the same step in the macro twice.. this is because the problem is more easily to reproducible if the macro contains more copy pastes. (But it is reproducible with 1 copy-paste, too.. sometimes.)
Attachments
Far Macro Copy Paste bug.gif
Far Macro Copy Paste bug.gif (979.85 KiB) Viewed 300 times
zgabi
Posts: 4
Joined: Tue 29 Sep, 2020 15:28

How to duplicate line in editor?

Post by zgabi »

buniak_a_h wrote: Tue 29 Sep, 2020 15:50 See Editor.GetStr, Editor.InsStr
Ok, probably that works, but the answer was "Record macro?"... And that's where the copy-paste is not working reliable.

Don't get me wrong, I live this macro recording feature in Far manager... it is very useful.. only the copy-paste is wrong in a recorded macro.
buniak_a_h
Posts: 2094
Joined: Sat 20 Apr, 2013 00:17
Location: Питер
Has thanked: 129 times
Been thanked: 207 times
Contact:

How to duplicate line in editor?

Post by buniak_a_h »

zgabi,
Go %FarProfile%\Macros\Scripts or any subfolder and write any LUA-file (for example Editor_DupLine.lua), containing:

Code: Select all

Macro{
  id="A24AE883-0DDA-45B7-B8CA-F35135901927";
  area="Editor";
  key="CtrlShiftC";
  description="Duplicate current line in Editor";
  flags="";
  action=function(data)
   Editor.InsStr(Editor.GetStr())
   Editor.Pos(1, 1, Editor.Pos(0,1)+1) -- Down on new line
  end;
}
and reload macros (macro:load in command line)
Sorry for my english.
Last edited by buniak_a_h on Wed 30 Sep, 2020 11:42, edited 1 time in total.
buniak_a_h
Posts: 2094
Joined: Sat 20 Apr, 2013 00:17
Location: Питер
Has thanked: 129 times
Been thanked: 207 times
Contact:

How to duplicate line in editor?

Post by buniak_a_h »

zgabi, P/S.
I set this function on hotkey Ctrl+Shift+C. If you want, you can change line 4 in this macro.
Last edited by buniak_a_h on Wed 30 Sep, 2020 11:47, edited 2 times in total.
zgabi
Posts: 4
Joined: Tue 29 Sep, 2020 15:28

How to duplicate line in editor?

Post by zgabi »

Thanks for implementing this macro, but it was only a sample.

I don't want to duplicate a line. I don't need a single macro.

Usually I'm recording macros for different things.... searching different texts in a file... copying from that location where it is found... manipulating it somehow... so it is not a single function like always duplicate a line... I record every time a new macro for different repeating things... which usually works except when it contains a copy-paste (and copy-paste also works 99% of the time, but sometimes it does not do the copy which makes the file what I edit corrupt... usually C# source files, xml-s, Visual Studio project files, etc.)

Another real example what I tried to solve with macros:

For example there is the following file:
--------------------------
<Project Sdk="Microsoft. NET.Sdk.WindowsDesktop">

<PropertyGroup>
<Product>AquaCore</Product>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<UseWPF>true</UseWPF>
<OutputType>WinExe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPublishable>true</IsPublishable>
</PropertyGroup>
...
--------------------------

I want to duplicate the TargetFrameworks tag and rename it to TargetFramework:
--------------------------
<Project Sdk="Microsoft. NET.Sdk.WindowsDesktop">

<PropertyGroup>
<Product>AquaCore</Product>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<UseWPF>true</UseWPF>
<OutputType>WinExe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IsPublishable>true</IsPublishable>
</PropertyGroup>
...
--------------------------

I have several files where I have to change this. (~200)
So I search them and feed to a side in Far manager
Record a macro, make the change in 1 file. (Open file, change it, save, move to next file in the list)
Then executing the macro by holding the hotkey... in some file it will be wrong because of the copy paste. Without copy paste I never had problem.

This was a real example, what I had to do only once, but on several files. I do not want to write lua scripts for that, only recording a macro.

Next time I have a different problem to solve.

I know that it can be done with regex, but much easier to record a macro and use it wen there are a lot of files, but not very very much. For example for some 100 files macro is a good solution, I think... but of course for 1 million file I'd find another solution :)
User avatar
DrKnS
Posts: 5686
Joined: Thu 04 Aug, 2005 06:44
Location: Kyiv
Has thanked: 9 times
Been thanked: 313 times

How to duplicate line in editor?

Post by DrKnS »

zgabi, I can't reproduce your problem.

No need to film the process, we believe that you can see it. Could you please instead make sure that:
- you're using the latest available version
- there are no other macros, plugins or 3rd party software interfering with the clipboard.

If you still can reproduce it after these steps, please provide your file and the recorded macro just in case (you can find it in %farprofile%\Macros\internal\Editor_CtrlA.lua).
Post Reply

Return to “Support and Troubleshooting”