I have some Bash aliases which I use to perform standard day-to-day tasks in my macOS terminal using the Bash shell. Sometimes I find myself wanting to run a one-off command very similar to an alias I already have configured. My current approach is to type alias my-alias-name, copy the portion of the result within the command section of the quotes, paste it back into the Terminal input, and then edit it.

This approach is distracting and a little time consuming since it my command auto-complete doesn't work after I've started typing the word alias (so I either have to autocomplete my alias first and then go back to the start of the line and type alias). Also it means I have to use my mouse and replace my clipboard, plus very specifically copy the correct portion of the output. to copy the command text.

Is there a way to automatically insert the alias command into the Terminal input without having to resort to copy/pasting a selection of a command output?

$ alias example-alias="ls -l"

<exa>tab,control+A<alias >

$ alias example-alias
alias example-alias='ls -l'

Highlight the text between the single quotes with mouse, +C, +V

For instance, is there a way to write an alias/script expand-alias such that the following will insert the alias command into the command input to be modified?

expand-alias example-alias

Per this tip, one can use control+option+E to perform alias, history, and shell expansion on the current line. Therefore one can type/autocomplete the alias command, type this key combination, and have the text inserted into the input to be modified.

A downside of this approach is that it requires remembering a three-key combination for something that is only used occasionally. Depending on how your brain works, it might be harder to remember than a well-named command would be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.