如何让windows terminal像VScode中嵌入的tcommandline一样使用code命令

前言

VS Code 远程连接服务器后,可以通过code命令实现打开文件,文件夹,打开新窗口等,这些功能对于一个不擅长Vim的人非常实用。

比如当我通过commandline进入到了指定目录,当我想查看目录中的某个文件内容时,可以通过cat / vim来实现,但它们都或多或少存在可读性及易用性的问题。
而若我们通过code命令打开文件,则可以通过本地的vs code查看该文件,很方便。

然而,由于vs code自带的commandline嵌入在客户端里面,使用起来不如一个独立的terminal方便,因此实际上平时导航目录,查看文件,多还是另外再开一些terminal去实现的。

但这些terminal, 如 windows terminal 并不能在连接远程后通过code命令打开vs code 客户端查看文件或者打开文件夹。

因此,本文通过总结google上的一些搜索结果,找到一条可以用windows terminal实现code命令的方法。

准备工作

想要通过code命令将远程vscode server与本地vscode client联动起来,还需要先通过ssh让彼此连接上,也就是在vs code中使用remote ssh连接远程主机。

本文提供一个可以通过terminal连接远程主机的思路:

1
2
code --remote ssh-remote+remote_server /code/my_project
# e.g. code --remote ssh-remote+<user>@<server>:<port>

reference : https://code.visualstudio.com/docs/remote/troubleshooting#_connect-to-a-remote-host-from-the-terminal

实现code命令

code脚本
  1. vim code.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash

# Zsh to Bash conversion notes:
# 1. Replaced zsh-specific glob modifiers (*oc[1]N) with ls/stat for timestamp sorting
# 2. Changed local variable declarations to use standard Bash syntax
# 3. Replaced array indexing and pattern matching with Bash equivalents
# 4. Added nullglob behavior manually using conditional checks
# 5. Used parameter expansion instead of zsh-specific syntax

# Find the latest VSCode server node process by modification time
vscode_servers=("$HOME"/.vscode-server/cli/servers/*/server/node)
if [[ ${#vscode_servers[@]} -eq 0 ]]; then
# Also check the legacy .vscodeserver directory if .vscode-server is empty
vscode_servers=("$HOME"/.vscodeserver/cli/servers/*/server/node)
if [[ ${#vscode_servers[@]} -eq 0 ]]; then
echo "VSCode remote server directory not found"
exit 1
fi
fi

# Find the most recently modified server directory
latest_server=""
for server in "${vscode_servers[@]}"; do
if [[ -f $server ]]; then
if [[ -z $latest_server ]] || [[ $server -nt $latest_server ]]; then
latest_server=$server
fi
fi
done

if [[ -z $latest_server ]]; then
echo "VSCode remote server process not found"
exit 1
fi

node_process=$latest_server

# Find the running server process
our_process=""
while IFS= read -r line; do
if [[ $line == *"$node_process"* && $line == *"start-server"* ]]; then
our_process=$line
break
fi
done < <(ps ux)

if [[ -z $our_process ]]; then
echo "Running VSCode server not found"
exit 1
fi

# Extract PID from the process info
our_pid=$(echo "$our_process" | awk '{print $2}')
if [[ -z $our_pid ]]; then
echo "PID for VSCode server could not be found"
exit 1
fi

# Find the socket associated with the PID
our_socket=$(ss -lx -p -s | grep "pid=$our_pid" | grep "\.sock" | head -n1)
if [[ -z $our_socket ]]; then
echo "VSCode IPC socket not found"
exit 1
fi

# Extract socket path from output
socket_path=$(echo "$our_socket" | awk '{print $5}')
if [[ -z $socket_path ]]; then
echo "VSCode IPC socket path could not be extracted"
exit 1
fi

# Trim the "/node" string from the node_process path
base_process=${node_process%"/node"}

# Execute with the appropriate environment variable
VSCODE_IPC_HOOK_CLI="$socket_path" "${base_process}/bin/remote-cli/code" "$@"
添加到bashrc
1
2
3
4
5
# 1. 设为可执行文件
chmod +x code.sh
# 2. 添加别名并写入bashrc
vim ~/.bashrc
alias code=/path/to/code.sh
测试
1
2
code -v
code -h

如何使用?

  1. 确保有一个vs code client连接上了目标主机
  2. 在terminal直接使用code命令,可以打开新的窗口,也可以查看文件

后记

再记录一些 code 常用命令

1
2
3
4
code -r . #在当前目录打开,并重用窗口
code -n . #在当前目录打开,并使用新的窗口
code file #查看文件
code -d file1 file2 #比较两个文件的差异