Introduction#
In the command line, simply entering s
will have the same effect as running yarn start
or other commands, which can be a great experience, especially for frequently used commands.
Windows Alias Configuration#
Create a file named alias.bat
#
doskey s = yarn start
Configure the registry#
- Press
win + r
to open the Run window, enterregedit
to open the registry. - Then use
ctrl + f
to search forCommand Processor
. - Under
Command Processor
, create a new => String Value. - Set the value name as AutoRun, and the value data as the absolute path of
alias.bat
.
After the configuration is complete, restart the command line window to take effect. From then on, every time you need to adjust a command, you only need to modify the alias.bat
file.
Extensions#
Call multiple commands#
For example, a one-click push
operation:
doskey c = git add . $t git commit -m "update" $t git push
View configuration#
You can list all configured shortcuts by using doskey /macros
. If you find it troublesome, you can define a command
command to assist with memory:
doskey command = doskey /macros
Pass a parameter#
Suppose you set a shortcut command n
to node
and want to query the version number of node
by n -v
to pass a parameter. You can define it as follows:
doskey n = node $*
Pass multiple parameters#
In the example of passing a parameter above, $*
refers to all the parameters after n
. You can also use $1
, $2
, etc. to define parameters. For example, define a v
command to view the version numbers of both npm
and node
by v -v -v
. You can also pass different parameters as needed.
doskey v = npm $1 $t node $2
Linux/Mac Alias Configuration#
Open or create the ~/.zshrc
file#
alias s="nr start"
After the configuration is complete, use source ~/.zshrc
to update the file to take effect. From then on, every time you need to adjust a command, you only need to modify the ~/.zshrc
file. Remember to source
it after making changes.
Note:
- If it is a
bash
type terminal, it is the~/.bash_profile
file.- The alias configuration syntax in Mac is slightly stricter, for example, there should be no spaces before or after the "=" sign.
Extensions#
Call multiple commands#
alias v="npm -v && node -v"
Pass parameters#
alias
does not support passing parameters. To achieve this, you need to use a function to handle it. After configuring the following, batchParams -v -v
will print the versions of npm
and node
respectively.
batchParams() {
npm "$1"
node "$2"
}
A More Elegant Way#
When configuring alias in Windows, the registered commands will be executed again every time the terminal is started, which is not elegant enough. You can write a node cli
tool to improve it.
Custom command#
First, run a i
command to represent npm install
. Create a directory named cli
and initialize package.json
. Then define the following code:
{
// ...
"bin": {
"i": "./lib/install.js"
}
// ...
}
./lib/install.js
#!/usr/bin/env node
const { spawn } = require("child_process");
// Execute npm install, configure stdio: "inherit" to inherit and display the behavior of npm install, such as printed logs
spawn("npm", "install", { stdio: "inherit" });
Command enhancement#
Use @antfu/ni
to run:
spawn("ni", "", { stdio: "inherit" })
It will automatically recognize which package manager the project needs and install it.
Open Source Sharing#
If you often switch between Windows and Mac working environments, it would be convenient to write such a tool. I have written one myself, which currently has the following capabilities:
- i => ni (Install dependencies)
- b => nr build (Build)
- e => nr e2e (Run e2e tests)
- t => nr test (Run unit tests)
- s => nr start (or nr dev, prioritize dev)
- copen repository URL => Clone the repository and open it with vscode immediately
- cbranch => Remove all local branches that do not exist in remote repositories
Interested friends can fork it and have fun.