There are a few projects I have where I need to work from two separate directories simultaneously, and while there are a number of ways I could set up workspaces in various esoteric IDEs or terminal session managers, I am stodgy in my ways and enjoy using the built-in Terminal in macOS for most things. If you use iTerm on the Mac, the commands are similar, but the AppleScript events that I use may need to be adjusted.
But I'm getting ahead of myself. For these projects, I want to have a bash/zsh alias that does the following:
- When I type
xyz(alias) and hit 'return'
- Open the current tab to path
- Open a new tab next to this tab
- Change directories in then new tab to path
Simple enough, you say, but I found that a number of AppleScript incantations (e.g.
do script and the like) could not be made to work with bash aliases easily. In the end, I put the following in my
.zshrc file (see all of geerlingguy's dotfiles here—some private aliases excluded):
# Alias to get two tabs ready for programming session for project xyz.
alias xyz='cd ~/projects/xyz && osascript \
-e '"'"'tell application "Terminal" to activate'"'"' \
-e '"'"'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'"'"' \
-e '"'"'tell application "System Events" to keystroke "cd ~/something-else/xyz" & return & "clear" & return'"'"''
Assuming you're always in a Terminal window when using the alias, it should work fine. Note that using
System Events pops a permissions warning in macOS 10.14 and later, and you may need to grant Accessibility privileges to the Terminal application in the Privacy settings in System Preferences—making changes like this can have security implications, so do so at your own risk!