插入模式
i : 在光标前插入
I : 在行首插入
a : 在光标后插入
A : 在行尾插入
o : 向下新插入一行,在行首插入
O : 向上新插入一行,在行首插入
命令模式(Esc)
移动光标
命令 | 作用 |
---|---|
h——j——k——l | 左移——下移——上移——右移 |
M | 光标移动到中间行 |
L | 光标移动到最后一行行首 |
数字G——例如:3G | 光标移动到第三行 |
w——b | 向后移动一个单词——向前移动一个单词 |
{ —— } | 向上移动一段——向下移动一段 |
ctrl+d | 向下翻半屏 |
ctrl+u | 向上翻半屏 |
ctrl+f | 向下翻一屏 |
ctrl+b | 向上翻一屏 |
gg——G | 光标移动到文件开头——光标移动到文件末尾 |
删除(剪切)
命令 | 作用 |
---|---|
x——X | 删除光标后一个字符——删除光标前一个字符 |
dd——3dd | 删除光标所在行——删除光标开始的往下3行 |
D | 删除包含光标在内,光标后的,本行所有内容 |
d0 | 删除本行光标前的所有内容,不包含光标 |
dw | 往后删除一个单词 |
撤销命令
u : 一步一步的撤销
ctrl+r : 反撤销
重复上一次操作
. : 重复执行上一次操作的命令
文本行移动
>> : 文本行向右移动
<< : 文本行向左移动
复制粘贴
yy : 复制当行
3yy : 从光标起向下复制3行
p : 在光标所在位置向下开辟新一行,粘贴
可视模式
v : 按住之后,按字符移动,选中文本
V : 按住之后,按行移动,选中文本
替换命令
r : 替换当前字符
R : 替换当前行光标后的字符
全部替换:
例如:把文本中所有的abc替换为123
- %s/abc/123/g
例如:把文本中第一行到第十行的abc替换为123- 1, 10 s/abc/123/g
查找
例如:
/hello : 查找hello
n : 查看找到的下一处
N : 查看找到的上一处
行末模式
命令 | 作用 |
---|---|
: | 进入行末模式 |
w | 保存 |
q | 退出 |
q! | 不管提示是否保存,强制退出 |
x | 等同于wq,保存并退出 |
我的配置文件
" /usr/share/vim/vimrc 配置文件的路径,下面才是配置文件
" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by
" the call to :runtime you can find below. If you wish to change any of those
" settings, you should do it in this file (/etc/vim/vimrc), since debian.vim
" will be overwritten everytime an upgrade of the vim packages is performed.
" It is recommended to make changes after sourcing debian.vim since it alters
" the value of the 'compatible' option.
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
runtime! debian.vim
" Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc.
" This happens after /etc/vim/vimrc(.local) are loaded, so it will override
" any settings in these files.
" If you don't want that to happen, uncomment the below line to prevent
" defaults.vim from being loaded.
" let g:skip_defaults_vim = 1
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
set compatible
" Vim5 and later versions support syntax highlighting. Uncommenting the next
" line enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
" set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
"if has("autocmd")
" au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif
" Uncomment the following to have Vim load indentation rules and plugins
" according to the detected filetype.
"if has("autocmd")
" filetype plugin indent on
"endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
set showcmd " Show (partial) command in status line.
set showmatch " Show matching brackets.
set ignorecase " Do case insensitive matching
set smartcase " Do smart case matching
set incsearch " Incremental search
set autowrite " Automatically save before commands like :next and :make
set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes)
" Source a global configuration file if available
if filereadable("/etc/vim/vimrc.local")
source /etc/vim/vimrc.local
endif
set nu " 显示行号
set ai
synta on
set tabstop=4
set ignorecase
syntax on " 自动语法高亮
set hlsearch " 搜索时高亮显示被找到的文本
set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4
以上是vim的一些基础操作,曾今我想直接用vim作为我编程的主力IDE,我也折腾过vim的扩展,自动补全,目录栏,不错,vim很强大,但是,不得不说,vim已经是上古神器了,比我的年龄都大了,还有一个年轻的vim版本,叫neovim( https://neovim.io ),感兴趣的可以去看一下。从以上配置文件可以看出,我的配置基本就等同于没有配置,没错,折腾过后的我,现在用着VSCodium( https://vscodium.com )写着python,写着C,用IDEA写着Java。vim我已经不用了吗?不是的,我还用着,但是我已经不再折腾它的配置,以上简简单单的配置,显示行号,鼠标点击,高亮,已经足够我用了,毕竟我现在也就改一改配置文件时用一下vim。再说一下,VSCodium,是vscode的完全开源版本,去除了vscode里微软监控的代码,相对来说更安全,但使用起来毫无差别,一样的插件库,一样的强大,我很推荐使用它。但vim一样要会,小白眼里的B格神器,怎能不学呢?O(∩_∩)O哈哈~。。。。。最后说一句,折腾是好事,要保持。