cause
The business lines at work are more complicated. Each business line corresponds to a project. It is often encountered that multiple requirements are developed in parallel or troubleshooting in multiple projects, and it is necessary to frequently switch between different projects.
When I come into contact with various new tools every day, I will try it out by myself. Sometimes, half of the research may be interrupted by other things and forgotten.
So I wrote a simple gadget to meet my needs:
- Can record the work area directory, add and delete
- You can directly open the workspace for editing through VS Code
Introduction to gadgets
This small tool has been used for more than a year, and it meets my own needs in most scenarios and also improves development efficiency. The gadget is written in nodejs, and inquire.js is used for interactive prompts. You can select by up and down keys, and press Enter to open it with VS Code. Use effect:
Other commands (recent can be abbreviated as: r):
- recent add + dirname (relative address or absolute address)
- recent del + dirname
- recent ls view all workspace data that has been added
- recent ls + str filter the workspace data according to the input content
- recent + str matches and opens according to the input
Specific use
Add the code command to the environment variable
In VS Code, use the shortcut key Command/ + shift + P to open the command line panel and execute the shell command to install the code.
After the installation is complete, you can open the file or folder in VS Code code .
link recent to global command
Because the function is relatively simple, it is not packaged into an npm package, and the code is placed on GitHub: tools
Execute after clone:
npm install && npm link
You can recent
(or r
for short), and call the code command to open the workspace in VS Code.
other
What does the installed code command do?
# 查看code命令地址
which code
# 查看软连接指向
ls -a /usr/local/bin/code
After finding the specific file, it was found to be a shell script:
#!/usr/bin/env bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
function realpath() { python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "$0"; }
CONTENTS="$(dirname "$(dirname "$(dirname "$(dirname "$(realpath "$0")")")")")"
ELECTRON="$CONTENTS/MacOS/Electron"
CLI="$CONTENTS/Resources/app/out/cli.js"
ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" "$@"
exit $?
Checked it under MacOS:
$ELECTRON
is /Applications/Visual Studio Code.app/Contents/MacOS/Electron
$CLI
is /Applications/Visual Studio Code.app/Contents/Resources/app/out/cli.js
$@
is the received folder or file address
open command
Under MacOS, we can use open -a to specify the application and pass parameters:
# 使用Typora打开文件或文件夹
open -a Typora README.md
# 使用Chrome打开文件或文件夹
open -a open -a Google\ Chrome cat.png
open -a open -a Google\ Chrome .
By default, the open command will open the file using the default application. If the file is in the form of a URL, the file will be opened as a URL:
# 目录默认使用Finder打开
open .
# html会用默认浏览器打开
open index.htmls
Through the -a
parameter, you can specify the application to open the file or directory, and other supported parameters can be viewed through open -h
.
Create a shortcut command for the Open command
Through the Open command, we can use the specified program to open the specified file or directory, but the open command is more cumbersome to open, and you need to enter the complete program name.
If you want to typora
, we can create a shortcut command for Open.
Node.js version:
#!/usr/bin/env node
// typora.js
const { spawn } = require('child_process')
const params = process.argv.slice(2)
spawn('open', ['-a', 'Typora', ...params]);
Create soft connection
Create soft connection directly
# 添加可执行权限 chmod +x ./recent.js # 链接到全局 ln -s 源文件绝对路径 本地环境变量 ln -s /User/xxx/typora.js /usr/local/bin/typora
Or put it in a separate directory and specify in package.json
{ "bin":{ "typora":"./typora.js", "t":"./typora.js" } }
The directory executes link, it will automatically add executable permissions and add soft links to local environment variables
npm link
typora
in the command line to open the specified file/folder through Typora. Similarly, we can create any shortcut command to open the software we want to use.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。