Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Ten Useful Shell Shortcuts

Here are ten shell commands, functions, and aliases that I use frequently on the job to save time and perform actions more consistently. Hopefully they are helpful for you to use or to inspire you to create your own automations!

 

1. Prepend a Jira ticket number to a git commit message

function commit() {
 gitBranchString="$(command git symbolic-ref --short HEAD 2> /dev/null)" || return
 gitBranchArray=("${(@s/-/)gitBranchString}")
 ticketTaggedMsg="${gitBranchArray[1]}-${gitBranchArray[2]} $1"
 git commit -m $ticketTaggedMsg
}

This function will prepend anything in your branch name before the second dash and prepend it to a commit message. For example, if you are on the git branch username/TICKET-707-validate-policy-name-on-create-and-rename and run

commit "implement validation"

the function will execute

git commit -m "username/TICKET-707 implement validation"

This is useful if your organization requires some specific commit message formatting and can be easily edited to your needs.

2. Kill process by port number

function killport() { # accept 1 argument, number of port to kill
 lsof -P | grep $1 | awk "{print $2}" | xargs kill -9
}

This command combines lsof and kill commands to kill any process running on an input port. Useful for fixing EADDRINUSE errors, but be careful with this!

3. Git checkout last branch

alias g-="git checkout -"

This is a simple alias that checks out the last git branch that you were on and is useful in switching between main and feature branches.

4. Clear

alias c="clear"

I use this command to clear the terminal so much that it’s worth aliasing to a single-letter.

5. Git status

alias s="git status"

Similarly checking the status of a git repo is such a frequent action that it’s also worth making a two keystrokes (“s” + “enter”).

6. Create a new branch on Bitbucket remote

alias chrome="open -a 'Google Chrome'"
function newbranch () {
  read "ticket?Enter the ticket number:"
  read "title?Enter the ticket title:"
  title="${title// /+}"
  chrome "https://bitbucket.org/branch/create?issueKey=$USER/${ticket}&issueType=Story&issueSummary=${title}"
}

This function will ask you for a Jira ticket and title and then open Google Chrome to https://bitbucket.org/branch/create?issueKey=username/ticketnumber&issueType=Story&issueSummary=branchdescription , a page where you can quickly create a branch on remote repos you have access to. This function is useful to quickly create branches and opening Chrome from the command line to specific URL is useful for other applications as well.

7. Changing directories effectively

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias cdproject="/Users/$USER/workspace/project"
alias cdsubproject="/Users/$USER/workspace/project/packages/pages/subproject"

These are useful commands to alias as changing to parent directories and project directories are common actions as well. Think of these aliases as “warping”, “teleporting”, or “apparating” to where you need to be!

8. List files in a repo by most commits

git log --pretty=format: --name-only -- "*.tsx" | sort | uniq -c | sort -rg | head -10

This command lists the top ten .tsx files in a repo by number of commits. This is useful for looking at the files in a repo that are most edited.

9. Cleaning up before git commits

alias precommit="cdsubproject && bolt install && project && yarn package:fix-dependencies:changed && yarn prettier:run-diff"
alias hardprecommit="cdproject && yarn i18n && yarn graphql && precommit"

These are common actions in some repos I contribute to that are often required to be run before committing certain changes. I’ve grouped them into aliases called `precommit` and `hardprecommit` depending on whether I need to run longer operations like generating internationalized messaging data and GraphQL files here. You can customize these to the needs of your project and run them to avoid pre-commit hooks failures.

10. Human scripting

These last commands are more involved and are more driven by process quality than expediency. This is for automating frequent processes that are mixtures of human and computer action. For example, the function `buildandroid` below walks a user through the complex process of deploying mobile apps to specific business logic, including reminding the user to communicate within the organization. The task function will hold the script from continuing until the human user has confirmed they have completed the required action, or exited the script. Thus, the process is both automated and documented in the shell script. Training people on these processes is also easier as all they have to do is run the script and follow the instructions. Try customizing this to a common process in your organization and watch it be done faster and more consistently!

function pause () {
 echo ""
 read -s -k $'?press any key to continue or ctrl+c to exit...\n'
}

function task () { # accept 1 argument, string of what needs to be done
 echo ""
 echo "TASK: ${1}"
 pause
}

# example - walks thru the process of building and deploying an Android app across platforms
function buildandroid() {
 android # cd to android project folder
 vim ./app/build.gradle
 task "increment and commit versionCode in build.gradle"
 read "version?Enter version (ex. 2.0.0): "
 read "versionCode?Enter versionCode (ex. 435): "
 ./gradlew :app:bundleProductionRelease # build bundle
 cd /Users/$USER/$DIR/android/app/build/outputs/bundle/productionRelease # nav to bundle location
 open . # open in Mac finder
 task "copy .aab file to GPC by creating a new release in the version number test track"
 task "copy PR and Trello links into Release Notes section of GPC for QA visability"
 task "complete rollout process in GPC"
 task "merge the current release branch into the qa branch to trigger bitrise.io build"
 echo ""
 echo ":rocket::package::robot_face::warning: I’ve bundled and rolled out a new Android bundle ${versionCode} (${version}) to GPC, this should be available in 24-72 hrs to testers in ${version}_test_track :rocket::package::robot_face::warning:"
 echo ""
 task "slack #app the above message" # reminder to communicate to stakeholders
 android # cd to android folder
}

References

The Full List

# the full list, ready to add to your ~/.zshrc, ~/.bashrc, or other shell config

# Ten Useful Shell Shortcuts
# Alex Vincent-Hill

# 1. prepend a Jira ticket number to a git commit message
function commit() {
 gitBranchString="$(command git symbolic-ref --short HEAD 2> /dev/null)" || return
 gitBranchArray=("${(@s/-/)gitBranchString}")
 ticketTaggedMsg="${gitBranchArray[1]}-${gitBranchArray[2]} $1"
 git commit -m $ticketTaggedMsg
}

# 2. kill process by port number
function killport() { # accept 1 argument, number of port to kill
 lsof -P | grep $1 | awk "{print $2}" | xargs kill -9
}

# 3. git checkout last branch
alias g-="git checkout -"

# 4. clear
alias c="clear"

# 5. git status
alias s="git status"

# 6. create a new branch on Bitbucket remote
alias chrome="open -a 'Google Chrome'"
function newbranch () {
  read "ticket?Enter the ticket number:"
  read "title?Enter the ticket title:"
  title="${title// /+}"
  chrome "https://bitbucket.org/branch/create?issueKey=$USER/${ticket}&issueType=Story&issueSummary=${title}"
}

# 7. changing directories effectively
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
# TBD: CUSTOMIZE THESE!
alias cdproject="/Users/$USER/workspace/project"
alias cdsubproject="/Users/$USER/workspace/project/packages/pages/subproject"

# 8. list files in a repo by most commits
alias listbycommits="git log --pretty=format: --name-only -- "*.tsx" | sort | uniq -c | sort -rg | head -10"

# 9. cleaning up before git commits
alias precommit="cdsubproject && bolt install && project && yarn package:fix-dependencies:changed && yarn prettier:run-diff"
alias hardprecommit="cdproject && yarn i18n && yarn graphql && precommit"

# 10. human scripting
function pause () {
 echo ""
 read -s -k $'?press any key to continue or ctrl+c to exit...\n'
}

function task () { # accept 1 argument, string of what needs to be done
 echo ""
 echo "TASK: ${1}"
 pause
}

Runners-up

  1. cat <file> | pbcopy → copy file contents to clipboard on Mac

  2. find . -type f | wc -l → count files in a directory recursively

  3. grep -rnw . -e "pattern" → recursively search current directory for file lines that include a pattern match

  4. code --list-extensions | xargs -L 1 echo code --install-extension → list VSCode extensions and print a list of commands to install them

7 comments

Comment

Log in or Sign up to comment
G subramanyam
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 23, 2023

Thank you @Alex 'AVH' Vincent-Hill for this wonderful list of shortcuts.

Like Dinesh Babu likes this
Daria Kulikova_GitProtect_io_ January 24, 2023

Thank you @Alex 'AVH' Vincent-Hill for such cool educational content

Like Zachary Anello likes this
Allie
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
May 1, 2023

YAY for shortcuts. Thank you for these, Alex!

Dinesh Babu June 11, 2023

Nice shortcuts

Like Zachary Anello likes this
Will Curtain November 22, 2023

Beginners just need to have a file with recordings at hand, I recommend

Like Zachary Anello likes this
Mihir Ajmera March 14, 2024


@Alex 'AVH' Vincent-Hill 

I appreciate you for sharing useful shortcuts for shell commands.

Thank You!

Like Zachary Anello likes this
Zachary Anello
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 16, 2024

Thank you! For all know how. 

TAGS
AUG Leaders

Atlassian Community Events