VSC 是一个很好实现了 editor + terminal 模式的文本编辑器:
-
editor 和 terminal 被整合到了同一个环境中, 通过 workbench.action.terminal.runSelectedText 命令把文本发送到 terminal 里;
-
通过快捷键方便地在二者间一步跳转,不需要像 nvim terminal 那样在 insert 模式下执行命令, 用 Ctrl-\ Ctrl-n 切换到 normal 模式才能转换到 editor;
-
terminal 直接使用操作系统的 terminal emulator 应用, 可以使用所有系统命令快捷键(例如 fzf 的命令行历史,autojump 的跳转等);
-
详细的 terminal 配置,提供了大量 terminal 相关的命令可以绑定快捷键, 详细列表在系统快捷键定义文件(Command Palette 里搜索
keyboard shortcuts
) 里搜索workbench.action.terminal.*
; -
基于文件的配置:全局配置和用户配置分离,主要的用户配置文件都在 ~/.config/Code/User 目录下,例如 keybindings.json 和 settings.json;
-
全局模糊匹配的 Command Palette (
Ctrl-Shift-p
), 很多使用频率不太高的命令不必定义快捷键也能通过键盘快速操作, 不需要通过鼠标点击菜单完成; -
与 neovim 深度整合(VSCodeVim/Vim 插件正是 neovim GUI 列表 中 VSC 的实现),vim mode 体验不错,很多 vim 插件也被移植到了 vsc-vim 上 (例如 easy-motion);
-
基于 electron 框架,可以方便地使用 JavaScript/TypeScript 开发插件;
大纲目录
小屏幕下,常开 outline 视图会压缩文件编辑和预览窗口,
这时如果需要查看文件目录,可以使用 Ctrl-b
打开/关闭 side bar,
或者 View > Show Breadcrumbs 也能比较方便地查看文件结构。
Install
Always install VSC with the deb file from its official website.
不要通过 flatpak 安装 VSC。flatpak 在沙箱中运行应用,应用环境与 用户环境隔离,所以适合运行微信等不依赖外界环境的应用,而 VSC 与系统终端深度整合, 通过 $SHELL 环境变量确定启动 shell 的种类和方式,而 flatpak sandbox 中 $SHELL 只能是 /bin/sh,无法改变。
P.S.: the configuration files above are in folder ~/.var/app/com.visualstudio.code/config/Code/User when installed with flatpak.
See vscodevim for more customization examples.
Install Vim extension in the Extensions bar (show with ctrl-shift-x, hide with ctrl-b).
Configuration
Add the following codes in ~/.config/Code/User/keybindings.json to enable Capslock-ESC swap and Ctrl-j to jump between editor and console:
[
{
"key": "capslock",
"command": "extension.vim_escape",
"when": "editorTextFocus && vim.active && !inDebugRepl"
},
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText"
},
{ "key": "alt+j", "command": "workbench.action.terminal.focus"},
{ "key": "alt+j", "command": "workbench.action.focusActiveEditorGroup", "when": "terminalFocus"}
]
The capslock
block above is a workaround for bug #23991.
When in i3wm, change alt+j to alt+m for avoiding shortcut confliction.
Add the following codes in ~/.config/Code/User/settings.json to customize proxy:
{
"http.proxy": "http://localhost:1080",
...
}
vim 相关配置
vim.handleKeys
的作用是避免 vim 拦截部分 Code 的快捷键,
例如 Ctrl-b
: toggle sidebar,
Ctrl-k z
: toggle Zen mode 等。
{
"vim.easymotion": true,
"vim.useSystemClipboard": true,
"vim.normalModeKeyBindingsNonRecursive": [
{
"before": [";"],
"after": [":"]
}
],
"vim.handleKeys": {
"<C-b>": false,
"<C-k>": false
},
...
}
Auto completion
推荐 All Autocomplete 插件,翻译 FPP 过程中使用它对中英文的自动补全都很不错。
Auto Switch of Input Method
在Linux系统上,使用 fcitx 输入法时,首先确定是否可以用 fcitx-remote -t
切换输入法,若能正确切换,在输入法关闭的情况下执行 fcitx-remote
,
得到的结果(应该是1)作为下面 "vim.autoSwitchInputMethod.defaultIM" 的值:
{
"vim.autoSwitchInputMethod.enable": true,
"vim.autoSwitchInputMethod.defaultIM": "1",
"vim.autoSwitchInputMethod.obtainIMCmd": "/usr/bin/fcitx-remote",
"vim.autoSwitchInputMethod.switchIMCmd": "/usr/bin/fcitx-remote -t {im}",
...
}
这样当使用 ESC 返回 normal 模式时,VSC 将自动关闭输入法。
另外 fcitx 还提供了 fcitx-remote -c
关闭输入法,
fcitx-remote -o
开启输入法,通过它们可以实现 vim 中自动切换输入法。
在 Windows 系统上使用 VSC 时,可以通过 im-select 实现输入法自动切换,首先下载 im-select.exe 64位版本到 d:/apps/im-select目录下, 然后在 C:\Users\lee_c\AppData\Roaming\Code\User\settings.json 文件中加入如下内容:
{
...
"vim.autoSwitchInputMethod.enable": true,
"vim.autoSwitchInputMethod.defaultIM": "1033",
"vim.autoSwitchInputMethod.obtainIMCmd": "d:/apps/im-select/im-select.exe",
"vim.autoSwitchInputMethod.switchIMCmd": "d:/apps/im-select/im-select.exe {im}"
}
这里 1033 是当关闭输入法时, 在 git bash (im-select 无法在 Windows cmd.exe 中运行)里执行 im-select 得到的返回值。 当中文输入法开启时,返回值为 2052.
详细方法参考 section "Input Method" in VSCodeVim.
Frequently Used Actions
-
Zoom in/out in editor: Ctrl +/-;
-
Toggle terminal: Ctrl-backslash;
-
Toggle side bar (file explorer and outline view): Ctrl-b;
-
Toggle Zen mode: Ctrl-k, z
-
Toggle line wrap: Alt-z
-
Open command palette: Ctrl-Shift-p, for example you can specify the Python interpreter via inputing
interpreter
in the command window; -
Open a folder (as the project root): Ctrl-k Ctrl-o, VSC create a .vscode folder in it, and save workspace settings (for example, Python Interpreter path) in .vscode/settings.json file.
-
Jump between opened files: Ctrl-p;
-
Jump between editors in the same group: Ctrl-tab;
-
Jump between editor groups: Ctrl-1/2/3/...;
-
Find keyboard shortcuts of a command: Ctrl-Shift-p >
open keyboard shortcuts file
. Then the file Default Keybindings is opened, search in it with the action name, for example,editorgroup\c
to find all command and corresponing shortcuts;
Markdown Preview
Install extension "Markdown All in One" for VSC. After opening a markdown file, split the window, and put it in a new editor group. Thus when you edit the markdown file in the left window, the preview in the right window will change accordingly.
Ref:
Define Macros
In haskell repl, you need put multiline codes between :{
and :}
.
So I defined the following macros to automate this workflow.
First install multi-command pulgin. Then add the following codes into settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.runHaskellCode",
"interval": 30,
"sequence": [
{
"command": "workbench.action.terminal.sendSequence",
"args": { "text": ":{\u000d" }
},
"workbench.action.terminal.runSelectedText",
{
"command": "workbench.action.terminal.sendSequence",
"args": { "text": ":}\u000d" }
}
]
}
]
Add the following codes in keybindings.json:
{
"key": "ctrl+enter",
"command": "workbench.action.terminal.runSelectedText",
"when": "editorTextFocus && editorLangId != haskell"
},
{
"key": "ctrl+enter",
"command": "multiCommand.runHaskellCode",
"when": "editorTextFocus && editorLangId == haskell"
},
Note \u000d
above means the Enter key, see section Send text from a keybinding in
Send text from a keybinding
for details.
There is also a macros plugin to build macros. But it cann't keep the command sequence correctly.
View documention on hover for Haskell
To show type information and documentation on mouse hover in VS Code, install HIE and Haskell Language Server:
git clone https://github.com/haskell/haskell-ide-engine --recursive
cd haskell-ide-engine && make build-all
This took 4~5 hours to finish (in the office of biot, 2018.11.7), with about 400MB contents downloaded and
some from s3.amazon.com. So you must run this command with proxy.
The final size of the folder haskell-ide-engine is about 6GB.
You can delete this folder after installation (there are several hie-*
executables
in the ~/.local/bin foler).
Then open VS Code, install the Haskell Language Server extension and reload.
Now you can see the documentations of any functions, types or variables with mouse hover.