221 字
1 分钟
macOS 使用 Vim 打开文件
macOS 使用 Vim 打开文件
原理,Automator.app + AppleScript
步骤
- 打开
Automator.app
- 创建新项目
- 选择
Application
- 搜索
Run AppleScript
- 编写脚本
- 可以把其中的
"Terminal"
改为你喜欢的终端 - 大概意思是,循环从输入中获取文件路径,如果文件可写,直接打开,否则使用
sudo
打开on run {input, parameters}tell application "Terminal"activateset firstTab to truerepeat with aFile in inputset filename to POSIX path of aFileset quotedFilename to quoted form of filenameset cmd to "if [ -w \"$FILENAME\" ]; then vim \"$FILENAME\"; else sudo vim \"$FILENAME\"; fi"if firstTab thendo script "FILENAME=" & quotedFilename & "; " & cmdset firstTab to falseelsedo script "FILENAME=" & quotedFilename & "; " & cmd in (do script "")end ifend repeatend tellend run
- 保存,选择你喜欢的名称
- 右键你想用 Vim 打开的文件,选择
Get Info
->Open With
-> 选择你刚刚保存的应用 -> 点击Change All
- Enjoy it!
macOS 使用 Vim 打开文件
https://blog.lpkt.cn/posts/macos-vim-openfile/