使用 VSCODE 写代码还是很爽的,因为有非常充裕的插件,多种语言的支持,并解决了我远程写代码无法同时看到输出的痛点。这篇文章记录了我配置 VSCODE 的步骤以及遇到的问题与解决方式,一方面作为我的备忘,另一方面希望帮助到他人。
基本配置
-
安装R插件用于在VSCODE中识别R语言
[图片上传失败...(image-cfe7c7-1664976327070)]
-
R中安装以下R包
install.packages("languageserver") install.packages("httpgd")
-
VSCODE 中安装
radian
终端pip3 install -U radian
安装完输入`radian`可以进入radian终端说明安装成功
-
使用
radian
替换默认终端文件 => 首选项 => 设置
- 搜索r.rterm.windows, 输入radian.exe路径(如C:\Users\way\anaconda3\Scripts\radian.exe)
- 搜索r.br,勾选R: Bracketed Paste
- 搜索httpgd,勾选R › Plot: Use Httpgd
- 搜索shell wind,勾选git bash
快捷输入
1. 快捷键
-
alt + -
输入<-
-
alt + =
输入管道%>%
-
ctrl + enter
运行代码
{
"key": "alt+-",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus&&editorLangId=='r'",
"args": {
"snippet": " <- "
}
},
{
"key": "alt+=",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus&&editorLangId=='r'",
"args": {
"snippet": "%>%"
}
},
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText",
"when": "editorTextFocus && editorHasSelection"
},
2. 注释模板
通过输入anno**
就可以生成不同样式的注释,如下
[图片上传失败...(image-7a939c-1664976327070)]
配置方式:ctrl+shift+p
输入Snippets: Configure User Snippets
=> 新建全局代码段文件,粘入以下内容:
{
// Place your 全局 snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"Annotate steps":{
"prefix": "annoStep",
"body": ["###### $1"]
},
"Annotate header":{
"prefix": "annoHead",
"body": [
"# TODO: ${1}",
"#",
"# Author: liuwei",
"# Date:$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
"###############################################################################"
]},
"Annotate function":{
"prefix": "annoFun",
"body": [
"#' @description ${1}",
"#' @param ${2}",
"#' @param ${3}",
"#' @output ${4}",
"#' @example ${5}",
"#' @author ${6}"
]
},
"Annotate section":{
"prefix": "annoSection",
"body": [
"### ${1}",
"#' ${2}",
"##################$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE##################",
],
},
"Annotate large section":{
"prefix": "annoLarge",
"body": [
"########################################################",
"#' ${1}",
"#######################$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE#######################",
]
},
"Annotate mid section":{
"prefix": "annoMid",
"body": [
"### ${1}",
"#' ${2}",
"################################"
],
},
"Annotate small section":{
"prefix": "annoSmall",
"body": [
"#######",
"#' ${1}",
"#######"
]
},
"Annotate test section":{
"prefix": "annoTest",
"body": [
"########",
"$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
"########",
"#' -----------测试案例-----------",
"${1}",
"#' -----------结束测试-----------",
"#####################"
],
},
"Annotate install":{
"prefix": "annoInstall",
"body": [
"#######################安装过程#############################",
"#' ${1}",
"###########################$CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE#######################",
],
},
"Annotate modify":{
"prefix": "annoModify",
"body": [
"#### 修改日志 - $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE",
"#' ${1}",
"##",
],
},
}
远程服务器配置
安装完这个插件后,在远程服务器上的操作就如同在本地一般丝滑,如下:
[图片上传失败...(image-77905-1664976327070)]
搜索安装Remote - SSH
插件后,通过如下方式连接:
[图片上传失败...(image-d87f85-1664976327070)]
远程服务器错误解决
解决此问题需要三个包文件,可以访问此处下载:
链接:https://pan.baidu.com/s/1mQS1CAQHRTIoqWoCPWXbig?pwd=9rzi
提取码:9rzi
问题1:连接服务器报错:The remote host may not meet VS Code Server's prerequisites for glibc and libstdc++
解决方式参考:Using Visual Studio Code for C Programming on an Old Linux Remote Server,简述如下
1. 检查问题:
# 74b1f979648cc44d385a2286793c226e611f59e7 这串字符根据随机生成,请根据实际修改
$ cd $HOME/.vscode-server/bin/74b1f979648cc44d385a2286793c226e611f59e7
$ ./node # 出现以下输出说明C++版本不够
# ./node: /usr/lib64/libc.so.6: version `GLIBC_2.16' not found (required by ./node)
# ./node: /usr/lib64/libc.so.6: version `GLIBC_2.17' not found (required by ./node)
$ strings /lib64/libc.so.6 | grep GLIBC # 目前最高版本2.15,而最低要求是2.17
...
GLIBC_2.14
GLIBC_2.15
GLIBC_PRIVATE
2. 以非管理员身份安装这些库
2.1. 下载库文件:
- glibc (the GNU C standard library): glibc-2.17–307.el7.1.x86_64.rpm
- libstdc++ (C++ standard library): libstdc++-4.8.5–39.el7.x86_64.rpm
2.2. 建立伪root安装目录,把待安装文件放在里面
$ mkdir ~/local && cd ~/local
$ ll
total 4600
-rw-rw-r-- 1 liuwei2019 bioX 3817244 May 19 20:50 glibc-2.17-326.el7_9.x86_64.rpm
-rw-rw-r-- 1 liuwei2019 bioX 816216 Jul 23 11:26 libstdc++-4.8.5-44.el7.x86_64.rpm
-rw-rw-r-- 1 liuwei2019 bioX 72524 Nov 13 2018 patchelf-0.9-10.sdl7.x86_64.rpm
2.3. 解压抽取,创建类似于root用户的安装结构
$ rpm2cpio glibc-2.17-326.el7_9.x86_64.rpm | cpio -idmv
$ rpm2cpio rpm2cpio libstdc++-4.8.5-44.el7.x86_64.rpm | cpio -idmv
2.4. 检查glibc版本
$ find . -name "lib*.so.6"
$ strings ./lib64/libc.so.6 | grep GLIBC
...
GLIBC_2.16
GLIBC_2.17 # 满足需求
GLIBC_PRIVATE
$ strings ./usr/lib64/libstdc++.so.6 | grep GLIBCXX
...
GLIBCXX_3.4.18
GLIBCXX_3.4.19 # 满足需求
GLIBCXX_DEBUG_MESSAGE_LENGTH
2.5. 重新链接
现在需要告诉VSCode使用新安装的库,一个天真的想法是改变$LD_LIBRARY_PATH
环境变量检查local文件夹,但是这会导致其它的命令不可用并且导致你无法再连上服务器(不要尝试)。我们需要改变的是可执行文件的链接,此时需要一个包:patchelf-0.9–10.el7.x86_64.rpm
,这个包里面我们只需要保留patchelf
命令,因此将它解压到一个临时目录,再把这个命令拷过来:
$ mkdir ../tmp && cd ../tmp
$ rpm2cpio patchelf-0.9-10.sdl7.x86_64.rpm | cpio -idmv
./usr/bin/patchelf
./usr/share/doc/patchelf-0.9
./usr/share/doc/patchelf-0.9/README
./usr/share/licenses/patchelf-0.9
./usr/share/licenses/patchelf-0.9/COPYING
./usr/share/man/man1/patchelf.1.gz
333 blocks
$ mkdir -p ~/bin
$ cp usr/bin/patchelf ~/bin
$ cd ..
$ rm -rf tmp/ #删除临时目录
之后进入 VSCode 目录运行以下代码:
$ cd $HOME/.vscode-server/bin/74b1f979648cc44d385a2286793c226e611f59e7
$ ~/bin/patchelf --set-interpreter $HOME/local/lib64/ld-linux-x86-64.so.2 --set-rpath $HOME/local/usr/lib64/:$HOME/local/lib64 node
2.6. 检查是否成功
上面的命令没有输出的话可以认为没有错误,让我们检查一下:
$ ldd node
...
linux-vdso.so.1 (0x00007fff1ebff000)
libdl.so.2 => /home/liuwei2019/local/lib64/libdl.so.2 (0x00007f0a29af1000)
libstdc++.so.6 => /pub5/xiaoyun/BioSoftware/MATLAB/MATLAB_Compiler_Runtime/v714/sys/os/glnxa64/libstdc++.so.6 (0x00007f0a297eb000)
libm.so.6 => /home/liuwei2019/local/lib64/libm.so.6 (0x00007f0a294e9000)
libgcc_s.so.1 => /pub5/xiaoyun/BioSoftware/MATLAB/MATLAB_Compiler_Runtime/v714/sys/os/glnxa64/libgcc_s.so.1 (0x00007f0a292d2000)
libpthread.so.0 => /home/liuwei2019/local/lib64/libpthread.so.0 (0x00007f0a290b6000)
libc.so.6 => /usr/lib64/libc.so.6 (0x000000390ee00000)
/home/liuwei2019/local/lib64/ld-linux-x86-64.so.2 => /lib64/ld-linux-x86-64.so.2 (0x000000324d200000)
3. 大功告成,重新打开远程(虽然有警告,但我们已经可以成功连上服务器)
[图片上传失败...(image-56d05f-1664976327070)]
问题2:远程后 bash_profile
不加载
修改settings.json
,添加以下内容,参考
"terminal.integrated.profiles.linux": {
"bash": {
"path": "bash",
"args": [
"-l"
]
}
}
其它插件:
https://renkun.me/2022/03/06/my-recommendations-of-vs-code-extensions-for-r/