1

Is it possible to create a Bash alias that upon running, enters a string of my prior choosing, into Terminal?

Let's pretend this is in my ~/.bash_profile:

alias start_youtube="cd ~/Video/Youtube; **[PASTE: youtube-dl URL]**"

Imagine that: the alias would change directory to Video/Youtube and then [PASTE] the string: youtube-dl URL. It could either do Return ⏎ or preferably just leave the string youtube-dl URL in the Terminal prompt so that I could swap the placeholder URL for a real URL.

If I try actually running the alias below;

alias start_youtube="cd ~/Video/Youtube; youtube-dl URL"

it will run the command as entered by which youtube-dl will return an error like "Could not parse URL" – which could've been fine. But pressing the key will not show the last input of the alias (youtube-dl URL) but rather shows the invoking of the alias (start_youtube) [which I'm sure is usually preferable].

So the requested solution is to paste a placeholder string into the Command Prompt, or somehow access the history of the alias command via the Shell.

1

Here's how I would approach it. Use this related question to write the function that would reside in your bashrc or bash_profile. Then add a line in the function that would delete the most recent line in your bash history using this

  • I settled for something inbetween. Created a function that echoes the "placeholder string" which I can Copy, Edit and Enter, once the alias has cd to the desired dir. Didn't understand how to access bash history from the alias or why to delete lines from it, because keyboarding ↑ does not reveal the commands used in the alias afaik. – Winterflags Jul 30 '17 at 18:30
1

You could use osascript for this and tell the System Events to type keys for you. Generally, I strongly advise against the whole approach of blindly sending keystrokes to the shell, you can wreak havoc on your system with this!

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to keystroke "# cd /tmp; your text" '

For safety reasons I send the comment character # as the first character, but this line gives the result you wanted. The cursor waits after the your text and you may continue typing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .