Translate

Tuesday, 3 March 2020

75 Zsh Commands, Plugins, Aliases and Tools

75 Zsh Commands, Plugins, Aliases and Tools

I spend a lot of my day in the terminal, and my shell of choice is Zsh — a highly customizable Unix shell that packs some very powerful features. As I’m a lazy developerTM, I’m always looking for ways to type less and to automate all the things. Luckily this is something that Zsh lends itself well to.

In this post, I’m going to share with you 75 commands, plugins, aliases and tools that will hopefully save you some keystrokes and make you more productive in your day-to-day work.

If you don't have Zsh installed on your machine, then check out this post, where I show you how to get up and running.

15 Things Zsh Can Do out of the Box

Zsh shares a lot of handy features with Bash. None of the following are unique to Zsh, but they're good to know nonetheless. I encourage you to start using the command line to perform operations such as those listed below. It might seem like more work than using a GUI at first, but once you get the hang of things, you'll never look back.

  • Entering cd from anywhere on the file system will bring you straight back to your home directory.
  • Entering !! will bring up the last command. This is handy if a command fails because it needs admin rights. In this case you can type sudo !!.
  • You can use && to chain multiple commands. For example, mkdir project && cd project && npm init -y.
  • Conditional execution is possible using ||. For example, git commit -m "whatever..." || echo "Commit failed".
  • Using a -p switch with the mkdir command will allow you to create parent directories as needed. Using brace expansion reduces repetition. For example, mkdir -p articles/jim/sitepoint/article{1,2,3}.
  • Set environment variables on a per-command basis like so: NODE_DEBUG=myapp node index.js. Or, on a per-session basis like so: export NODE_DEBUG=myapp. You can check it was set by typing echo $<variable-name>.
  • Pipe the output of one command into a second command. For example, cat /var/log/kern.log | less to make a long log readable, or history | grep ssh to search for any history entries containing "ssh".
  • You can open files in your editor from the terminal. For example, nano ~/.zshrc (nano), subl ~/.zshrc (Sublime Text), code ~/.zshrc (VS Code). If the file doesn't exist, it will be created when you press Save in the editor.
  • Navigation is an important skill to master. Don't just rely on your arrow keys. For example, Ctrl + a will take you to the beginning of a line.
  • Whereas Ctrl + e will take you to the end.
  • You can use Ctrl + w to delete one word (backw­ards).
  • Ctrl + u will remove everything from the cursor to the beginning of the line.
  • Ctrl + k will clear everything from the cursor to the end of the line. These last three can be undone with Ctrl + y.
  • You can copy text with Ctrl + Shift + c. This is much more elegant than right clicking and selecting Copy.
  • Conversely, you can paste copied text with Ctrl + shift + v.

Try to commit those key combos to memory. You'll be surprised at how often they come in handy.

15 Custom Aliases to Boost Your Productivity

Aliases are terminal shortcuts for regular commands. You can add them to your ~/.zshrc file, then reload your terminal (using source ~/.zshrc) for them to take effect.

The syntax for declaring a (simple) alias is as follows:

alias [alias-name]='[command]'

Aliases are great for often-used commands, long commands, or commands with a hard-to-remember syntax. Here are some of the ones I use on a regular basis:

  • A myip alias, which prints your current public IP address to the terminal: alias myip='curl http://ipecho.net/plain; echo'.
  • A distro alias to output information about your Linux distribution: alias distro='cat /etc/*-release'.
  • A reload alias, as I can never seem to remember how to reload my terminal: alias reload='source ~/.zshrc'.
  • An undo-git-reset alias: alias undo-git-reset-head="git reset 'HEAD@{1}'". This reverts the effects of running git reset HEAD~.
  • An alias to update package lists: alias sapu='sudo apt-get update'.
  • An alias to rerun the previous command with sudo: alias ffs='sudo !!'.
  • Because I’m lazy, I have aliased y to the yarn command: alias y='yarn'. This means I can clone a repo, then just type y to pull in all the dependencies. I learned this one from Scott Tolinski on Syntax.
  • Not one of the ones I use, but this alias blows away the node_modules folder and removes the package-lock.json file, before reinstalling a project's dependencies: alias yolo='rm -rf node_modules/ && rm package-lock.json && yarn install'. As you probably know, yolo stands for You Only Live Once.
  • An alias to open my .zshrc file for editing: alias zshconfig='subl $HOME/.zshrc'.
  • An alias to update the list of Ruby versions rbenv can install: alias update-available-rubies='cd ~/.rbenv/plugins/ruby-build && git pull'
  • An alias to kick off a server in your current directory (no npm packages required): alias server='python -m SimpleHTTPServer 8000'.
  • You can also create an alias to open documentation in your browser: alias npmhelp='firefox https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/npm'.
  • A global alias to pipe a command's output to less: alias -g L='| less'. You can use it like so: cat production.log L.
  • A global alias to pipe a command’s output to grep: alias -g G='| grep'. You can use it like so: history G ssh.
  • You can also use functions to create aliases. The following (taken from here) creates an alias that adds, commits, and pushes code to GitHub:
    bash
    function acp() {
    git add .
    git commit -m "$1"
    git push
    }

There are lots of places to find more ideas for aliases online. For example, this Hacker News discussion, or this post on command line productivity with Zsh.

The post 75 Zsh Commands, Plugins, Aliases and Tools appeared first on SitePoint.



No comments:

Post a Comment