How Replace a Text Character With a Tab for Indentation in a Text File

Hi everyone!

I'm excited to have finally purchased a license and explore Keyboard Maestro, I loved it during my trial!

I'm hoping someone here can provide some help as I'm unable to turn up any solutions on the web so far.

The issue

I'm trying to have a list in txt file which has 2 hierarchy levels, the first level start with "xx" and the second one "zz"
I want to automatise the replacement of the character with a one TAB for xx and two tabs for zz to recreate the hierarchy I had in a word document with indentation and save the list a .txt.

Example

xx Mammals
zz Lion
zz Elephant
zz Giraffe

xx Birds
zz Eagle
zz Ostrich
zz Pigeon

Desired outcome

# My system

  • macOS Ventura 13.1
  • Keyboard Maestro 10.2

# Things I've tried

*Automatize the action search and replace searching for xx and replacing with %Tab% or with ^t however it doesn't write a tab and but it jumps to the next element (when I replace with %Tab%) or it just replace with ^t. I was able to write in the replace bar the tab by using the unicode Hex input and holding + 0009 however I don't know to replicate this manual key sequence in KM. Can somebody help me or suggest an alternative way?

Thank you!

Apps tested with

  • TextEdit

Use ⌥⇥ to type a tab in TextEdit's "Find" box -- or, as a KM action:

image

1 Like

Keyboard Maestro has it's own Search and Replace action.

I'd use that and not try to monkey with an app's UI unless it's really necessary.

For plain text editing run don't walk over to Bare Bones Software and grab a copy of BBEdit.

I recommend BBEdit because…
  • Although BBEdit is a commercial product, the trial version reverts to a
    still very powerful and free (BBEdit-Lite) version after 30 days.
  • It is very Mac-like.
  • It has excellent support for RegEx.
  • It is very AppleScriptable.
  • It has a two versatile command line tools.
  • It has been in continuous development for over 25 years.

BBEdit is highly scriptable and is useful for many tasks.

1 Like

TextEdit is not a very good vehicle for automation. Despite having been around for such a long time it's woefully underpowered and feature poor.

Nevertheless it's on every Mac and can be useful for various things, once you understand its idiosyncrasies.

Here's a macro that does what you want using TextEdit.

  • You may have a more preferred font and font size.
    • Those can be changed in the second AppleScript action in the macro.

Find and Replace v1.00.kmmacros (7.8 KB)

Macro Image

Keyboard Maestro Export


You could also accomplish this task by:

1 Like

Thank you so much both for your suggestions, I was not expecting to receive an answer and a solution so quickly!

@Nige_S solution was the quick answer for including the tab character by ⌥⇥ and it fixed my issue, but thanks @ccstone for recommending BBEdit, I don't like at all TextEdit and this looks much better!

I also tried the macro you provided that reach the same result in a more elegant and quicker way, but I wouldn't be able to make it as I don't know AppleScript, thanks for building it!

1 Like

For future reference, this task can be achieved using the built-in capabilities of TextEdit.

image

See the upside-down caret—properly known as a caron—denoted in the image above with the red arrow (to the right of the Find magnifying glass icon)? Whether the cursor is in the Find or the Replace Field, using that menu will offer special characters for searching/replacing.

image

(That Command-Option-Control-P shortcut may be custom to my setup.)

1 Like

Stock back to Mojave at least.

I've got it on a 10.11 El Capitan system, too, and I think it predates that version of OS X (now that I know it's not a custom configuration).

1 Like

Hi @ccstone, would you know how to modify the great script you created to replace instead of the specific "xx" or "zz"

x with t/
x.1 with t/t/
x.1.1. with t/t/t/ ?

where x could be any number of a list and x.1 a sub level and x.x.1 sub-sub (example below). So that the script can recognise the hierarchy of a list regardless the specific written number but only counting N of levels?

  1. Mammals
    1.1) Lion
    1.2) Elephant
    1.3) Giraffe

  2. Birds
    2.1) Eagle
    2.1.1) American Eagle
    2.1.2.) Asian Eagle
    2.2) Ostrich
    2.3) Pigeon

Search the forum for "markdown table" and see what you can find.

This task is very doable, but it's already been done on the KM forum (I think) and elsewhere:

script to convert text to markdown table - Google Search

Holler if you can't find anything useful.

1 Like