Command line proficiency on the Mac system can make work more efficient, almost all operations can be done from the command line. However, the premise of these operations is to remember various commands, and the native terminal of the system does not have auto-complete function, which makes us used to Xcode especially uncomfortable. In addition, switching paths under the command line is also tedious. In this article, you’ll find some tools and Settings that make command execution easier and smarter to use.

Automatic completion

First, let’s add auto-completion to the terminal command line by installing bash_completion at Homebrew. Of course you need to install BREW first:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code

Enter the above command on the command line to install Homebrew. Homebrew is a software package management tool for Mac OS with many functions including install, uninstall, update, view, search, and so on. Package management can be implemented with a single instruction, regardless of dependencies and file paths.

Homebrew installs the software into a separate directory and links the files to the /usr/local path

Once Homebrew is installed, you can use it to install bash_completion by typing the following command from your terminal:

brew install bash-completion
Copy the code

When the installation is complete, you will be prompted:

# Add the following lines to your ~/.bash_profile:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi
Copy the code

Copy the above statements (the last three lines) into the.bash_profile file as prompted. Note that.bash_profile is a hidden file, so show all files first and then jump to it by pressing Command+Shift+G in Finder.

Bash-completion is installed on the default brew and can be viewed through the brew List without performing the above installation steps.

After completing the above operations, you can use autocomplete, for example, if you want to open a file in a certain path, but you can’t remember the name of the file, or you only remember the first few letters, with autocomplete, you just type open and press TAB to see the file in the directory. Then press TAB to select the file name you want to open and press Enter to confirm. The effect is as follows:

Git command completion: Git command completion: Git command completion: Git command completion Go to the git home page to download the git-completion.bash file in contrib/completion/ and put the file in your home directory:

Then modify the.bash_profile file by adding the following:

mv git-completion.bash ~/.git_completion.bash
# Add to your .bash_profile:
source ~/.git_completion.bash
Copy the code

Git auto-complete: Git auto-complete: git auto-complete

Path switching

To change the path on a Mac, run the CD command. For example, enter:

cd /Users/Ryan/iOSDev/Documents 
Copy the code

Jump to the Documents directory, but the tricky part is that you need to enter the full path each time. Can the command line remember some common paths and switch them quickly? You can! This can be done using Autojump. Autojump is a command line tool that allows you tojump directly to your usual directories, regardless of the current directory.

ZSH is a type of shell. On Mac OS, the default shell is bash, but ZSH is a more powerful shell and fully compatible with bash. Let’s look at how to install and switch to ZSH:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Copy the code

To install ZSH, enter the above command in the command line. After successful installation, you need to set the default shell of the system to ZSH:

chsh -s /bin/zsh
Copy the code

This command will restart the shell, and when done we will type on the command line:

echo $SHELL
Copy the code

You can see which shell (bash or ZSH) is currently in use.

Shell is actually a program written in C language. The commands we input on the command line are transmitted to the operating system (kernel) for execution after shell interpretation.

After the shell switch, we can install Autojump using brew as above. Type the following command on the command line:

brew install autojump
Copy the code

After the installation is complete, the.zshrc file will appear in the system user root directory (the same directory as the.bash_profile file above). Jump to this file and open it with a text editor. Find plugins= in this file and change it to:

plugins=(
    git autojump
)
Copy the code

Create a new line and add:

[[ -s $(brew --prefix)/etc/profile.d/autojump.sh ]] && . $(brew --prefix)/etc/profile.d/autojump.sh
Copy the code

After the modification, ZSHRC is shown as the following figure:

After installation, we can use Autojump tojump to a path quickly. In Autojump, we use j(alias) instead of CD. For example, we want tojump to a path, but we can’t remember the full name of the path, just remember the word perfect. Then directly enter J Perfect in the command line and press TAB, the path containing perfect will come out. Press TAB again to select the path you need to enter and press Enter to switch to the path:

Autojump sorts accessed files and folders by frequency of use, so the paths that you want tojump to quickly by AutoJump must be those that have previously accessed files and folders in the database, so that autoJump can list files and folders by frequency.

The above is just an introduction to the basic features of Autojump, but a full list of features can be found on autojump’s home page. I won’t go into any more details here. There is also a similar quick jump tool, FASD, if you are interested.

In addition, there is also a cheating software TermHere. After downloading and installing it, right-click the mouse in any position (on the folder or in the directory inside the folder), you will find a menu item “New Terminal Here”, click it, and a Terminal window will appear, and the current directory is the position you point to.

Command alias

Sometimes the command line command is too long, we can use an alias instead, similar to the macro in the program. For example, our command to display hidden files is:

defaults write com.apple.finder AppleShowAllFiles true ; killall Finder
Copy the code

This command is too long, we can add an alias instead. Open the.zshrc file and add the following:

alias sfy="defaults write com.apple.finder AppleShowAllFiles true ; killall Finder"
alias sfn="defaults write com.apple.finder AppleShowAllFiles false ; killall Finder"
Copy the code

It should be noted that there is no space on either side of the equal sign. If there is a space in the command name, it should be enclosed in quotation marks. The specific format is:

alias[alias] ='[directive name]'
Copy the code

After adding, as shown in the figure below, we added two aliases sfy and SFN to show hidden files and not to show hidden files respectively. Entering these two commands on the command line has the same effect as the long sequence above.

Save and close the. ZSHRC file, restart the command line and execute the alias command. The result is as follows:

If you have not switched shells and are still under bash by default, you need to add the alias content to bash’s.bash_profile file.

Refer to the article

  1. MAC Command Line Autocomplete (git/ Maven)
  2. Introduction and use of Homebrew
  3. MAC installation autojump
  4. Mac Adds a command alias