iTerm2 shell集成有一些巧妙的技巧,比如它的it2copy
命令,即使我通过ssh登录到远程机器,它也会复制到本地剪贴板。
它可以用来运行任意shell命令吗?
例如,当我通过ssh登录时,我想执行一个命令,在本地机器上打开一个编辑器。VSCode可以使用以下命令打开远程目录:
code --remote ssh-remote+myserver /home/stuart/some-directory
我想通过远程机器上的ssh会话在本地触发该命令。
PS——我知道还有一种选择:创建一个(嵌套的(ssh连接返回到我的本地机器,通过ssh执行命令,而不是使用iTerm2的反向通道。但这有各种不利因素,因此产生了这个问题。
我还知道~/.ssh/config
中的PermitLocalCommand
选项,它允许我发送转义码(~C
(,然后发送本地命令(!code --remote ...
(。但我希望有一个可以在脚本或bash别名中使用的解决方案。
例如,如果it2local
存在,我会这样使用它:
alias code_here='it2local "code --remote ssh-remote+$(uname -n) $(pwd)"'
如果单用ssh就可以做到这一点,我很想听听。
正确的方法是通过iTerm2触发器,只要终端输出中出现特定模式,它就可以运行任意命令(以及其他选项(。
我上面描述的假设it2local
命令只需要将一些预定义的触发模式与您想要执行的命令一起回显到您的终端。
在我的情况下,我没有实现通用的it2local
命令。(也许我稍后会更新这个答案。(目前,我已经实现了一个为我的特定用例服务的脚本:用VSCode打开远程文件。我正在使用的代码如下所示。
#!/bin/sh
#
# This file contains the code and instructions to set up an iTerm2 "Trigger" from a
# remote ssh session that will open up VSCode on your local machine to edit a
# file on the remote server over ssh.
#
# Author: Stuart Berg
# https://github.com/stuarteberg
# bergs@janelia.hhmi.org
# https://stackoverflow.com/questions/61699447
# SETUP OVERVIEW
# --------------
# - Install the VS Code Remote Development Extension Pack
# - Ideally, setup passwordless ssh access to the remote machines you want to access
# - Place this script somewhere on your local machine (and make sure it's executable).
# - Copy the localcode() shell function below into your remote machine's .bashrc
# - Define the Trigger in iTerm2 as defined below.
#
# Notes:
# Docs for iTerm2 Triggers: https://iterm2.com/documentation-triggers.html
# Docs for VSCode Remote Extension: https://code.visualstudio.com/docs/remote/remote-overview
# - CLI: https://github.com/microsoft/vscode-remote-release/issues/585#issuecomment-536580102
# iTerm2 Preferences Setup
# ------------------------
#
# In your iTerm2 preferences, set up a Trigger (Profiles > Advanced > Triggers > Edit)
#
# Regular Expression: .*ITERM-TRIGGER-open-with-local-vscode-remote ([^ ]+) ([^ ]+) (([^ ]+ ?)+)
# Action: Run Command...
# Parameters: /path/to/this/script 1
#
# Tip: For additional feedback, try adding a duplicate entry with a "Post Notifcation" action.
# HOW TO TEST
# -----------
#
# NOTE: The new trigger will not be active for already-open terminal sessions.
# Open a new terminal after you add the trigger to your preferences.
#
# To test it, ssh into the remote machine, and try the 'localcode' function:
#
# localcode .
# localcode /some/dir
# localcode /some/file
# localcode /some/file remote-machine-name
#
# If something is going wrong, inspect /tmp/iterm-vscode-trigger.log
#
# Put this in your remote ~/.bashrc
#
# Set this to the name of your remote machine,
# which will be serving the files you want to edit.
# (In my case, the machine is named 'submit'.)
DEFAULT_REMOTE_MACHINE_FOR_VSCODE=submit
localcode() (
# Tell zsh to use bash-style arrays
setopt ksh_arrays 2> /dev/null || true
CMD=ITERM-TRIGGER-open-with-local-vscode-remote
MACHINE=${LOCALCODE_MACHINE-${DEFAULT_REMOTE_MACHINE_FOR_VSCODE}}
FILENAMES=( "$@" )
if [[ ${#FILENAMES[@]} == 0 ]]; then
FILENAMES=.
fi
if [[ ${#FILENAMES[@]} == 1 && -d ${FILENAMES[0]} ]]; then
FILENAMES[0]=$(cd ${FILENAMES[0]}; pwd)
FTYPE=directory
else
# Convert filenames to abspaths
for (( i=0; i < ${#FILENAMES[@]}; i++ )); do
FN=${FILENAMES[i]}
if [[ -f ${FN} ]]; then
DIRNAME=$(cd $(dirname ${FN}); pwd)
FILENAMES[i]=${DIRNAME}/$(basename ${FN})
FTYPE=file
else
1>&2 echo "Not a valid file: ${FN}"
exit 1
fi
done
fi
echo ${CMD} ${FTYPE} ${MACHINE} ${FILENAMES[@]}
)
export -f localcode
#
# Copy this whole file onto your local machine, or at least the following lines.
# Make sure it is executable (chmod +x /path/to/this/script)
#
trigger_vscode_remote_editing() (
# Tell zsh to use bash-style arrays
setopt ksh_arrays 2> /dev/null || true
# The git extension runs 'git status -z -u' on the remote machine,
# which takes a very long time if the remote directory is a git repo
# with a lot of untracked files.
# That can be fixed if you configure .gitignore appropriately,
# but for my purposes it's easier to just disable git support when editing remote files.
# If you want git support when using remote SSH, then comment out this line.
# See: https://github.com/microsoft/vscode-remote-release/issues/4073
VSCODE='/usr/local/bin/code'
VSCODE="${VSCODE} --disable-extension vscode.git --disable-extension vscode.github --disable-extension waderyan.gitblame"
LOGFILE=/tmp/iterm-vscode-trigger.log
FTYPE=$1
MACHINE=$2
FILEPATHS=( "$@" )
FILEPATHS=( "${FILEPATHS[@]:2}" )
TS="["$(date "+%Y-%m-%d %H:%M:%S")"]"
echo "${TS} Triggered: ""$@" >> ${LOGFILE}
# https://github.com/microsoft/vscode-remote-release/issues/585#issuecomment-536580102
if [[ "${FTYPE}" == "directory" ]]; then
CMD="${VSCODE} --remote ssh-remote+${MACHINE} ${FILEPATHS[@]}"
echo "${TS} ${CMD}" >> ${LOGFILE}
${CMD}
elif [[ "${FTYPE}" == "file" ]]; then
for FN in ${FILEPATHS[@]}; do
CMD="${VSCODE} --file-uri vscode-remote://ssh-remote+${MACHINE}${FN}"
echo "${TS} ${CMD}" >> ${LOGFILE}
${CMD}
done
else
echo "${TS} Error: Bad arguments." >> ${LOGFILE}
exit 1
fi
)
trigger_vscode_remote_editing "$@"
对于仍然想这样做的人:
我的解决方案受到@StuartBerg的启发。THX很多。
- 在iTerm2中,找到触发器:
iTerm2 -> Perenfences -> Profiles -> Advanced -> Triggers -> Edit
并按如下方式设置触发器(请记住在设置后重新启动iTerm2(:
# open a folder
- Regular Expression: ^open this folder in local vscode(s):(.+)
- Action: Run Command...
- Parameters: /path/to/your/vscode --folder-uri=vscode-remote://ssh-remote+${your remote ip}1
# open a file
- Regular Expression: ^open this file in local vscode(s):(.+)
- Action: Run Command...
- Parameters: /path/to/your/vscode --file-uri=vscode-remote://ssh-remote+${your remote ip}1
- 在远程服务器中设置命令别名,通常在linux的
~/.bashrc
中(code
别名的大多数行为与本地终端中的行为相同,只是不会打开服务器中未退出的任何文件(:
# pay attention to " and '
alias code='func(){
if [[ "$1" == "." ]]; then
CODE_PATH=$(pwd | tr -d "nr");
else
if [[ "$1" == /* ]]; then
CODE_PATH="$1";
else
CODE_PATH=$(pwd | tr -d "nr")/"$1";
fi;
fi;
if [[ -d $CODE_PATH ]]; then
echo open this folder in local vscode"("s")":$CODE_PATH;
else
if [[ -f $CODE_PATH ]]; then
echo open this file in local vscode"("s")":$CODE_PATH;
else
echo No such file or directory: $CODE_PATH;
fi;
fi;
};func'
- 使别名生效:
$ source ${path/to/your/alias/file}
- 在ssh终端中,将
cd
指向要在本地vscode中打开的目录(或使用以/
开头的绝对路径(,然后执行:
# open the current folder
$ code .
# output:
# open this folder in local vscode(s):/folder/you/want/to/open
# open a file or a folder in current directory
$ code ${filename or folder}
# output:
# open this file(folder) in local vscode(s):/path/you/want/to/open
# open an absolute path, i.e, any path that starts with `/`
$ code `/${absolute/path}`
如果您已经将ssh密钥设置为本地远程服务器,那么目录/path/you/want/to/open
将在本地vscode中打开。
PS:^
、(s)
和'('s')'
是故意添加的,目的是在配置文件和触发器Regular Expression
之间做一点区别,这样在打开别名配置文件时不会意外触发触发器。你可以随心所欲地修改它。