Instrument: vim
vimrc的配置:
colorscheme default filetype on filetype plugin on filetype indent on syntax enable syntax on set ai set autochdir set autoread set cindent " set cursorcolumn set cursorline set encoding=utf-8 set ff=unix set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 set history=100 set ignorecase set incsearch set laststatus=2 set list set listchars=tab:»■,trail:■ " set mouse=a set nobackup set nocompatible set noerrorbells set nohlsearch set noswapfile set number set pastetoggle=<F2> set relativenumber set ruler " set selection=exclusive " set selectmode=mouse,key set shiftwidth=4 set showmode set showcmd set showmatch set smartcase set smartindent set softtabstop=4 " set spell spelllang=en_us set termencoding=utf-8 set t_Co=256 set tabstop=4 " undofile if !isdirectory($HOME."/.vim") call mkdir($HOME."/.vim", "" , 0770) endif if !isdirectory($HOME."/.vim/undo") call mkdir($HOME."/.vim/undo", "" , 0700) endif set undofile set undodir=~/.vim/undo " undofile set vb t_vb=
set viminfo+=n~/.vim/viminfo " set visualbell set wildmenu set wildmode=longest:list,full set wrap set autoindent set smarttab set expandtab " No Auto Comment augroup Format-Options " autocmd! " autocmd BufEnter * setlocal formatoptions-=c formatoptions-=r formatoptions-=o " This can be done as well instead of the previous line, for setting formatoptions as you choose: " autocmd BufEnter * setlocal formatoptions=crqn2l1j autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o augroup END au FileType c,cpp setlocal comments-=:// comments+=f:// " Support yaml autocmd FileType yaml setlocal tabstop=2 shiftwidth=2 cursorcolumn expandtab autoindent smartindent smarttab autocmd FileType yml setlocal tabstop=2 shiftwidth=2 cursorcolumn expandtab autoindent smartindent smarttab
No Auto Comment:
r Automatically insert the current comment leader after hitting <Enter> in Insert mode. c Auto-wrap comments using textwidth, inserting the current comment leader automatically. o Automatically insert the current comment leader after hitting 'o' or 'O' in Normal mode. :help formatoptions?
缩进:
syntax on | syntax enable: :syntax enable
What this command actually does is to execute the command (实际上:syntax enable相当于执行下面这个命令) :source $VIMRUNTIME/syntax/syntax.vim If the VIM environment variable is not set, Vim will try to find the path in another way (see $VIMRUNTIME). Usually this works just (如果:syntax enable命令无效,可能是$VIMRUNTIME变量没有设置好) fine. If it doesn't, try setting the VIM environment variable to the directory where the Vim stuff is located. For example, if your syntax files are in the "/usr/vim/vim50/syntax" directory, set $VIMRUNTIME to "/usr/vim/vim50". You must do this in the shell, before starting Vim. :syn-on :syntax-on The ":syntax enable" command will keep your current color settings. This (syntax enable会保留当前的颜色设置) allows using ":highlight" commands to set your preferred colors before or after using this command. If you want Vim to overrule your settings with the (syntax on将覆盖以前所做的颜色配置) defaults, use: :syntax on
创建undodir
mkdir -pv ~/.vim/undo
重新从磁盘载入文件:
1 2 | :e :e! #放弃当前修改,强制重新载入 |
光标:
- CTRL + o:调整到上一次光标所在的行号上,即往后跳转
- CTRL + i :调整到jump list中当前记录的下一个记录的行号上,即往前调整
shortcut:
查看 set 配置选项
:set filetype?
:set fileencoding?
:help > 获取帮助
查看 vim 内置变量
:echo $VIMRUNTIME
ctrl+v,shift+<|>
hi string|commnet|type|number|constant|statement ctermfg=[0-7]
[7,+∞) white
0 black
1 red
2 green
3 yellow
4 dark blue
5 红紫色
6 blue
常用指令:
- 以空格开头的行插入注释
1 1,10s/^[[:blank:]]\+/#&/ 2 1,10s/^\s\+/#&/ 3 4 [[:blank:]]可用[[:space:]]代替
- 删除空行
g/^$/d g/^[[:blank:]]*$/d g/^\s*$/d
后面两种可删除带有空格或tab的空行
- 注释所有非注释行
%s/^[^#]/#/g 错误:会将首字节替换为# %s/^[^#]/#&/g
- 替换所有^#为空行
%s/^#.*//g
替换成
.,+2s/^\([^\/]*\s\+\)[^\/]\+\//\1/
主要用到了后向引用\1,可以把/换成其他符号
.,+2s#^\([^/]*[ ]\{1\}\)[^/]\+/#\1#
- check where your backup files are being written
:se backup? backupdir? backupext?
默认bakcup选项关闭,backupdir为当前目录, bacupext 扩展名 为 ~ ,在set backup后生效
-
格式化代码
删除空行的空格, 空行的空格必定全部是空格%s@^\s*$@@g
删除行尾多余空格
%s@\s*$@@g
删除所有行首空格
%s@^\s*@@g
翻页技巧:
- ctrl+f (forward) ctrl+b (backward) 整页翻页
- ctrl+d (down) ctrl+u (up) 半页
- ctrl+e ctrl+y
- zz 让光标所杂的行居屏幕中央
zt 让光标所杂的行居屏幕最上一行 t=top
zb 让光标所杂的行居屏幕最下一行 b=bottom
shiftwidth tabstop softtabstop:
shiftwidth(缩写sw) - (语法自动缩进或按“>”“<”手动缩进)时,缩进的空白个数。
tabstop(缩写ts) - 每个定位字符HT(0x9)在文件中代表的空白个数。VIM文档提示修改缺省值8会导致在很多场合格式不正确(比如打印时)。 Setting 'tabstop' to any other value than 8 can make your file appear wrong in many places (e.g., when printing it).
softtabstop(缩写sts) - 不更改tabstop值还要满足用户使用习惯,可借助此参数。
vim中每次按键盘上的,会按照softtabstop设定的空白个数在屏幕上定位。如果未设置expandtab,当设置的softtabstop值大于tabstop时,会优先使用定位字符HT(0x9),余下的才使用空格(0x20)。
例如ts=8,sts=12,则按下一次Tab键时会产生一个定位字符HT(0x9)+4个空格(0x20)。
按键产生的空白个数受已有字符影响。比如sts=8,在新行中第1列按tab键,增加8个空白,这时候光标在第9列,这时输入一个字符a,再按tab,则只增加7个空位了。
expandtab - 是否将tab转换为空格(主要是防止不同的用户不同的tab设置导致文档缩进格式发生变化)
如果set expandtab,则定位字符HT(0x9)都转换为tabstop指定数量的空格(0x20)。
即使这种情况下如果按或退格键,仍然以定位字符方式而不用每个空格来处理。但删除键x以单个空格处理。
shiftwidth和tabstop不一样的话,你会发现程序比较难看的。
这时候,softtabstop就起作用了。可以从vim的说明中看到,一旦设置了softtabstop的值时,你按下tab键,插入的是空格和tab制表符的混合,具体如何混合取决于你设定的
softtabstop,举个例子,如果设定softtabstop=8, 那么按下tab键,插入的就是正常的一个制表符;如果设定 softtabstop=16,那么插入的就是两个制表符;如果softtabstop=12,那么插入的就是一个制表符加上4个空格;如果softtabstop
=4呢?那么一开始,插入的就是4个空格,此时一旦你再按下一次tab,这次的四个空格就会和上次的四个空格组合起来变成一个制表符。换句话说,softtabstop是“逢8空格进1制表符”,前提是你tabstop=8。
这时候,softtabstop就起作用了。可以从vim的说明中看到,一旦设置了softtabstop的值时,你按下tab键,插入的是空格和tab制表符的混合,具体如何混合取决于你设定的
softtabstop,举个例子,如果设定softtabstop=8, 那么按下tab键,插入的就是正常的一个制表符;如果设定 softtabstop=16,那么插入的就是两个制表符;如果softtabstop=12,那么插入的就是一个制表符加上4个空格;如果softtabstop
=4呢?那么一开始,插入的就是4个空格,此时一旦你再按下一次tab,这次的四个空格就会和上次的四个空格组合起来变成一个制表符。换句话说,softtabstop是“逢8空格进1制表符”,前提是你tabstop=8。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律