Zsh: 75 commands, plugins, alias and tools to help you improve terminal efficiency
I work most of my time every day at the terminal, and the shell I chose is Zsh – a highly customizable Unix shell with many powerful features. As a lazy developer™, I've been looking for ways to reduce the amount of input and automate all tasks. Fortunately, Zsh was born for this.
In this article, I will share 75 commands, plugins, alias and tools, hoping to help you save some key presses and improve your daily work efficiency.
If you haven't installed Zsh on your machine, check out this article and I'll show you how to get started quickly.
Key Points
Zsh's 15 out-of-the-box features
Zsh shares many convenient features with Bash. The following features are not unique to Zsh, but are still worth knowing. I encourage you to start using the command line to do the actions listed below. At first it may seem like it's more laborious than using a GUI, but once you get the trick, you'll never regret it.
cd
from anywhere in the file system will take you back to your home directory directly. !!
will call up the last command. This will be very convenient if the command fails because administrator privileges are required. In this case, you can enter sudo !!
. &&
to link multiple commands. For example, mkdir project && cd project && npm init -y
. ||
to perform conditional execution. For example, git commit -m "whatever..." || echo "Commit failed"
. mkdir
command's -p
switch will allow you to create a parent directory as needed. Use curly braces to expand to reduce duplication. For example, mkdir -p articles/jim/sitepoint/article{1,2,3}
. NODE_DEBUG=myapp node index.js
. Or, set on a per-session basis: export NODE_DEBUG=myapp
. You can check if it is set by typing echo $variable-name
. cat /var/log/kern.log | less
makes long logs easy to read, or history | grep ssh
searches for any history entry containing "ssh". nano ~/.zshrc
(nano), subl ~/.zshrc
(Sublime Text), code ~/.zshrc
(VS Code). If the file does not exist, it will be created when you press "Save" in the editor. <kbd>Ctrl</kbd> <kbd>a</kbd>
will take you to the beginning of a line. <kbd>Ctrl</kbd> <kbd>e</kbd>
will take you to the end. <kbd>Ctrl</kbd> <kbd>w</kbd>
to delete a word (backward). <kbd>Ctrl</kbd> <kbd>u</kbd>
will delete everything from the cursor to the beginning of the line. <kbd>Ctrl</kbd> <kbd>k</kbd>
Clears everything from the cursor to the end of the line. The last three can be undoed using <kbd>Ctrl</kbd> <kbd>y</kbd>
. <kbd>Ctrl</kbd> <kbd>Shift</kbd> <kbd>c</kbd>
to copy text. This is much more elegant than right-clicking and selecting Copy. <kbd>Ctrl</kbd> <kbd>shift</kbd> <kbd>v</kbd>
to paste copied text. Try to remember these key combinations. You'll be surprised at how often they come in handy.
15 custom aliases to increase your productivity
Alias is a terminal shortcut for regular commands. You can add them to your ~/.zshrc
file and reload your terminal (using source ~/.zshrc
) to make them take effect.
The syntax for declaring (simple) alias is as follows:
<code>alias [alias-name]='[command]'</code>
Alias is perfect for common commands, long commands, or commands that are difficult to remember in syntax. Here are some aliases that I often use:
A myip
alias that prints your current public IP address to the terminal: alias myip='curl http://ipecho.net/plain; echo'
.
A distro
a alias distro='cat /etc/*-release'
alias that outputs information about your Linux distribution:
reload
A alias reload='source ~/.zshrc'
a
undo-git-reset
alias undo-git-reset-head="git reset 'HEAD@{1}'"
A git reset HEAD~
Alias:
alias sapu='sudo apt-get update'
alias ffs='sudo !!'
y
yarn
Because I'm lazy, I'll use alias y='yarn'
as an alias for the <kbd>y</kbd>
command:
node_modules
package-lock.json
I don't use it often, but this alias clears the alias yolo='rm -rf node_modules/ && rm package-lock.json && yarn install'
folder and deletes the file, and then reinstalls the project's dependencies: . As you may know, yolo stands for You Only Live
.zshrc
alias zshconfig='subl $HOME/.zshrc'
A alias that opens my
alias update-available-rubies='cd ~/.rbenv/plugins/ruby-build && git pull'
alias server='python -m SimpleHTTPServer 8000'
alias npmhelp='firefox https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/npm'
alias -g L='| less'
cat production.log L
A global alias for pipes the output of the command to less:
alias -g G='| grep'
history G ssh
A global alias for pipes the output of the command to grep:
You can also use functions to create alias. The following (taken from here) creates an alias to add, commit and push code to GitHub:
<code>alias [alias-name]='[command]'</code>
There are many places to find more ideas about alias online. For example, this Hacker News discussion, or this article about using Zsh to improve command line productivity.
15 cool things you can do with (Oh My) Zsh
Oh My Zsh is a community-driven framework for managing your Zsh configuration and bundled with thousands of useful functions, assistants, plugins, and themes. If you are going to use the Z shell as your daily driver, you should really install Oh My Zsh.
Here are fifteen useful things Oh My Zsh can do for you:
take
command will create a new directory and enter the directory. take my-project
Replace mkdir my-project && cd my-project
. zsh_stats
will provide you with a list of the first 20 commands and their number of runs. ..
is an alias for cd ...
. ...
moves you upwards two directories, ....
moves three upwards, and .....
moves four upwards. cd
when navigating. For example, entering /
will take you directly to your file system root directory. /h/j/De
and press <kbd>TAB</kbd>
and then <kbd>Return</kbd>
will take me to /home/jim/Desktop/
. rd
is an alias for rmdir
and md
is an alias for mkdir -p
. d
to list the last directory used in the terminal session. cd -n
to navigate to any of these directories, where n is the directory number. ls -
and pressing <kbd>TAB</kbd>
will list all command options, as well as a useful description of their functions. This also applies to cap
, rake
, ssh
and kill
. alias
All your current aliases will be listed. ls *.html
will list all HTML files in the current directory. To include subdirectories, change to: ls **/*.html
. ls -l **/*(.x)
will find all executables in the current directory and all subdirectories. ls *(m-7)
will list all files modified in the past week. ls *(Lm 1)
will look for all files with a size greater than 1MB. Get fun and benefits with plug-ins
Oh My Zsh comes with a large number of plugins. You should browse these plugins and invest some time learning those that can help your workflow.
The following are three plugins I often use that provide a lot of handy shortcuts and aliases.
10 beautiful Git alias
git plugin provides many aliases and some useful functions. Why not browse these and try to remember the ten you use most often? Here are the ones I use most.
g
is a convenient alias for git
. This means you can type something like g clone <whatever></whatever>
instead of git clone <whatever></whatever>
. There may be only two keys, but they will accumulate soon. gaa
is an alias for git add all
. I've been using this all the time. gb
is an alias for git branch
that will list all branches in the current repository and show which branch you are currently in. gcb
is an alias for git checkout -b
, which allows you to create a new branch. gcm
is an alias for git checkout master
. This returns you to the main branch. gdca
is an alias for git diff --cached
. This allows you to make a differential comparison of any files that have been staging for submission. gf
is an alias for git fetch
. gm
is an alias for git merge
. gp
is an alias for git push
. To synchronize the branches of the repository, you can do: gf upstream
, gm upstream/master
, and then gp
. glog
is an alias for git log --oneline --decorate --graph
, which will give you a beautiful git branch graph. 10 convenient npm alias
Thenpm plugin provides completion and many useful aliases.
npmg
is an alias for npm install --global
that you can use to install dependencies globally on your system. For example, npmg nodemon
. npmS
is an alias for npm install --save
that you can use to install dependencies and add them to your package.json
section of dependencies
. Note that starting with npm 5.0.0, this is the default when running npm i <package></package>
. npmD
is an alias for npm install --save-dev
that you can use to install dependencies and add them to your package.json
section of devDependencies
. npmO
is an alias for npm outdated
that will check the registry to see if any (or specific) installed packages are currently expired. npmL
is an alias for npm list
that will list installed packages. npmL0
is an alias for npm list --depth=0
that lists top-level packages. This is especially useful for viewing which modules are installed globally without flooding your terminal with a huge dependency tree: npmL0 -g
. npmst
is an alias for npm run start
, a npm script commonly used to start applications. npmt
is an alias for npm run test
, and as you might guess, it is used to run your tests. npmR
is an alias for npm run
. It itself will list all available npm scripts for the project, as well as a description of their functionality. When used with the script name, it will run the script, for example, npmR build
. npmI
is an alias for npm init
. This will ask you some questions and then create a package.json
based on your answer. Use the -y
flag to automate this process. For example, npmI -y
. 10 Time-saving Rails/Rake Alias
This plugin adds completion of the Ruby on Rails framework and Rake programs, as well as some aliases for logs and environment variables.
rc
is an alias for rails console
that allows you to interact with your Rails application from the CLI. rdc
is an alias for rake db:create
, which (unless RAILS_ENV
is set) will create a development and test database for your application. rdd
is an alias for rake db:drop
that will delete the development and testing database of your application. rdm
is an alias for rake db:migrate
that will run any pending database migrations. rds
is an alias for rake db:seed
that runs the db/seeds.rb
file to populate your development database with data. rgen
is an alias for rails generate
that will generate boilerplate code. For example: rgen scaffold item name:string description:text
. rgm
is an alias for rails generate migration
that will generate a database migration. For example: rgm add_description_to_products description:string
. rr
is an alias for rake routes
that lists all defined routes for the application. rrg
is an alias for rake routes | grep
that allows you to list and filter defined routes. For example, rrg user
. rs
is an alias for rails server
, which starts the Rails default web server. Other Resources
The main job of the plug-in listed above is to provide alias for common commands. Note that there are many other plugins that can add additional features to your shell.
The following are four of my favorites:
sudo
allows you to easily prefix the current or previous command by pressing <kbd>ESC</kbd>
twice. sudo
autosuggestions
key to accept it. A real time-saving tool! <kbd>→</kbd>
command-not-found
, it will use the Ubuntu's $PATH
package to find it or suggest a misspelling. command-not-found
z
You can learn more about the topic in my article 10 Zsh tips and tricks: Configuration, Customization, and Usage.
Conclusion
It's like this: 75 Zsh commands, plugins, alias, and tools. I hope you have learned a trick or two during the learning process and I encourage you to leave your GUI and go to the terminal. It's much easier than it seems and is a great way to increase productivity.
If I missed your favorite plugin or time-saving alias/command, please let me know on Twitter.
Want to get more from your toolkit? Check out Wiley's Visual Studio Code: End-to-end editing and debugging tools for web developers.
FAQs about Zsh commands, plugins, alias, and tools
Zsh, also known as Z shell, is a powerful shell that contains features from other shells such as Bash, tcsh, and ksh. It offers some advantages over these shells. First of all, Zsh has powerful autocomplete features that can suggest commands, file names, options, and even host names. This feature can significantly speed up your workflow. Second, Zsh supports sharing command history, which allows you to view commands typed in another terminal session. Third, Zsh allows you to use Emacs and vi editing modes at the same time, which makes it more flexible for different users. Finally, Zsh has a powerful scripting language with features such as associative arrays and floating point operations that are not available in Bash.
Zsh allows you to customize your prompt using the PROMPT
variable. You can set this variable in your .zshrc
file. For example, if you want to display the current directory in your prompt, you can use the %~
parameter. Therefore, your PROMPT
variable will look like this: PROMPT='%~%# '
. You can also add colors to your prompt using the %F{color}
parameter. For example, to make your prompt green, you can use: PROMPT='%F{green}%~%# %f'
. %f
Reset the color to the default color.
Aliases in Zsh can help you save time by allowing you to create shortcuts for long or common commands. You can create an alias by using the alias
command followed by the alias and the command you want to be quick. For example, to create an alias for the ls -l
command, you can use: alias ll='ls -l'
. You can add this line to your .zshrc
file to make the alias permanently effective.
Zsh plugin is a script that adds additional features to your Zsh shell. They can help you automate tasks, add new features, or customize your shell. To use the Zsh plugin, you first need to install it. This usually involves cloning the plugin's repository into your .oh-my-zsh/plugins
directory and then adding the plugin to the .zshrc
array in your plugins
file. Once the plugin is installed, you can start using its features.
Switching from Bash to Zsh is an easy process. First, you need to install Zsh using your package manager. For example, on Ubuntu, you can use: sudo apt install zsh
. After installing Zsh, you can set it as your default shell using the chsh
command: chsh -s $(which zsh)
. The next time you open the terminal, it will use Zsh.
Zsh theme allows you to customize the appearance of the shell. You can change the color scheme, prompt layout, and even add elements such as the current time or git status. To use the Zsh theme, you first need to install it. This usually involves cloning the repository of the topic into your .oh-my-zsh/themes
directory and setting the .zshrc
variable in your ZSH_THEME
file to the name of the topic. After installing the theme, you can activate it by getting your .zshrc
file: source ~/.zshrc
.
Oh My Zsh is a community-driven framework for managing your Zsh configuration. It comes with many useful functions, plugins, and themes. To use Oh My Zsh, you first need to install it. This usually involves downloading the installation script and running it in your terminal. After installing Oh My Zsh, you can start customizing your shell by editing your .zshrc
file.
Zsh function allows you to group commands together and execute them as a single command. You can define a function using the function
keyword followed by the function name and command block. For example, to create a function that prints "Hello, world!", you can use: function hello { echo "Hello, world!"; }
. You can call this function by typing the function name: hello
.
Zsh supports indexed arrays and associative arrays. You can use the set
command followed by the array name and element to create an index array. For example, to create a color array, you can use: set -A colors red green blue
. You can access elements of an array using its index: echo $colors[1]
. To create an associative array, you can use the typeset
command: typeset -A colors; colors[red]=#FF0000; colors[green]=#00FF00; colors[blue]=#0000FF;
.
Zsh supports several types of loops, including for, while, and until loops. The for loop allows you to execute command blocks repeatedly for each element in the list. For example, to print numbers from 1 to 5, you can use: for i in {1..5}; do echo $i; done
. The while loop allows you to repeat the command block when the condition is true. For example, to print numbers from 1 to 5, you can use: i=1; while (( i < 6 )); do echo $i; i=$((i 1)); done
. The until loop allows you to repeat the command block when the condition is false. For example, to print numbers from 1 to 5, you can use: i=1; until (( i > 5 )); do echo $i; i=$((i 1)); done
.
The above is the detailed content of 75 Zsh Commands, Plugins, Aliases and Tools. For more information, please follow other related articles on the PHP Chinese website!