As a web developer, the command line is becoming an increasingly important part of our workflow. We use it to install packages from npm, test API endpoints, push commits to GitHub, and much more.
My mollusk of choice isfor example. It's a highly customizable Unix shell that includes some very powerful features like tab completion, smart history, remote file extension, anda lot more.
In this article, I'll show you how to install zsh, and then I'll give you ten tips and tricks to help you be more productive with it.
This is a beginner's guide that anyone can follow (even Windows users, thanks to Windows Subsystem for Linux). Given Apple's announcement thatzsh is now the default shell on macOS Catalina, Mac users may find it particularly useful.
Let's begin.
Installation
I don't intend to provide detailed installation instructions for each operating system, just some general guidelines. If you are unable to install zsh, there is a lot of help online.
At the time of writing, the current version of zsh is 5.7.1.
MacOS
Most versions of macOS come with zsh pre-installed. You can check whether this is the case, and if so, which version you are using, using the command:zsh --version
. If the version is 4.3.9 or higher, you should be good to go (we need at least this version to install Oh My Zsh later). If not, you can continue.this guideto install a newer version of zsh using homebrew.
After installation, you can set zsh as the default shell using:chsh -s $(era zsh)
. After issuing this command, you must log out and log back in for the changes to take effect.
If at any point you decide you don't like zsh, you can use:chsh -s $(was bash)
.
Linux
On Ubuntu-based distributions, you can install zsh with:sudo apt-get install zsh
. When the installation is complete, you can check the version withzsh --version
, then make zsh your default shell withchsh -s $(era zsh)
. You must sign out and then sign back in for the changes to take effect.
Just like macOS, you can switch back to bash with:chsh -s $(was bash)
.
If you are running a non-Ubuntu-based distribution, read theInstructions for other distributions.
Window
Unfortunately, this is where things start to get a little tricky. Zsh is a Unix shell and to work on Windows you need to activate itSubsistema do Windows para Linux (WSL), an environment on Windows 10 and 11 for running Linux binaries.
There are several tutorials online that explain how to get started with zsh on Windows 10s. These two seemed timely and easy to follow:
- How to Install and Use Linux Bash Shell on Windows 10- Follow these steps first to install WSL
- How to use Zsh (or any other shell) in Windows 10- Follow this second to install zsh
Note that it is also possible to run zsh withCygwin. Are hereinstructions on how to do this.
First try
When you open zsh for the first time, the following menu will appear.
if you choose(1)
You access a menu that allows you to configure history, keyboard shortcuts and many other things. However, I recommend choosing(2)
This creates a configuration profile with recommended default settings.
Configuration file location
Now let's look at the zsh file you just created. Go to your home directory and open the.zshrc
file in the editor of your choice. This file runs every time you start zsh and contains all your custom settings.
To use: Filenames that start with a dot are hidden by default on most operating systems, so you must make them visible before you can edit them. If you're not sure how to do this, search for "Show hidden point files mac/linux etc.".
We can demonstrate how this file works by creating an alias. In their simplest form, aliases are terminal shortcuts to regular commands. Add the following at the end.zshrc
and save the file:
pseudonym my IP="curl http://ipecho.net/plain; eco"
Restart your shell (for example, by closing and reopening it) and typemy IP
. As long as you have itProgram cURLinstalled, you should see your current public IP address output in the terminal. If you want to experiment more with aliases, see7 super useful aliases to make your development life easier.
Close the terminal by typingsalida
, then look for the.zsh_historia
File also located in your home directory. This is where zsh stores a list of your previous terminal commands. Currently it should contain two lines vizmy IP
jsalida
.
To use:We'll reload our shell several times throughout the tutorial. You can also do this with one command:
Quelle ~/.zshrc
.
check your progress
At this point you should have zsh installed and set as your default shell. If you are having problems, try creating a topicSitePoint-Forenand ask for help there.
Now let's move on to some tips.
1. Instale Oh My Zsh
If you only follow one of these tips, it has to be this one.
oh my zshis a community-based framework for managing your zsh configuration and includes thousands of useful functions, helpers, plugins and themes. Many of the tips below depend on whether you have it installed.
As explained on the project's home page, you can install it with cURL or wget.
Sch-C"p.sreis-fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Sch-C"p.swgethttps://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
This assumes you have git installed, which we hope you already have. if you can'tget it from the project home page.
As you can see from the message after installation, Oh My Zsh has made a backup of you..zshrc
file and then replace it with your own version. This means that you can use any custom configuration (like ourmy IP
alias) to your new.zshrc
File.
2. Choose a topic
If you spend a lot of time on the terminal, it's worth putting a little effort into making it visually appealing. Fortunately, Oh My Zsh is includedMany subjects.
If you look at the~/.zshrc
file for theZSH_THEME
variable, you'll notice it's set torobbie russel
. This is the default theme used by the creator of Oh My Zsh.
To install a different theme, select one from the link above and change the value ofZSH_THEME
variable before restarting your shell. set for exampleZSH_THEME="avito"
gonna use Oh My Zshtheme grandpa.
You can also change your terminal's color scheme. On my Linux machine I was able to do this viaTo edit>preferences>cores. remove tickUse system theme colors, then selectsolarized darknessAs a scheme, it gives me a very attractive result in a very short time.
Notice how the theme recognizes that it's in a Git repository, tells me which branch I'm on, and displays a green check mark to let me know that everything is up to date.
I encourage you to look through the available themes and find one that suits you.
Problems solution
Testing this in WSL, I saw garbled characters in my terminal as soon as I installed Oh My Zsh. To fix this I had to install thepower line suppliesand tell my terminal to use them.
gitClone https://github.com/powerline/fonts.git
Then open an admin PowerShell, navigate to the root of the repository and run:
.\install.ps1
If PowerShell is preventing you from running the script, try theexecution policy
consequentlyReply to Stack Overflow. After installing the fonts, make sure to configure them again.
Finally, right-click on the terminal's title bar and selectCharacteristics>sourceand be sure to select a powerline font. in tests,Deja Vu without mono for powerlineIt worked fine for me.
Also note that some themes require powerline fonts regardless of operating system. We will see the installation of one of these themes (Agnostic) at the end of the article.
3. Working with the story
One of my favorite features of Oh My Zsh is the way you can search your history. Imagine that the content of.zsh_historia
Looks like it:
CDMesamkdirMy projectCDMy projectnpm-y initializemkdir {origin, structure}to knockindex.js
If you are in the terminal and press the button↑Keys, you would expect to iterate through each of these history entries one by one, right? There's no surprise there.
However, Oh My Zsh allows you to type the start of a command and press the key↑to scroll through the corresponding entries. So, in the example above, if you typed "mkdir" and pressed the↑press , you will see "mkdir {src, build}" on the screen. If you press it again you will see "mkdir my-project".
I use this feature all the time. For example, if I want to log in to a server and I can't remember the exact credentials, I just type "ssh" and I can scroll through recent connections until I find the right one. Or, if I can't remember the exact syntax of a command, I can just type the first few characters and repeat the matches.
4. FTW auto-suggestions!
Another great feature of Oh My Zsh isadded. They add new features to your shell and increase its functionality.
Oh My Zsh is includedhundreds of pluginsfor every imaginable use and we'll look at some of them later in this article. However, in this section, I would like to introduce you to a plugin called Third Partyautosuggestion. This suggests commands based on history as you type.
To install you need to clone your repository$ZSH_CUSTOM/add-ons
. By default, this is~/.oh-my-zsh/custom/plugins
.
gitClone https://github.com/zsh-users/zsh-autosuggestions${ZSH_CUSTOM:-~/.oh-mi-zsh/Habit}/plugins/zsh-autossugestões
Once that's done, to activate it, jump to~/.zshrc
file and comment out the following line:
# add-ons = (git)
then replacegit
swindlerautosugestö zsh
added=(autosugestö zsh)
Restart your shell and you'll find Oh My Zsh making suggestions based on your history whenever you type a command. If the suggestion is what you are looking for, press the button→key to accept it. A real time saver!
5. Browsing files with Oh My Zsh
Another amazing thing about Oh My Zsh is how easy it is to navigate your computer.
For example, if you want to change the directory, you don't need to enter itCD
command, that isDesktop-CD
Will be easyMesa
. And this also works with routes - you can board/start/jim/Desktop
and end up exactly where you wanted to be.
Oh My Zsh also has excellent autocomplete. Once you start entering a route, you can pressEYELASHthen it expands a list of all possible folders that you can navigate to. If you continue with the tab key, you can scroll through this list until you select a folder by pressingGive back. PressEYELASHAfter that, the process starts again.
Finally, it's worth noting that Oh My Zsh offers a range ofCommands and aliases for working with and navigating directories. write for example...
will move up two directories and writeget <directory name>
creates a new directory and navigates to it. Oh My Zsh also supports dynamic path completion, so write (for example)/h/d/since
and pressureeyelashwill clear the way/start/jim/Desktop
. Amazing right?
Bonus-Tipp: If you're feeling lazy, just drag and drop a folder into the terminal window and the path will be expanded for you.
6. Use z to navigate to anything
z is a useful pluginwhich creates a list of your most frequent and recent folders (call them "frequent") and lets you jump to them with a single command. This is often much more convenient than having to navigate through a nested folder structure.
To install the plugin just add it to your.zshrc
file like this:
added=(from zsh autossugestões)
Then restart your terminal.
Once installed, z will have a short learning period as you watch it navigate your PC using the terminal. But after a while you will be able to writez
followed by the word found in the desired directory path. The plugin uses fuzzy matching to figure out which folder you want to go to and expands the path accordingly. If there is more than one option, you can cycle through the options as described in the previous tip.
This might not seem like a big problem, but you can open a shell, typez my project
and make it expand the path/home/jim/files/some/deeply/nested/directory/or/other/my-project
. Then when you are in that directory you can typez my-other-project
and make it expand the path/var/www/html/projects/top/secret/my-other.project
.
To use:The first time I ran z and tried to change the directory I got the error
_z_dirs:2: no such file or directory: /home/jim/.z
. This was an exception, as z apparently had to do a.z
file in my home directory. If you find any problems,This article provides some suggestions.
7. Working with Git
Zsh comes with some basic Git integrations out of the box. For example, when you writegit
and press the buttoneyelashkey, zsh displays a long list of possible Git commands. This becomes a little more useful as you type.git
, followed by the first two letters of the command you are looking for. For exampleBecomes
+eyelashto product:
Add to--Add to filecontent to index -- Apply patches from an anapply mailbox -- Apply patches to files and/or indeximport --Objectein Arch-Repository emgitarchive - Creates an archive of files from the named tree
Things start to get more interesting when you install them.Suplemento Oh My Zsh git.
added=(git z zsh-autosuggestions)
This provides a large number of aliases, as well as some useful features for working with Git. Some of the ones I use frequently are:
Owner
–add all
gdca
–git diff - em cache
GP
–git push
gpf!
–git push --force
gghhh
–git reset --hart
gst
–Git-Status
You can also alias multiple commands at once by adding the following to your.zshrc
File:
function acp() { git Add to . gitcometer-m"$ 1" gitpress}
If you are now in a Git repository, type the followingacp "an appointment message"
It adds all changed files, commits them with the commit message you specify, and sends them to your remote. Credits go to this ideaAli salivated.
8. Get instant feedback on the status of a Git repository
At the beginning of the article we enabled a theme calledgrandmother. In a Git repository, this topic displays the name of the branch and a cross or check mark indicating whether the entire branch is up to date. I love being able to see this information at a glance and it's definitely a huge step up from typingGit-Status
all the time.
However, there are some themes that display this information in a more intuitive way. my favorite isAgnostic. Let's install it now.
In order for Agnoster to render Git arrows and icons correctly, you will probably need aFonte do patch Powerline. WSL users can do this by following the instructions atTroubleshooting sectionabove. For Mac and Linux UsersInstallation instructions can be found in the project repository.
Mac users running High Sierra or later may need to do additional work to see these fonts. Unless you're in Catalinathis method is recommended. Catalina users must install acorrected fontand select it in your terminal settings.
To test whether your terminal can display all required characters. Run the following command:
Echo "\ue0b0 \u00b1 \ue0a0 \u27a6 \u2718 \u26a1 \u2699"
The result should look like this:
After that, activate the theme by changing the following line in your.zshrc
File:
ZSH_THEME="agnostic"
and restart the terminal. Once Agnoster is up and running, you'll have a more intuitive view of the status of any Git repository. You also get visual feedback on whether or not the previous command was successful and whether you are working with elevated privileges.
Adviceif you don't like him
user@host
is part of the indicator, you can remove it by adding itDEFAULT_USER=your_username
for.zshrc
File.
9. Plugins, Plugins, Plugins
As mentioned,Oh My Zsh is includedquiteof supplements. You really should check them out and spend some time learning the ones that will help your workflow. To get you started, here are a few popular ones to keep in mind:
- cloud app- Uploading files and targeted content to the Cloudapp service. Preview the resulting URL and copy it to your clipboard.
- command not found– if a command is not recognized in the
$RUTA
, this will use the Ubuntu package not found command to find it or suggest misspellings (Ubuntu and openSUSE only). - It- Open the Node API for your current version in your browser.
- npm– Add autocompletion to npm.
- sudo- blowESCtwice puts sudo before the current command or last if the command line is empty.
- vscode- Facilitates interaction between the command line and the code editor.
10. Keyboard shortcuts
This isn't a zsh-specific tip, but finally, I'd like to share these five time-saving keyboard shortcuts with you.
- to control+tu– delete from the cursor to the beginning of the line.
- to control+k– delete from the cursor to the end of the line.
- to control+C– Delete from the cursor to the beginning of the previous word.
- Alternative+D– delete from the cursor to the end of the next word.
- to control+eu– Delete terminal.
If you can memorize them, you'll be surprised how often they come in handy.
Diploma
In this post I showed how to install zsh and set it as default shell. I also showed you how to get the most out of your new terminal using the Oh My Zsh framework. Hopefully, after trying them for a few weeks, you'll wonder how you ever lived without them.
And don't forget, if you have a problem with this, you can skip it.SitePoint-Forenand ask for help there. I would also like to know if I missed anything, e.g. B. Your favorite topic, plugin, alias, or productivity tip.
You can contact me on the forum or atblood.