Jump to content

Workflow to add line numbers to selected text

Recommended Posts

I want to create a workflow that will take selected text in a document, add line numbers to it, and replace the selected text in the document.



I created a workflow to be initiated by a hotkey when I have text selected but it doesn't work. I think that's mostly because I'm not much of a programmer and don't know how to work this thing very well.


Download the workflow I created here.


Any suggestions for how to make this work would be appreciated...

Edited by jochi
Link to comment

$1 is to be used on the shell; inside an Alfred workflow, you should be working with query (you have that instruction on the Run Script window).

You should already have a nl utility installed, which is exactly for that. This code


echo "{query}" | nl

should do what you want.

Run man nl inside the terminal to see more options (you might want to take a look specifically at the -n formatting alternatives).

Link to comment

One thing that seems like it might be a but is the workflow is leaving escaped spaces "\" in the output.

You’re telling it to


(again, look around the windows Alfred shows, they try to give you instructions for each function).

The awk way seems to work fine, but for reference, here’s something you could do using nl, to get the formatting you want

echo "{query}" | nl | perl -pe 's/\t/. /'

That’ll take the first tab character (the one that separates the number and the text) of each line, and output a period and space instead.

On another note, when updating the workflow, you should update the link on the first post (stops people from having to read the whole thread to get the latest version).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...