终端下光标移动到文件路径上,⌘+click可以触发执行某个命令,比如点击一个项目文件夹,触发WebStorm,但是有时我希望不同的文件类型,触发不同的动作,这样就需要自定义脚本进行拓展。

解决方案

实现方案如下,因为个人Shell渣渣,为了方便,实际打开IDElogic使用JS编写,只是在shell中执行nodejs

贴下完整代码

iterm2-trigger.js

const [, , file] = process.argv;
const { execSync } = require('child_process');
const fs = require('fs');

const commandMap = new Map();

/**
* 设置各种文件的默认打开程序
* key为条件,value为执行命令,缺省使用默认打开程序
*/
commandMap.set(
() => fs.lstatSync(file).isDirectory(),
`/usr/local/bin/webstorm ${file}`
);

(function () {
let commandStr = `open ${file}`;
for (const fn of commandMap.keys()) {
if (fn(file)) {
commandStr = commandMap.get(fn);
}
}
execSync(commandStr);
})();

iterm2-trigger.sh

#!/usr/bin/env bash
DIR="$(dirname $0)"

/usr/local/bin/node "$DIR/iterm2-trigger.js" $1
exit 0;

iTerm2下进入Preferences=>Profiles=>local Profile=>Advanced=>Semantic History,配置如下

$HOME/bin/iterm2-trigger.sh \1

当然如果只想所有文件都走WebStorm或某程序打开,直接如下配置即可

/usr/local/bin/webstorm \1

写在最后

有了这个设定脚本,就可以灵活控制文件点击的动作了。

--

--

--

Develop with pleasure!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alan He

Alan He

Develop with pleasure!

More from Medium

Insist On Your Right To Breathe In Clean, Fresh Air — Especially In City Buildings!

Configure HTTPS SSL certificate on your AWS instance for Free

Gloom Guild: The Importance of Tackling Depression and Peer Pressure

ATEEZ: 3 Years Strong And Putting On A Show Like Seasoned Performers