[SOLVED] Delete line in text file

I want to assign a shortcut to a macro that deletes a line, regardless of its content.
I don't want to process the whole file and remove lines based on certain criteria.
I want to be able to just move my cursor up and down and wherever my cursor is, I hit the shortcut and it deletes that line, making everything else go up.

Like this:
image

Is this possible?

Depends, I think, on what application is displaying the line.

I mean, if I can target any application that deals with text editing, that would be perfect.

I mainly use these now (by order of priority, high to low)

Obsidian
TextEdit
Pages
Notes

Obsidian is where I've been spending more time and where I see myself doing this more often, followed by TextEdit. The other two are just extras. If I can at least do it in Obsidian and eventually (not required) in TextEdit, I'm already a happy guy

If I read you correctly something like this could maybe do what you want?

A bit crudely done, maybe, but it does okay in most text edit programs I've now tried here.

Delete Text Line With Cursor.kmmacros (2.4 KB)


EDIT: Changed the image to a more sensible crop

1 Like

This could be an option, but it doesn't work if there's text in the line

For example if you have this

Text1
Aaaaa
Text2

And you use that macro you end up with this

Text1

Text2

But I wanted to delete the whole line and get this
Text1
Text2

One way to achieve this would be to use CMD X after the right arrow action. If there's no text it will delete that line and it ignores the Delete action. So after CMD X we could check if the clipboard has any content (text, numbers, etc) and if so, run the Delete action, if not, ignore the Delete

This looks like a work around, but I was wondering if there was maybe an AppleScript or Shell script that could do this?

Shorter version:


This is what I came up with and it's working:

Delete Line in Text File Macro (v11.0.1)

Delete Line in Text File.kmmacros (29 KB)

When the line is empty and we use CMD + X, the clipboard doesn't become empty, that's why I added the first action. So if we hit CMD + X and the clipboard remains EmptyLine, that means that the current line was already empty, because the line was automatically deleted when we hit CMD + X so it ignores the Delete keystroke.

2 Likes

Alternatively, you can do this via a triple click:

This would not work for 3 reasons:

1 - Your coordinates point to the top left corner of the window, not the line
2 - Even if we could use the triple click, the mouse would have to be exactly where the line is, which is not ideal if you are trying to delete things in series, like "delete, move on to the next line, delete, move on", etc... I want to be able to delete a line, use the arrows to move to the next line, shortcut to delete, arrow, etc
3 - Triple click doesn't always work (in Obsidian, for example, it just selects the text, not the whole line). So I would always need an IF THEN to check if there was an empty line or not.

My macro works as expected. No "tricks"

If you wanted, you could replace the actions for "shift-cmd-rightArrow" and "cmd-X" with just one action for "ctrl-K".

This would delete to the end of line without the visual distraction of highlighting the text.

I tried with Control K, but the end result is not the same. It just deletes the text, not the line.
It seems that different apps actually behave differently. For example Obsidian, when hitting CMD X deletes the line if it's empty. Notes, does not.
So I need to change the macro to add some conditions depending on the front app.

Yes, my apologies, ctrl-k doesn't touch the clipboard, where of course cmd-X does, and it's the clipboard changing that's the key to your macro.

1 Like

Yes, the CMD+X is what changes the clipboard state and allows the macro to do its thing.
I updated the macro above, because I had to make changes for Obsidian.

Unless I'm missing something, this should fulfill your requirements too:

Screenshot 2023-12-27 at 17.17.07-pty-fs8

[test] Delete Line.kmmacros (2.8 KB)

In Adobe apps (InDesign at least), you have to use fn-LeftArrow instead of ⌘-LeftArrow, or configure the app's shortcuts towards Mac standards.


Edit/PS: Just downloaded Obsidian; it works there too.

3 Likes

Thanks for sharing.
Yes, it works with all 4 apps I can think of that I use: TextEdit, Pages, Obsidian, Notes.
Even if we need an IF THEN for other apps, it's still a shorter version compared to mine.
I added a link to your reply if others want to use that instead.

⌘-LeftArrow and ⇧-DownArrow semantics are really quite common in Mac text apps (more so than the Emacs-style shortcuts like Ctrl-A, Ctrl-E, etc.). Go figure, they even work in alien programs like MS Word :wink:

In InDesign, you can easily "standardize" the text shortcuts, so you don't need a conditional for that either (unless someone really loves the Adobe shortcuts):

yeah that's what I mean. Even if they have to, it's a way shorter version than mine