- windows capslock 修改映射为left-Ctrl
- insert & visual mode
- setting
- cursor
- operator pending
- insert mode
- viusual mode
- command
- view
- edit
- file
- completion
- macro
- file explore
- config
- vim-plug
windows capslock 修改映射为left-Ctrl
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1D,00,3A,00,00,00,00,00
保存为 .reg 文件 执行即可
insert & visual mode
i I a A o O s S c C
setting
:hls highlight search 高亮搜索结果 配合 / ? 的字符串搜索
:noh no highlight search 搜索结束后取消高亮显示
:set incsearch 增量搜索 边搜索边高亮
:h option-list 查看vim所有设置选项
:help ( 查询(的用法
:set nu 显示行号
:syntax on 语法高亮
:set autoindent 自动缩进 百度查看vim的几种自动缩进方式
:colorscheme 显示当前主题配色 默认default
:colorscheme ctrl+d 显示所有配色 然后可以输入想要的主题配色
google : vim colorscheme 可以下载并安装其他的配色 如hybrid
cursor
hjkl top down left right
H/M/L screen head/middle/last
gg/G file head/tail
w/W next word/WORD b/B previous word/WORD e/E end word/WORD
f/F· finc char (backward)
t/T· till char (backward)
;/, repeat/reverse
/ ?· find (backward)
n/N next/previous find
(/) begin/end sentence
{/} begin/end parag
*/# next/previous ident
+/- next/previous line
0/_ or ^ hard/soft bol
$/g_ hard/soft eol
% ' | ` 还不知道怎么使用
ctrl + f/b move to page down/up
ctrl + e/y move to line down/up
zz/zt/zb scroll cursor to middle/top/bottom
ctrl + o last time cursor stoped(on record normal mode cursor movement)
:range s/{pattern}/{string}/[flags] pattern替换为string
range: 10,20 表示10-20行 %表示当前文件全文
pattern : 正则
string : 替换文本
flags :
g(global) 全局范围执行
c(confirm) 每次替换进行确认提示
n(number) 查询匹配次数而不替换
eg:
:% s/foo/bar/g 当前文件文本替换 (把foo替换为bar)
:% s/self/this/g :1,6 s/self//n 统计self出现次数 :% s/\<quack\>/jiao/g do_quack不会被匹配到 man.quack()会被匹配到
operator pending
w 以非空白符为边界 W 以空白符为边界
s 以.为边界
p 以回车换行为边界
{"register}{num}<command>{modifier}{text object}
register 不指定使用默认的无名寄存器, 也可指定 "a-z 特定的寄存器
command d c y v
modifier a(around) i(inner) t(till)
text object
w s p brackt(() [] {} <> ' ") eb hl 0/$(不需要modifier)
{char}(只能配合t使用)
vim-go插件扩展了文本对象 支持f(function)
< > = unindent indent autoindent
g~ gu gU toggle/lowercase/uppercase
! external filter
gc comment/uncomment (vim-commentary plug)
insert mode
寄存器
默认无名寄存器
0 yank reg
= expression reg
a-z 普通寄存器
+ 系统剪贴板寄存器 %当前文件名 .上次插入的文本
:reg 查看指定寄存器的内容,默认查看无名寄存器的内容
:echo has('clipboard') 查看是否支持系统剪贴板
:set clipboard=unnamed 把系统剪贴板设置为 无名寄存器
ga look up character's numeric code at current cursor position
<C-h> Delete back one character (backspace)
<C-w> Delete back one word
<C-u> Delete back to start of line
<Esc> Switch to Normal mode
<C-[> Switch to Normal mode
<C-o> Switch to Insert Normal mode
<C-r> {reg} paste reg's content
<C-r><C-p> {reg} paste reg's content literally (ignore textwidth autoindent which lead to unwanted line breaks or extra indentation)
<C-v>{123} Insert character by decimal code
<C-v>u{1234} Insert character by hexadecimal code
<C-v>{nondigit} Insert nondigit literally
<C-k>{char1}{char2} Insert character represented by {char1}{char2} digraph
R/r replace mode
gR/gr virtual replace mode (treats the tab character as though it consisted of spaces)
viusual mode
v Enable character-wise Visual mode
V Enable line-wise Visual mode
<C-v> Enable block-wise Visual mode
gv Reselect the last visual selection
<C-g> toggle select mode and view mode (in view mode)
command
K looks up the man page for the word under the cursor
J joins the current and next lines together
view
vim filename1 filename2 -O 左右两个分屏打开两个文件
:vs(vertical split) {filename} 竖直分屏 如果不加filename则当前文件分两屏 使用:q退出分屏
:sp(split) {filename} 水平分屏 或者<ctrl + w>s/v 进行分割
多文件操作:
:e 文件名 文件存在则打开 不存在则创建一个(文件名可通过tab补全)
buffer:每打开一个文件就打开一个buffer
:ls 列举所有buffer
:b {num} 跳转到第num个buffer :bpre :bnext :bfirst :blast :b buffer_name(一般是文件名 使用Tab补全)
window:
<ctrl + w>s/v :sp :vs {filename} 进行window分割, 当前窗口永远在左上方
<ctrl + w>w 多个窗口间循环切换
<ctrl + w>hjkl 切换到←↓↑→窗口
<ctrl + w>HJKL 当前窗口和←↓↑→窗口 交换位置
<ctrl + w>= 所有窗口等高等宽 <ctrl + w>_/| 最大化活动窗口的高度/宽度 {N}<ctrl + w>_/| 活动窗口的高度/宽度设为N行
:e 文件名 指定文件在当前窗口打开
tab:
:tabnew {filename} 在新tab打开文件
<ctrl + w>T 当前window移到新tab
:tabc 关闭当前标签页及所有window
:tabo 关闭所有非活动标签页
:tabn {N} {N}gt 切换到指定编号tab
:tabn gt 切换到下一个tab
:tabp gT 切换到上一个tab
edit
{num}ctrl + a/x add/subtract num on next occurance number
set nrformats= 把07当做10进制 而非默认的8进制
p 粘贴
u 撤销 ctrl + r 重做
x 剪切当前字符 {num}x
xp 交换两个相连的字符
R 在当前字符进入插入模式且覆写
r{char} 当前字符替换为指定字符 {num}r
一般情况下 :set clipboard=unnamed 就可在vim中直接粘贴系统剪贴板的内容
或者 +p 粘贴系统剪贴板中的内容
如果在服务器上 无法使用系统剪贴板 则只能使用 ctrl + v 进行粘贴 只能粘贴前 :set pasete 粘贴后 :set nopaste 进行粘贴
file
:w :q :q! :e :h :new
ZZ :wq
ZQ :q!
gf open file whose name is under cursor
gi(光标到最近编辑的地方)
completion
※ctrl + n / ctrl + p 单词补全
ctrl + x , ctrl + n 当前缓冲区关键字
ctrl + x , ctrl + i 包含文件关键字
ctrl + x , ctrl + ] 标签文件关键字
ctrl + x , ctrl + k 字典查找
ctrl + x , ctrl + l 整行补全
※ctrl + x , ctrl + f 文件名补全(当前文件所在目录所有文件名)
ctrl + x , ctrl + a 全能补全(omni)
※ctrl + x , ctrl + o 代码补全,需要开启文件类型检查,安装插件
:filetype on 开启文件类型检查
:set filetype 查看当前文件类型的编程语言
google :vim input current path 查看获取当前文件路径的命令
macro
q{register} 录制 默认使用无名寄存器 再次按q 结束录制
@{register} 回放
例子: 多行url 前后加上双引号
录制:qaI"[esc]A"[esc]q
回放:VG选中剩下所有行 :normal @a
file explore
:Explore - opens netrw in the current window
:Sexplore - opens netrw in a horizontal split
:Vexplore - opens netrw in a vertical split
i cycle through the view types (thin, long, wide and tree)
config
vim配置文件路径:
Linux/Unix : vim ~/.vimrc
Windows : vim $MYVIMRC 通过环境变量指定配置文件
:version
:echo $VIM
:echo $HOME
:echo $VIMRUNTIME
配置文件组成:
设置::set nu行号 :colorscheme hybrid配色 等
映射: noremap <leader> w:w<cr> 保存文件
自定义 vimscrirpt
插件配置
重新进入vim 或 :source ~/.vimrc 让新的vimrc文件生效
github 的 vim-go 的vimrc文件
" 常用设置
" 显示行号
set number
" 配色
colorscheme hybrid
" 按F2进入粘贴模式
set pastetoggle=<F2>
" 高亮搜索结果
set hlsearch
" 设置折叠方式
set foldmethod=indent
" 常用映射
" 设置leader键 常用空格或逗号 默认是 / \ ?
let mapleader=','
let g:mapleader=','
" 使用jj进入normal模式 `^代表上次insert模式鼠标停留的位置 通过:help `^查看具体
inoremap jj <Esc>`^
" 使用leader+w 在插入模式直接保存 <cr>是回车
inoremap <leader>w <Esc>:w<cr>
noremap <leader>w :w<cr>
" 切换buffer
noremap <silent> [b :bprevious<CR>
noremap <silent> [n :bnext<CR>
" use ctrl+h/j/k/l switch window
noremap <C-h> <C-w>h
noremap <C-j> <C-w>j
noremap <C-k> <C-w>k
noremap <C-l> <C-w>l
" sudo to write
cnoremap w!! w !sudo tee % >/dev/null
" json格式化
com! FormatJSON %!python3 -m json.tool
" 插件设置
call plug#begin('~/.vim/plugged')
" 安装插件只需要把 github地址放在这里重启后执行 :PlugInstall即可
Plug 'mhinz/vim-startify'
Plug 'scrooloose/nerdtree'
call plug#end()
映射:
com!代表 command模式
nmap/vmap/imap 分别指定normal/visual/insert模式下的映射
map命令的递归风险
:nmap - dd
:nmap \ -
按下\ 会删除一行 (进行了递归)
多个插件如果对 同一个按键映射了不同的行为, 其中一个会失效
noremap/vnoremap/inoremap 非递归映射,任何时候都使用非递归映射
使用:unmap - 来取消指定映射
:map - x 使用-删除字符
:map <space> viw 按下空格的时候选中整个单词
:map <c-d> dd 使用ctrl+d 来剪切一行内容
:vmap \ U 在visual模式下的文本转大写(一般情况下 在visual模式下 选中文本 按u/U 进行大小写控制)
:imap <c-d> <Esc>ddi 在insert模式下 删除一行
vim-plug
yum install curl
yum install git
安装vim-plug : https://github.com/junegunn/vim-plug (里面有各个系统不同安装方法)
安装插件:
找到插件的vim-plug引入方式
修改.vimrc 添加插件名称
重启vim 或 source ~/.vimrc
执行 :PlugInstall
:qa 退出所有窗口
搜索插件:
google vim xxxx github
https://vimawesome.com/
开源的 .vimrc 里使用的vim插件 自己去搜索
vim-startify (启动界面)
https://github.com/mhinz/vim-startify (里面有vim-plug方式的安装方法)
使用: 直接执行 vim 不指定文件名,有最近文件记录
vim-airline(状态栏美化)
https://github.com/vim-airline/vim-airline
indentline(增加代码缩进线条 python尤其需要)
https://github.com/yggdroot/indentline
vim-hybrid(配色)
https://github.com/w0ng/vim-hybrid
使用: :colorscheme hybrid 添加到 .vimrc 文件中
来源:CSDN
作者:金山打字通
链接:https://blog.csdn.net/qq_27441757/article/details/103746852