使用 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 這串字符根據(jù)隨機生成,請根據(jù)實際修改
$ cd $HOME/.vscode-server/bin/74b1f979648cc44d385a2286793c226e611f59e7
$ ./node # 出現(xiàn)以下輸出說明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. 解壓抽取,創(chuàng)建類似于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. 重新鏈接
現(xiàn)在需要告訴VSCode使用新安裝的庫,一個天真的想法是改變$LD_LIBRARY_PATH
環(huán)境變量檢查local文件夾,但是這會導致其它的命令不可用并且導致你無法再連上服務器(不要嘗試)。我們需要改變的是可執(zhí)行文件的鏈接,此時需要一個包: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/