Struggling to make Macros work in KATE

Hello,

I switched from Windows to Linux last September and I initially jumped to Ubuntu for a few months before moving to Nobara. My current issue is making Macros work properly in Kate. Here is some context.

Originally, on Windows I used Notedpad++ to manage large text files that’d be chaotic when imported from a .doc. For that I used a macro that essentially was tied to a keybind and executed a two-pass cleanup + spacing normalizer in Extended Search Mode through these

Find: \r\n\r\n
Replace: \r\n

Find: \r\n
Replace: \r\n\r\n\r\n\r\n


As Linux is fundamentally different, I did some digging and made some changes when trying to do the same in Kate. I switched to Regular Expression Mode and used the following two for cleanup and spacing:

Find: \n{2,}
Replace: \n

Fine: \n
Replace: \n\n\n\n

Till this point everything works perfectly. However, it’s when I try to record a macro to automate this, is where things hit a wall. I followed the following steps:

  1. Started recording
  2. Did Step 1 replace
  3. Did Step 2 replace
  4. Stopped recording
  5. Saved current macro by giving it a name

Unfortunately, try as I might, loading or playing the named macro doesn’t execute the function at all. The text remains unchanged and undisturbed.

I tried a different approach and gave the macro a keybind as well. However when I try the keyind, for some reason it started splashing the following in the text file

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n

From what I can theorize, that was the “Replace” space normalizer(\n\n\n\n) being pasted 4 times in quick succession.

I know macros in Kate work cause it worked just fine when I was in Ubuntu. I just do not remember if I did doing something different then or am doing something wrong on my present install. Could somebody guide me as to what is it that I am missing here?

Regards,
LonerPrime

Recording macros in Kate? I think you’re not using the right terminology for Kate as this sounds more like something from a distant past for me.
I use Tools > External Tools > Configure… > Add / Add Tool… to create a new one.
For instance, I have one that deletes all blank lines in the current file as seen in the screenshot, which effectively executes the command sed -i ‘/^\s*$/d’ “$1” from the specified shell script. Probably it could have been achieved simply by executing that command without using an external shell script, but my several attempts failed, and I chose to stick with this lazy method. :wink:
You might be able to do it I guess, using sed as the “Executable”, -i ‘/^\s*$/d’ as “Arguments” and something else as “Input”.

No, Macros are absolutely the correct term.

Example, create a document with:

apple
banana
cherry
date
egg
fig
grape

Now press Ctrl+Shift+K to record a macro (if it’s enabled in Configure :play_button: Plugins :play_button: Keyboard Macros… and ensuring you have no conflicting keyboard shortcuts set).

So you can then edit - press HOME to go start of line, press Ctrl_U to capitalise, press down arrow to go to the next line.

Ctrl_Shift_K ends the recording, so then Ctrl Alt_K replays it… and would finish the whole line.

However, I do think the regex solution here is tidier. I never actually tried using macros this way.

Document
apple


banana


cherry


date


elderberry


fig


grape


So start recording: CtrlShiftK
Find: CtrlR\n\n\n
Replace: CtrlR\n
End recording: CtrlShiftK

So playing this macro now I get this:

apple
banana
cherry
date
elderberry
Fig
Grape

To @LonerPrime Make sure you have MODE set to ‘Escape’ in the Replace dialog.

Oh, I see! So, Kate has also something called “Keyboard Macros”, which I’ve always overlooked/ignored.
However, I would prefer using “External Tools” for such tasks as deleting all blanks lines (and then probably creating a shortcut key combination for it).

To be honest, after spending time working out how to tidy up some horribly bloated (like Kitty) config files, I like the plan to use Regex on the file instead… but I found it really hard to learn regex, so macro is good for simple enough tasks.

I never used this before, so give me a clap :wink:
:clap:

The right tool for this is scripting. I think we already have a builtin script that removes all blank lines. F7 to open command line, type rmblank. OR right-clickEditingRemove blank lines. This action is also available from the main menu, ToolsScriptsEditing. All are the same thing.

See Scripting with JavaScript

I have Kate version 25.12.1, and it seems the number of built-in scripts have increased since I last looked at them - it’s time to get rid of some external tool / scripts that are no longer necessary.

Scripting has some additional powers (undocumented I think) that external tools dont have. For e.g., ability to execute a command in the Project terminal, show/hide toolviews, do a multifile search, execute an external tool etc.