gvim常见操作使用手册

简介

不知道简介啥,鼠标坏了想用个不用鼠标的编辑器。

vim的四种模式

模式 说明
正常模式 默认,按 esc 进入,可复制粘贴、撤销重做
命令模式 正常模式下输入:或/进入,可进行保存、退出、搜索、运行
插入模式 按 i 进入,可进行输入编辑
可视模式 选中一块区域进行操作,按 v 进入

文件操作

命令 效果
:wq 保存并退出
:w 文件名 在当前路径下保存为指定文件
:q 退出
:q! 强制退出
:Ex 和 :Ve 打开当前文件所在的目录
:set ff dos和unix两种

移动

命令 效果
xw 光标向下移动x个单词,这里的x是一个数字
xb 光标向上移动x个单词,这里的x是一个数字
0 移动到行首

文本删除

命令(普通模式) 效果
dd 删除一行
de 从光标开始删除一个单词
dw 从光标开始删除一个单词(包括末尾空白)
dxw 删除x个单词,这里的x是一个数字
d$ 删除到行末
dxd 删除x行,这里的x是一个数字

复制粘贴

按v进入可视模式,通过hjkl选择区域后,按下:y即可复制,在普通模式按p即可粘贴。

跳转

命令 效果
gg 跳转到第一行
G 跳转到最后一行
xG 跳转到第x行
[[ 跳转到上一函数
% 跳转到匹配括号

编译

命令 效果
cpp test.cpp > test.i 将编译预处理结果(文本替换、宏展开以及删除注释)输入到test.i文件
g++ % -e % 同上
g++ -S % 使用指令编译成汇编代码
g++ tmp.cpp -o tmp -std=c++14 -static-libgcc -Wall -g3 编译

GVim配置文件

_vimrc

存放于安装目录的根目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin

set encoding=utf-8 fileencodings=utf-8 termencoding=utf-8 "自动识别utf8
source $VIMRUNTIME/delmenu.vim "解决右键乱码
source $VIMRUNTIME/menu.vim
"解决consle输出乱码
language messages zh_CN.utf-8

"设置默认保存位置
exec 'cd ' . fnameescape("C:\\Users\\Public\\Desktop")
set autochdir "自动识别当前打开文件路径
set autoread " 开启文件监视,外部修改会提示
set pythonthreedll="C:\Program Files\Python38\python38.dll "改成自己的版本
"set pythonthreehome="C:\Program Files\Python38"

set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction


filetype on " 打开文件类型检测功能,通过$vimRUNTIME/filetype.vim和$vimRUNTIME/scripts.vim来判断
filetype plugin on " 允许vim加载文件类型插件
filetype indent on " 为不同类型的文件定义不同的缩进格式
syntax on " 高亮
set cindent " C代码里需要的缩排
set smartindent
set autoindent " 自动缩进
set tabstop=4 " 修改 tab 字符的显示宽度
set shiftwidth=4 " 自动缩进所使用的空白长度
set softtabstop=4 " tab转换为多少个空格
"set expandtab " tab转换为空格
"set ts=4
"set sw=4
"set sts=4
"缩进
if $OS == 'Windows_NT'
set mouse=a
else
set mouse-=a " Linux鼠标使用
endif
color molokai
"colo desert
"主题
set number " 行号
set cursorline "所在行高亮
set guifont=Consolas:h15
"Linux: set guifont=Consolas\ h16
set backspace=2
set ruler " 显示标尺(光标位置)
"set bs=2
set clipboard=unnamed " 共享剪切板
set go= " 不要图形按钮
set nobackup " 不创建备份文件
set noswapfile " 不创建交换文件
set undofile " 保留历史撤销记录
" 保存 undo 历史,生成文件到该目录
set undodir=D:/ProgramData/Vim/.undo_history
"set backupdir=~/.vim/.backup//
"set directory=~/.vim/.swp//
"set undodir=~/.vim/.undo/
set spell spelllang=en_us " 拼写检查

" 括号补全
inoremap ( ()<esc>i
inoremap [ []<esc>i
inoremap { {}<esc>i
inoremap ' ''<esc>i
inoremap " ""<esc>i
" 插入模式按住ctrl的光标移动
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>

function! Compile()
exec "w"
if &filetype == 'python'
exec "!python %"
elseif &filetype == 'cpp'
exec "!g++ % -o %< -std=c++11 -static-libgcc -Wall -g3 -DDEBUG"
"exec "!g++ % -o %< -std=c++17 -static-libgcc -Wall -g3 -fexec-charset=GBK -finput-charset=UTF-8"
elseif &filetype == 'c'
exec "!g++ % -o %<"
elseif &filetype == 'kotlin'
exec "!kotlinc -d . %"
elseif &filetype == 'java'
exec "!javac -source 14 %"
else
exec "!start %"
endif
endfunction

function! Run()
if &filetype == 'kotlin'
exec "!kotlin %<Kt"
elseif &filetype == 'java'
exec "!java %<"
elseif &filetype == 'go'
exec "!go run %"
else
exec "!start %<"
endif
endfunction

function! Open()
exec "vsp %<.out"
exec "sp %<.in"
endfunction

":call libcallnr("vimtweak64.dll", "SetAlpha", 200)
au GUIEnter * call libcallnr("vimtweak64.dll", "SetAlpha", 185) "自动透明

syntax enable


map<F4> <Esc>:call Open() <CR>
imap<F4> <Esc>:call Open() <CR>
map<F9> <Esc>:call Compile() <CR>
imap<F9> <Esc>:call Compile() <CR>
map<F10> <Esc>:call Run() <CR>
imap<F10> <Esc>:call Run() <CR>
map<F11> <F9> <CR> <F10> <CR>
imap<F11> <F9> <CR> <F10> <CR>


" <CTRL>-w m : mark first window
" <CTRL>-w m : swap with that window
let s:markedWinNum = -1

function! MarkWindowSwap()
let s:markedWinNum = winnr()
endfunction

function! DoWindowSwap()
"Mark destination
let curNum = winnr()
let curBuf = bufnr( "%" )
exe s:markedWinNum . "wincmd w"
"Switch to source and shuffle dest->source
let markedBuf = bufnr( "%" )
"Hide and open so that we aren't prompted and keep history
exe 'hide buf' curBuf
"Switch to dest and shuffle source->dest
exe curNum . "wincmd w"
"Hide and open so that we aren't prompted and keep history
exe 'hide buf' markedBuf
endfunction
function! WindowSwapping()
if s:markedWinNum == -1
call MarkWindowSwap()
else
call DoWindowSwap()
let s:markedWinNum = -1
endif
endfunction
nnoremap <C-w>m :call WindowSwapping()<CR>


" 设置环境保存项
set sessionoptions="blank,buffers,globals,localoptions,tabpages,sesdir,folds,help,options,resize,winpos,winsize"
" 保存快捷键
" 定义快捷键的前缀,即<Leader>
let mapleader=";"
" 敲下;ss即可保存到路径下
map <leader>ss :mksession! D:/ProgramData/Vim/.undo_history/my.vim<cr> :wviminfo! D:/ProgramData/Vim/.undo_history/my.viminfo<cr>
" 恢复快捷键
map <leader>rs :source D:/ProgramData/Vim/.undo_history/my.vim<cr> :rviminfo D:/ProgramData/Vim/.undo_history/my.viminfo<cr>