[toc]
Vim emulation for VS code:个人觉得Vim+VSCode是绝配,Vim负责文本编辑,VSCode负责插件等高级功能
Vim的设计逻辑:a modal editor,多模态的编辑器
- Normal (ESC): for moving around a file and making edits,ESC很重要,我用Karabiner把MacOS的右Command键设成了ESC
- Insert (i): for inserting text
- Replace (R): for replacing text,无需删除,在文本上覆盖编辑;
r
替换字符 - Visual (plain (v), line (V), block (C-v)) mode: for selecting blocks of text
^V = Ctrl-v = <C-v>
- Command-line (:): for running a command
Vim基础
- 插入,按i进入,Esc退出
- 概念:Buffers, tabs, and windows
- buffer和window关系:一对多
- Command-line
- :q quit (close window)
- :w save (“write”)
- :wq save and quit =
ZZ
- :e {name of file} open file for editing 利用这一命令和:sp在文件间复制粘贴
:E
打开netrw文件浏览器- :ls show open buffers
- :help {topic} open help,
Ctrl-D
显示补全命令列表 :r
提取和合并文件;:r !ls
可读取存放外部命令输出
Movement:也称作“nouns”,因为指代chunks of text
- Basic movement:
hjkl
左下上右 - Words:
w
(下一个词开头),b
(本词或上一个词开头),e
(本词或下一个词末尾,常和a
搭配) - Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line) - Paragraph (原文没写):
{ and }
- Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen) - Scroll:
Ctrl-U (up), Ctrl-D (down)
- File:
gg
(beginning of file),G
(end of file),Ctrl-G
显示行号信息,数字+G
移动到某一行 - Line numbers:
:{number}
or{number}G
(line {number}) - Misc:
%
(corresponding item,比如括号匹配) - Find:
f{character}
,t{character}
,F{character}
,T{character}
- find/to forward/backward {character} on the current line
,
/;
for navigating matches
- Search:
/{regex}
向后搜索,n
/N
for navigating matches?{regex}
向前搜索- 退出查找高亮状态:
:nohl
:set ic
忽略大小写;:set hls is
; 选项前加no可关闭选项:set noic
/\cword
不区分大小写查找
Selection:Visual modes
- plain (v)
- line (V)
- block (Ctrl-v)
v
键提取后按: ... w ABC
可以保存文件
Edits: "Verbs"
i
进入insert模式o
/O
insert line below / aboved{motion}
delete {motion}- e.g.
dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
- e.g.
c{motion}
change {motion}- e.g.
cw
is change word - like
d{motion}
followed byi
- e.g.
x
delete character (equal dodl
)s
substitute character (equal toxi
)- visual mode + manipulation
- select text,
d
to delete it orc
to change it
- select text,
u
to undo,<C-r>
to redo,U
撤销行内命令- vim plugin: undotree[https://github.com/mbbill/undotree]
- how to persistently undo
y
to copy / “yank” (some other commands liked
also copy)p
to paste- Lots more to learn: e.g.
~
flips the case of a character - d, y, c均可双写,表示单行操作
A a S s
附加操作,相当于操作后移一格y
操作符命令会把文本复制到一个寄存器3中。然后可以用p
命令把它取回。因为y
是一个操作符命令,所以可以用yw
来复制一个word. 同样可以使用counting, 如用y2w
命令复制两个word,yy
命令复制一整行,Y
也是复制整行的内容,复制当前光标至行尾的命令是y$
{ }段首段尾
Counts:
3w
move 3 words forward5j
move 5 lines down7dw
delete 7 words
Modifiers: 接在nouns后面,i=inside,a=around,t=to
ci(
change the contents inside the current pair of parenthesesci[
change the contents inside the current pair of square bracketsda'
delete a single-quoted string, including the surrounding single quotesd2a
删除到a之前
./vimrc: 课程推荐config, instructors’ Vim configs (Anish, Jon (uses neovim), Jose)
plugin: 推荐网站,git clone到~/.vim/pack/vendor/start/
-
ctrlp.vim: fuzzy file finder
-
ack.vim: code search
-
nerdtree: file explorer
- 手动输
vim -u NONE -c "helptags ~/.vim/pack/my_plugs/start/nerdtree/doc" -c q
激活帮助插件,配置在了我的dotfiles里
- 手动输
-
vim-easymotion: magic motions
-
:ALEGoToDefinition
:ALEFindReferences
:ALEHover
:ALESymbolSearch
-
Shell:If you’re a Bash user, use
set -o vi
. If you use Zsh,bindkey -v
. For Fish,fish_vi_key_bindings
. Additionally, no matter what shell you use, you canexport EDITOR=vim
. This is the environment variable used to decide which editor is launched when a program wants to start an editor. For example,git
will use this editor for commit messages. -
Readline: Many programs use the GNU Readline library for their command-line interface.
- Readline supports (basic) Vim emulation too, which can be enabled by adding the following line to the
~/.inputrc
file:set editing-mode vi
- With this setting, for example, the Python REPL will support Vim bindings.
- Readline supports (basic) Vim emulation too, which can be enabled by adding the following line to the
-
Others:
There are even vim keybinding extensions for web browsers, some popular ones are Vimium for Google Chrome and Tridactyl for Firefox. You can even get Vim bindings in Jupyter notebooks.
-
Q: 如果文件太大打不开怎么办?
- A: 先
grep -nr $target $FILE
获取行号,然后vim $FILE +N
进入定位所在。
- A: 先
-
粘贴文本
:set paste
:set nopaste
-
缩进操作:
- 先
v
进入visual模式选定要缩进的内容,再shift + </>
进行整体的左右缩进 - 对齐缩进:
v
选中第二、三行,然后=
,与第一行对齐缩进
- 先
-
补全:连续按
Ctrl-P
buffer操作::ls,:b num, :bn(下一个), :bp(前一个), :b#(上次的buffer)
window操作: :sp / :vsp
split window,C-w + hjkl
切换
tab操作:gt
切换tab
跳转操作:
-
Ctrl-O/I
进入更旧/新的位置 -
Marks - In vim, you can set a mark doing
m<X>
for some letterX
. You can then go back to that mark doing'<X>
. This let’s you quickly navigate to specific locations within a file or even across files.
查找替换:
-
:%s/foo/bar/g
- replace foo with bar globally in file
%
表示修改全文件而不是第一个匹配串,/g
表示全行/全文件匹配,/gc
会提示每个匹配串是否替换:#,#s/...
表示对行号之间的内容操作
-
:%s/\[.*\](\(.*\))/\1/g
- replace named Markdown links with plain URLs
命令操作:
:g/pattern/command
,对匹配行执行命令.
复制操作- 外部命令:
:!
Advanced Text Objects - Text objects like searches can also be composed with vim commands. E.g. d/<pattern>
will delete to the next match of said pattern or cgn
will change the next occurrence of the last searched string.
-
q{character}
to start recording a macro in register{character}
-
q
to stop recording -
@{character}
replays the macro -
Macro execution stops on error
-
{number}@{character}
executes a macro {number} times -
Macros can be recursive
- first clear the macro with
q{character}q
- record the macro, with
@{character}
to invoke the macro recursively (will be a no-op until recording is complete)
- first clear the macro with
-
Example: convert xml to json (file)
-
Array of objects with keys “name” / “email”
-
Use a Python program?
-
Use sed / regexes
g/people/d
%s/<person>/{/g
%s/<name>\(.*\)<\/name>/"name": "\1",/g
- …
-
Vim commands / macros
-
Gdd
,ggdd
delete first and last lines -
Macro to format a single element (register
e
)- Go to line with
<name>
qe^r"f>s": "<ESC>f<C"<ESC>q
- Go to line with
-
Macro to format a person
- Go to line with
<person>
qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
- Go to line with
-
Macro to format a person and go to the next person
- Go to line with
<person>
qq@pjq
- Go to line with
-
Execute macro until end of file
999@q
-
Manually remove last
,
and add[
and]
delimiters
-
-
vimtutor
is a tutorial that comes installed with Vim- Vim Adventures is a game to learn Vim
- Vim Tips Wiki
- Vim Advent Calendar has various Vim tips
- Vim Golf is code golf, but where the programming language is Vim’s UI
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim (book)
- 文件栏:
command + shift + E
- 快速修复警告:
command + .
- 随处搜索:两次shift
-
查文件:
ctrl + P
,@追加符号,冒号追加行 -
头文件和源文件切换:
option + O
- 搜索switch获知快捷键
-
折叠函数:
- 折叠:
Command K + 1/2/3/4
(按住command) - unfold:
Command K + J
- 折叠:
-
跳转前一次/后一次光标位置:
ctrl + -
/shift + ctrl + -
-
Format:
Option + Shift + F
-
历史打开文件切换:
ctrl + TAB
-
ctrl + command + P
- Settings
-
打开终端:ctrl + `
参考「dotfiles:README」
- Yapf
pip install yapf
Option + Shift +F
自动格式化代码- .vscode/settings.json
{
"python.linting.enabled": true,
"python.linting.pylintPath": "pylint",
"editor.formatOnSave": false,
"python.formatting.provider": "yapf", // or "black" here
"python.linting.pylintEnabled": false,
"python.formatting.yapfArgs": [
"--style={based_on_style: google, indent_width: 2}" // column_limit: 80
]
}
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-13.0.1/clang+llvm-13.0.1-x86_64-linux-gnu-ubuntu-18.04.tar.xz
tar xvf clang+llvm-13.0.1-x86_64-linux-gnu-ubuntu-18.04.tar.xz
sudo ln -s xxx/bin/clang /usr/bin/clang-13.0
- clang-format: https://clang.llvm.org/docs/ClangFormat.html
- style-options: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
clang-format -style=llvm|Google -dump-config > .clang-format
# 格式化main.cpp, 结果直接写到main.cpp
clang-format -i main.cpp
# 支持对指定行格式化,格式化main.cpp的第1,2行
clang-format -lines=1:2 main.cpp
- clang-tidy: https://clang.llvm.org/extra/clang-tidy/
- clangd插件:https://clangd.llvm.org/installation
sudo apt install clangd-13
- command-p # 跳转符号
---
Checks: >
-*,
bugprone-*,
clang-analyzer-*,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-pro-type-cstyle-cast,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-type-union-access,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-special-member-functions,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
hicpp-*,
-hicpp-avoid-c-arrays,
-hicpp-use-auto,
-hicpp-vararg,
-hicpp-no-array-decay,
-hicpp-signed-bitwise,
-hicpp-special-member-functions,
modernize-*,
-modernize-use-trailing-return-type,
-modernize-avoid-c-arrays,
-modernize-pass-by-value,
-modernize-avoid-bind,
-modernize-use-auto,
performance-*,
-performance-no-int-to-ptr,
portability-*,
readability-*,
-readability-magic-numbers,
-readability-redundant-access-specifiers,
-readability-convert-member-functions-to-static,
-readability-implicit-bool-conversion,
-readability-identifier-length
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
InheritParentConfig: true
User: gycherish
CheckOptions:
- { key: readability-identifier-naming.AbstractClassPrefix, value: I }
- { key: readability-identifier-naming.AbstractClassCase, value: CamelCase }
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.ClassMemberCase, value: camelBack }
- { key: readability-identifier-naming.ClassMemberPrefix, value: m_ }
- { key: readability-identifier-naming.FunctionCase, value: camelBack }
- { key: readability-identifier-naming.EnumCase, value: CamelCase }
- { key: readability-identifier-naming.EnumConstantCase, value: CamelCase }
- { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE }
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
- { key: readability-identifier-naming.ProtectedMemberCase, value: camelBack }
- { key: readability-identifier-naming.ProtectedMemberPrefix, value: m_ }
- { key: readability-identifier-naming.PrivateMemberCase, value: camelBack }
- { key: readability-identifier-naming.PrivateMemberPrefix, value: m_ }
- { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
- { key: readability-identifier-naming.VariableCase, value: camelBack }
...
-
VSCode with GDB
- sourceFileMap 解决找不到源文件的问题
gdb --directory
{
"version": "0.2.0",
"configurations": [
{ //出现在vscode界面上的名字
"name": "(gdb) 启动",
// debugger类型 这是gdb
"type": "cppdbg",
"request": "launch",
//可执行文件位置
"program": "/home/huangruiteng/../workspace/program",
"args": ["--arg1=abc", "--directory=/home/huangruiteng/.../workspace"],
//是否进入后暂停
"stopAtEntry": false,
//工作目录
"cwd": "/home/huangruiteng/.../workspace",
"environment": [],
//是否使用外部终端
"externalConsole": false,
// dubug之前要运行的任务
//"preLaunchTask": "Build Fortran",
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"sourceFileMap": {
"/.../bazel-buildfarm/default/operations/.../abc.cpp": "/home/huangruiteng/.../workspace/.../abc.cpp"
}
// debug程序位置 gdb的位置
"miDebuggerPath": "/usr/bin/gdb",
},
]
}
{
"python.linting.enabled": true,
"python.linting.pylintPath": "pylint",
"editor.formatOnSave": false,
"python.formatting.provider": "yapf", // or "black" here
"python.linting.pylintEnabled": false,
"python.formatting.yapfArgs": [
"--style={based_on_style: google, indent_width: 2}" // column_limit: 80
],
"terminal.integrated.scrollback": 100000,
}
- 配置 compile_commands.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/../**"
],
"defines": [],
"compilerPath": "/usr/bin/clang-13.0",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "linux-clang-x64",
"compileCommands": "xxx/build/compile_commands.json",
"compilerArgs": [
"-O3"
],
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}/../a.cc",
"${fileDirname}/../b.cc",
"${fileDirname}/../c.cc",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lpthread"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: /usr/bin/g++"
}
],
"version": "2.0.0"
}
- remote ssh can't connect - Server installation process already in progress
- remove the hardlink
/home/sma/.vscode-server/bin/78a4c91400152c0f27ba4d363eb56d2835f9903a/vscode-remote-lock.sma.78a4c91400152c0f27ba4d363eb56d2835f9903a'
- remove the hardlink
-
Ctrl + M 括号匹配
-
Shift + Ctrl + M 选中该级括号内容
- command + / 切换编辑模式
- shift + command + L 打开边栏
- control + command + 1/2/3 切换边栏类型