zsh 别名
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.
我花了很多时间在终端上,我选择的shell是Zsh,Zsh是一种高度可定制的Unix shell,其中包含一些非常强大的功能。 因为我是一个懒惰的开发人员 TM ,所以我一直在寻找减少打字和自动化所有事情的方法。 幸运的是,这是Zsh非常适合的。
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.
在本文中,我将与您分享75个命令,插件,别名和工具,这些命令,插件,别名和工具有望为您节省一些按键操作,并使您的日常工作效率更高。
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.
如果您的计算机上未安装Zsh,请查看这篇文章 ,我将向您展示如何启动和运行它。
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.
Zsh与Bash共享许多方便的功能。 以下内容都不是Zsh所独有的,但是仍然很高兴知道。 我鼓励您开始使用命令行来执行以下所列的操作。 与起初使用GUI相比,这似乎需要做更多的工作,但是一旦掌握了一切,就永远不会回头。
Entering cd
from anywhere on the file system will bring you straight back to your home directory.
从文件系统上的任何位置输入cd
,将使您直接回到主目录。
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 !!
.
输入!!
将显示最后一条命令。 如果命令因为需要管理员权限而失败,这很方便。 在这种情况下,您可以输入sudo !!
。
You can use &&
to chain multiple commands. For example, mkdir project && cd project && npm init -y
.
您可以使用&&
链接多个命令。 例如, mkdir project && cd project && npm init -y
。
Conditional execution is possible using ||
. For example, git commit -m "whatever..." || echo "Commit failed"
.
使用||
可以有条件地执行 。 例如, git commit -m "whatever..." || echo "Commit failed"
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}
.
在mkdir
命令中使用-p
开关将允许您根据需要创建父目录。 使用括号扩展可减少重复。 例如, 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 $
.
像这样按每个命令设置环境变量 : NODE_DEBUG=myapp node index.js
。 或者,基于每个会话,如下所示: export NODE_DEBUG=myapp
。 您可以通过键入echo $
来检查它是否已设置。
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”.
将一个命令的输出通过管道传递给第二个命令。 例如, cat /var/log/kern.log | less
cat /var/log/kern.log | less
使长日志可读的history | grep ssh
history | grep ssh
搜索任何包含“ 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.
您可以从终端在编辑器中打开文件。 例如, nano ~/.zshrc
(纳米), subl ~/.zshrc
(卓异文本), code ~/.zshrc
(VS码)。 如果文件不存在,则在编辑器中按“ 保存”时将创建该文件。
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.
导航是一项重要的技能。 不要仅仅依靠箭头键。 例如, Ctrl + a将带您到行首。
Whereas Ctrl + e will take you to the end.
而Ctrl + e会将您带到最后。
You can use Ctrl + w to delete one word (backwards).
您可以使用Ctrl + w删除一个单词(向后)。
Ctrl + u will remove everything from the cursor to the beginning of the line.
Ctrl + u将从光标到行首的所有内容删除。
Ctrl + k will clear everything from the cursor to the end of the line. These last three can be undone with Ctrl + y.
Ctrl + k会清除从光标到行尾的所有内容。 最后三个可以通过Ctrl + y撤消。
You can copy text with Ctrl + Shift + c. This is much more elegant than right clicking and selecting Copy.
您可以使用Ctrl + Shift + c复制文本。 这比右键单击并选择“ 复制”要优雅得多。
Conversely, you can paste copied text with Ctrl + shift + v.
相反,您可以使用Ctrl + shift + v粘贴复制的文本。
Try to commit those key combos to memory. You’ll be surprised at how often they come in handy.
尝试将这些组合键提交到内存。 他们会经常派上用场,您会感到惊讶。
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.
别名是常规命令的终端快捷方式。 您可以将它们添加到~/.zshrc
文件中,然后重新加载终端(使用source ~/.zshrc
),以使它们生效。
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'
.
一个myip
别名,它将您当前的公共IP地址打印到终端: alias myip='curl http://ipecho.net/plain; echo'
alias myip='curl http://ipecho.net/plain; echo'
。
A distro
alias to output information about your Linux distribution: alias distro='cat /etc/*-release'
.
用于输出有关Linux distro
信息的distro
别名: alias distro='cat /etc/*-release'
。
A reload
alias, as I can never seem to remember how to reload my terminal: alias reload='source ~/.zshrc'
.
reload
别名,因为我似乎永远都记不起如何重新加载终端: 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~
.
undo-git-reset
别名: alias undo-git-reset-head="git reset 'HEAD@{1}'"
。 这将恢复运行git reset HEAD~
的效果。
An alias to update package lists: alias sapu='sudo apt-get update'
.
用于更新软件包列表的alias sapu='sudo apt-get update'
: alias sapu='sudo apt-get update'
。
An alias to rerun the previous command with sudo
: alias ffs='sudo !!'
.
使用sudo
重新运行上一个命令的alias ffs='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.
因为我懒,我有别名y
到yarn
命令: alias y='yarn'
。 这意味着我可以克隆一个存储库,然后键入y以获取所有依赖项。 我从Scott Tolinski的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.
不是我使用的一个,但是这个别名在重新安装项目的依赖项之前会node_modules
文件夹并删除package-lock.json
文件: alias yolo='rm -rf node_modules/ && rm package-lock.json && yarn install'
。 正如你可能知道,YOLO代表Ÿ欧唯一一句大号 IVEØNCE。
An alias to open my .zshrc
file for editing: alias zshconfig='subl $HOME/.zshrc'
.
别名打开我的.zshrc
文件进行编辑: 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'
可以安装用于更新rbenv的Ruby版本列表的alias update-available-rubies='cd ~/.rbenv/plugins/ruby-build && git pull'
: 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'
.
在当前目录中启动服务器的别名(不需要npm软件包): 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'
.
您还可以创建别名以在浏览器中打开文档: 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
.
用于将命令输出传递到less
的全局别名: alias -g L='| less'
alias -g L='| less'
。 您可以像这样使用它: 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
.
全局别名,用于将命令的输出传递给grep
: alias -g G='| grep'
alias -g G='| grep'
。 您可以这样使用它: 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:
您还可以使用函数来创建别名。 以下代码(从此处获取 )创建了一个别名,用于添加,提交并将代码推送到GitHub:
bash function acp() { git add . git commit -m "$1" git push }
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.
有很多地方可以在线找到更多有关别名的想法。 例如, 此Hacker News讨论或有关Zsh的命令行生产力的帖子。
Oh My Zsh is a community-driven framework for managing your Zsh configuration and comes bundled with thousands of helpful functions, helpers, plugins and themes. If you’re going to make the Z shell your daily driver, you should really install Oh My Zsh.
噢,我的Zsh是一个社区驱动的框架,用于管理您的Zsh配置,并捆绑了成千上万的有用功能,帮助程序,插件和主题。 如果要将Z shell用作日常驱动程序,则应真正安装Oh My Zsh。
Here are fifteen useful things Oh My Zsh can do for you:
以下是15项有用的功能,噢,我的Zsh可以为您做:
The take
command will create a new directory and change into it. take my-project
replaces mkdir my-project && cd my-project
.
take
命令将创建一个新目录并将其更改。 take my-project
替换为mkdir my-project && cd my-project
。
zsh_stats
will give you a list of the top 20 commands and how many times they’ve been run.
zsh_stats
将为您列出前20条命令以及它们已运行了多少次。
Oh My Zsh simplifies navigating your file system. For example, ..
is an alias for cd ..
.
噢,我的Zsh简化了文件系统的导航。 例如, ..
是cd ..
的别名。
In the same way, ...
moves you up two directories, ....
moves you up three, and .....
moves you up four.
以同样的方式, ...
将您上移两个目录, ....
将您上移三个目录, .....
将您上移四个目录。
You can omit the cd
when navigating. Typing /
, for example, will take you straight to your filesystem root.
您可以在导航时省略cd
。 例如,输入/
会将您直接带到文件系统根目录。
Partial matching is also supported. For example, typing /h/j/De
and pressing TAB, then Return, takes me to /home/jim/Desktop/
.
还支持部分匹配。 例如,输入/h/j/De
并按TAB ,然后按Return键 ,将带我到/home/jim/Desktop/
。
rd
is an alias for rmdir
and md
is an alias for mkdir -p
.
rd
是rmdir
的别名, md
是mkdir -p
的别名。
You can type d
to list the last used directories from a terminal session.
您可以键入d
以列出终端会话中最后使用的目录。
You can then navigate to any of these using cd -n
, where n
is the directory number.
然后,您可以使用cd -n
导航至其中任何一个,其中n
是目录号。
Tab completion is another great feature. For example, typing ls -
and pressing TAB will list all of the command’s options, along with a helpful description of what they do. This also works for cap
, rake
, ssh
, and kill
.
制表符补全是另一个很棒的功能。 例如,键入ls -
并按TAB键将列出命令的所有选项,以及有关其功能的有用说明。 这也适用于cap
, rake
, ssh
和kill
。
Typing alias
lists all of your current aliases.
键入alias
列出您当前的所有别名。
With globbing (a Zsh feature), you can list files with a particular extension. For example, ls *.html
will list all HTML files in the current directory. To include subdirectories, change to: ls **/*.html
.
使用globbing(Zsh功能),可以列出具有特定扩展名的文件。 例如, ls *.html
将列出当前目录中的所有HTML文件。 要包含子目录,请更改为: ls **/*.html
。
Glob qualifiers allow you to select types of files by using flags. For example, ls -l **/*(.x)
will find all executable files in the current directory and all sub-directories.
Glob限定符使您可以使用标志来选择文件类型。 例如, ls -l **/*(.x)
将在当前目录和所有子目录中找到所有可执行文件。
You can search for files by date modified. For example, ls *(m-7)
will list all files modified within the last week.
您可以按修改日期搜索文件。 例如, ls *(m-7)
将列出上周修改的所有文件。
You can search for files by size. For example, ls *(Lm+1)
will find all files with a size larger than 1MB.
您可以按大小搜索文件。 例如, ls *(Lm+1)
将查找所有大于1MB的文件。
Oh My Zsh ships with a lot of plugins. You should look through these and invest some time learning those that will help your workflow.
哦,我的Zsh附带了许多插件 。 您应该仔细阅读这些内容,并花一些时间学习那些对您的工作流程有帮助的内容。
Here are three plugins I regularly use, that provide a ton of handy shortcuts and aliases.
这是我经常使用的三个插件,可提供大量便捷的快捷方式和别名。
The git plugin provides many aliases and several useful functions. Why not go through these and attempt to memorize your top ten? Here are the ones I use most often.
git插件提供了许多别名和一些有用的功能 。 为什么不经历这些并尝试记住前十名呢? 这是我最常使用的。
g
is a handy alias for git
. This means you can type things like g clone
instead of git clone
. Might only be two keystrokes, but they soon add up.
g
是git
的方便别名。 这意味着您可以输入g clone
而不是git clone
。 可能只有两次击键,但很快就会加起来。
gaa
is an alias for git add all
. I use this one all the time.
gaa
是git add all
的别名。 我一直在用这个。
gb
is an alias for git branch
, which will list all of the branches in the current repo and show you which one you’re on.
gb
是git branch
的别名,它将列出当前存储库中的所有分支,并向您显示您所在的分支。
gcb
is an alias for git checkout -b
, the command that allows you to create a new branch.
gcb
是git checkout -b
的别名,该命令使您可以创建新分支。
gcm
is an alias for git checkout master
. This returns you to the master branch.
gcm
是git checkout master
的别名。 这将使您返回到master分支。
gdca
is an alias for git diff --cached
. This allows you to diff any files you’ve staged for commit.
gdca
是git diff --cached
的别名。 这使您可以比较已准备提交的任何文件。
gf
is an alias for git fetch
.
gf
是git fetch
的别名。
gm
is an alias for git merge
.
gm
是git merge
的别名。
gp
is an alias for git push
. To sync a fork of a repo, you could do: gf upstream
, gm upstream/master
, followed by gp
.
gp
是git push
的别名。 要同步存储库的分叉,您可以执行以下操作: gf upstream
, gm upstream/master
,然后是gp
。
glog
is an alias for git log --oneline --decorate --graph
, which will give you a pretty git branch graph.
glog
是git log --oneline --decorate --graph
的别名,它将为您提供漂亮的git分支图。
The npm plugin provides completion as well a bunch of useful aliases.
npm插件提供了完成功能以及许多有用的别名。
npmg
is an alias for npm install --global
, which you can use to install dependencies globally on your system. For example, npmg nodemon
.
npmg
是npm install --global
的别名,您可以使用它在系统上全局安装依赖项。 例如, npmg nodemon
。
npmS
is an alias for npm install --save
, which you use to install dependencies and add them to the dependencies
section of your package.json
. Note that, as of npm 5.0.0, this is the default when running npm i
.
npmS
是npm install --save
的别名,可用于安装依赖项并将其添加到package.json
的“ dependencies
部分。 请注意,从npm 5.0.0开始 ,这是运行npm i
时的默认设置。
npmD
is an alias for npm install --save-dev
, which you use to install dependencies and add them to the devDependencies
section of your package.json
.
npmD
是npm install --save-dev
的别名,可用于安装依赖项并将其添加到package.json
的devDependencies
部分。
npmO
is an alias for npm outdated
, which will check the registry to see if any (or, specific) installed packages are currently outdated.
npmO
是npm outdated
的别名,它将检查注册表以查看当前是否已安装任何(或特定的)已安装软件包。
npmL
is an alias for npm list
, which will list installed packages.
npmL
是npm list
的别名,它将列出已安装的软件包。
npmL0
is an alias for npm list --depth=0
, which lists top-level packages. This is especially useful for seeing which modules are installed globally without flooding your terminal with a huge dependency tree: npmL0 -g
.
npmL0
是npm list --depth=0
的别名,该列表列出了顶级软件包。 这对于查看全局安装了哪些模块而不用巨大的依赖关系树npmL0 -g
淹没您的终端特别有用。
npmst
is an alias for npm run start
, an npm script often used to start an application.
npmst
是一个别名npm run start
,一个NPM脚本通常用来启动一个应用程序。
npmt
is an alias for npm run test
, which, as you might guess, is used to run your tests.
npmt
是npm run test
的别名,您可能会猜到它用于运行测试。
npmR
is an alias for npm run
. On its own, this command will list all of a project’s available npm scripts, along with a description of what they do. Used in conjunction with a script name, it will run that script, For example, npmR build
.
npmR
是npm run
的别名。 该命令将单独列出项目中所有可用的npm脚本,并说明它们的作用。 与脚本名称结合使用,它将运行该脚本,例如npmR build
。
npmI
is an alias for npm init
. This will ask you a bunch of questions, then create a package.json
based on your answers. Use the -y
flag to automate the process. For example, npmI -y
.
npmI
是npm init
的别名。 这将询问您很多问题,然后根据您的答案创建package.json
。 使用-y
标志自动执行该过程。 例如, npmI -y
。
This plugin adds completion for the Ruby on Rails framework and the Rake program, as well as some aliases for logs and environment variables.
这个插件增加了Ruby on Rails框架和Rake程序的完成 ,以及一些日志和环境变量的别名。
rc
is an alias for rails console
, which allows you to interact with your Rails app from the CLI.
rc
是rails console
的别名,它使您可以从CLI与Rails应用程序进行交互。
rdc
is an alias for rake db:create
, which (unless RAILS_ENV
is set) creates the development and test databases for your app.
rdc
是rake db:create
的别名,(除非设置RAILS_ENV
)为您的应用程序创建开发和测试数据库。
rdd
is an alias for rake db:drop
, which drops your app’s development and test databases.
rdd
是rake db:drop
的别名,它删除应用程序的开发和测试数据库。
rdm
is an alias for rake db:migrate
, which will run any pending database migrations.
rdm
是rake db:migrate
的别名,它将运行任何暂挂的数据库迁移。
rds
is an alias for rake db:seed
, which runs the db/seeds.rb
file to populate your development database with data.
rds
是rake db:seed
的别名,后者运行db/seeds.rb
文件以用数据填充开发数据库。
rgen
is an alias for rails generate
, which will generate boilerplate code. For example: rgen scaffold item name:string description:text
.
rgen
是rails generate
的别名,它将生成样板代码。 例如: rgen scaffold item name:string description:text
。
rgm
is an alias for rails generate migration
, which will generate a database migration. For example: rgm add_description_to_products description:string
.
rgm
是rails generate migration
的别名,它将生成数据库迁移。 例如: rgm add_description_to_products description:string
。
rr
is an alias for rake routes
, which list all of an app’s defined routes.
rr
是rake routes
的别名,它列出了应用程序所有已定义的路由。
rrg
is an alias for rake routes | grep
, which will allow you to list and filter the defined routes. For example, rrg user
.
rrg
是rake routes | grep
的别名rake routes | grep
rake routes | grep
,它将允许您列出和过滤定义的路由。 例如, rrg user
。
rs
is an alias for rails server
, which launches the Rails default web server.
rs
是rails server
的别名,它将启动Rails的默认Web服务器。
The main job of the plugins listed above is to provide aliases to often-used commands. Please be aware that there are lots more plugins out there that augment your shell with additional functionality.
上面列出的插件的主要工作是为常用命令提供别名。 请注意,还有更多的插件可以通过其他功能扩展您的Shell。
Here are four of my favorites:
这是我的四个最爱:
sudo allows you to easily prefix your current or previous commands with sudo
by pressing ESC twice.
sudo允许您通过按两次ESC轻松为当前或先前的命令添加sudo
前缀。
autosuggestions suggests commands as you type based on history and completions. If the suggestion is the one you’re looking for, press the → key to accept it. A real time saver!
当您键入命令时, autosuggestions会根据历史记录和完成情况建议命令。 如果建议是您要寻找的建议,请按→键接受。 实时节省!
command-not-found: if a command isn’t recognized in the $PATH
, this will use Ubuntu’s command-not-found package to find it or suggest spelling mistakes.
command-not-found :如果$PATH
未识别命令,则将使用Ubuntu的command-not-found包查找该命令或提示拼写错误。
z is a handy plugin that builds a list of your most frequent and recent folders (it calls these “frecent”) and allows you to jump to them with one command.
z是一个方便的插件 ,可构建您最常用和最近的文件夹的列表(它称为“最新”),并允许您使用一个命令跳转到它们。
And don’t forget, if you spend a lot of time in the terminal, it’s worth investing some effort in making it visually appealing. Luckily, Oh My Zsh ships with a whole bunch of themes for you to choose from. My pick of the bunch is Agnoster.
而且请不要忘记,如果您在终端上花费大量时间,则值得投入一些精力使其外观更具吸引力。 幸运的是,“噢,我的Zsh”附带了很多主题供您选择。 我最喜欢的是Agnoster 。
You can find out more about themes in my article 10 Zsh Tips & Tricks: Configuration, Customization & Usage.
您可以在我的文章10 Zsh技巧与窍门:配置,自定义和用法中找到有关主题的更多信息。
So there we have it: 75 Zsh commands, plugins, aliases and tools. I hope you’ve learned a trick or two along the way, and I encourage you to get out of your GUIs and into the terminal. It’s easier than it looks and a great way to boost your productivity.
因此,我们有了:75种Zsh命令,插件,别名和工具。 希望您在此过程中学到了一两个技巧,并鼓励您退出GUI并进入终端。 它比看起来容易,是提高生产力的好方法。
If I’ve missed your favorite plugin, or time-saving alias/command, let me know on Twitter.
如果我错过了您最喜欢的插件或省时的别名/命令,请在Twitter上告诉我。
Want to get even more out of your toolkit? Check out Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers from Wiley.
想要从您的工具包中获得更多收益? 查看来自Wiley的Visual Studio代码:Web开发人员的端到端编辑和调试工具 。
翻译自: https://www.sitepoint.com/zsh-commands-plugins-aliases-tools/
zsh 别名