Vim学习笔记-基础篇

简介: Vim学习笔记-基础篇

vim是vi改进版

windows 体验版 gvim

基本命令

$ vim # normal模式

:q(quit)  # 退出

# 进入编辑insert模式(6种)
i(insert)            # 光标前插入
a(append)            # 光标后插入
o(open a line below) # 行下插入

I insert before line # 行首 
A append after line  # 行尾 
O open a line above  # 行上插入

ESC 回到normal模式
:wq  # 保存退出write quit

vim4种模式

1、Normal普通模式(浏览)

Esc插入模式回到普通模式

普通模式下进行各种命令操作和移动

大部分情况下,使用的是浏览


2、Insert插入模式(编辑)

a/i/o 光标进入插入模式

A/I/O 行进入插入模式

3、Command命令模式(操作)

Normal模式下:进入命令模式

:q退出 :w保存

:vs(vertical split)垂直左右分屏 :sp(split) 水平上下分屏

:set nu 设置行号

:% s/java/python/g 全部替换java->python

:syntax on 语法高亮


4、Visual可视模式(选择)

Nomal模式下进入Visual模式

v选择字符

V选择行

ctrl+v块状选择


插入模式小技巧

ctrl + h 删除上一个字符

ctrl + w 删除上一个单词

ctrl + u 删除当前行


终端:

ctrl + a 移动到行首

ctrl + e 移动到行尾


切换到Normal模式:

Esc、ctrl + c、 ctrl + [


gi 回到刚才插入位置并进入插入状态

快速移动

Normal模式下


1、字符移动

h左 j下 k上 l右

     ^

k
< h l >
j
v

2、单词移动

w/W 移动到下一个word/WORD开头

e/E 下一个word/WORD尾

b/B 回到上一个word/WORD开头,backword


word 非空白符分割的单词

WORD 以空白符分割的单词


3、行间搜索移动

f{char} 移动到char字符上

t{char} 移动到char前一个字符上 until

第一次没搜到,可以继续搜该行

分号; 下一个

逗号, 上一个

F反向搜索


4、水平移动

0 移动到行首第一个字符

^ 移动到第一个非空白字符

$ 移动到行尾</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">g_ 移动到行尾非空白字符</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px"><br /></span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">5、垂直移动</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">括号() 句子间移动</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">查看帮助 :help (</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">{} 段落之间移动</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px"><br /></span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">6、屏幕移动</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">H head 屏幕开头</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">M middle 中间</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">L lower 结尾</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">zz 把屏幕置为中间&#xff0c;当前行放中间</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px"><br /></span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">7、页面移动</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">gg 文件开头</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">G 文件结尾</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">ctrl &#43; o 快速返回</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px"><br /></span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">ctrl &#43; u upword 上翻半页</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">ctrl &#43; f forword 下翻半页</span></p><h1 style="padding:7px 0px;margin:0px;font-weight:700;font-size:28px;line-height:36px">增删改查</h1><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">1、增加</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">Normal模式 a/i/o A/I/O</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px"><br /></span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">2、删除</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">Normal模式</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">x 删除一个字符</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">4x 删除4个字符</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><br /></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">d(delete)</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">daw 删除单词和周围空格&#xff08;delete around word&#xff09;</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">diw 删除单词&#xff08;dw&#xff09;</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">dd 删除行</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">dt{char} 删除直到</p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px">d$ 删除到结尾

d0 删除到开头

2dd 删除两行


u undo 撤销操作

ctrl + r 恢复撤销


3、修改

Normal模式下

r replace 替换一个字符

eg: 光标下g ra g->a

c change

cw 删除单词进入插入模式 change word

ct{char} 删除到字符,进入插入模式

s substitute 删除并进入插入模式 eg: 4s 删除4个字符进入插入模式


R 不断替换多个字符

S 删除整行进行插入

C 删除整行进行插入


4、查询

/ 前向搜索

? 反向搜索

n/N 下一个或者上一个

*/# 当前单词的前向和后向匹配


搜索结果高亮 :set hls (high light search)

:set incsearch

搜索替换

substitute 支持正则

:[range] s[ubstitute]/{pattern}/{string}/flags

range 范围

eg:

10,20 表示10-20行

% 表示全部

pattern 替换模式

string 替换后文本

flags 替换标志位

g global 全局替换

c confirm 确认

n number 查询匹配次数而不替换


eg:

:% s/self/this/g  # 替换 self->this
:1,6 s/self//n # 查询 计算有1-6行有多少个self
:% s/\&lt;name\&gt;/Name/g # 精确匹配单词

多文件操作

Buffer 打开一个文件的缓冲区

Window 可视化分割区域

Tab 组织窗口为一个工作区


1、Buffer

:ls 列举缓冲区

:b n 跳转到第n个缓冲区

:bpre :bnext :bfirst :blast

:b buffer_name tab补全


:e filename 打开文件


2、Window

一个缓冲区可以分割为多个窗口

每个窗口也可打开不同缓冲区

窗口可以无限分割


(1)窗口分割

+ s 水平分割

+ v 垂直分割

:sp [filename] 水平分割(打开文件)

:vs [filename] 垂直分割(打开文件)


(2)窗口切换

+ w 循环切换

+ h 切换到左边

+ j 切换到下边

+ k 切换到上边

+ l 切换到右边


(3)窗口移动

+ H 移动到左边

+ J 移动到下边

+ K 移动到上边

+ L 移动到右边


(4)重排窗口

:h window-size

+ = 所有窗口等宽等高

+ 最大化活动窗口高度

+ | 最大化活动窗口宽度

n + + 把活动窗口的高度设为n 行

n + + | 把活动窗口的宽度设为n 行


3、Tab标签页

一系列窗口的容器:h tabpage

:tabnew {filename} 新标签中打开

:tabe[dit] {filename} 新标签中打开

+ T 当前窗口移动到一个新标签页

:tabc[lose] 关闭当前标签页及其中的所有窗口

:tabo[nly] 只保留当前标签页,关闭其他标签页

:tabn[ext] {N} {N}gt 切换到编号N 的标签页

:tabn[ext] gt 切换到下一个标签页

:tabp[revious] gT 切换到上一个标签页


插件:ctrlp nerdtree

文本对象text object

命令格式:

[number]<command>[text object]

number 次数

command 命令 d(delete), c(change), y(yank), v(visual)

text object 文本对象 w(单词word), s(句子sentence),p(段落paragraph)


eg:

iw 插入单词

viw 选择模式插入单词

vaw around word 选中当前单词和单词之后的空格

vis/vas/vip/vap


vi(/)//{/}/"/"/’/’

va(/)//{/}/"/"/’/’


ciw 删除单词并插入

ci{ 删除{内容


插件 vim-go

复制粘贴

Normal模式:

y(yank) p (put)

dd 删除一行 p粘贴

Visual模式:选中之后 p粘贴

文本对象:yiw 复制一个单词 yy复制一行


vimrc

:set autoindent 设置自动缩进


粘贴代码格式错乱解决:

:set paste 设置

:set nopaste 取消


Insert模式

ctrl + v/ command + v


Vim寄存器

无名寄存器

dw->p 剪切粘贴单词

x->p 剪切粘贴字符


指定寄存器 ”{register}

eg:

    "ayiw 复制单词到寄存器a中
"bdd 删除当前行到寄存器b中
:reg a 查看a寄存器内容
"ap 粘贴寄存器a中的内容
""无名寄存器,默认模式
"0 寄存器0
寄存器a-z
"+系统剪切板
"% 当前文件名
:echo has("clipboard") 1则支持
:set clipboard=unname 直接使用系统剪切板
".上一次插入的文本
ctrl + r +

:e! 重新加载不保存当前文件

Vim宏macro批量操作

录制操作,回放操作

q录制 q结束

q{register} 保存到寄存器

@{register} 回放操作


v 模式下选择

:normal @a 回放宏操作


eg:

每行前后加"

V 全选行 G 全选文本

:normal I" A"


Vim代码补全

普通关键字

当前缓冲区关键字

包含文件名关键字

标签文件关键字

字段查找

整行补全

文件名补全

全能Omni补全


常用:

补全单词

文件名补全

补全代码,需开启文件类型检查,安装插件


syntax on

:set nu

:set indent

:r! echo % 文件名

:r! echo %:p 文件路径

更换配色

:colorscheme 显示当前主题配色 默认default

:colorscheme 显示所有配色

:colorscheme 配色名 修改配色

$ vim duck.go duck.py -O  # 两个窗口打开</span></span></pre></div></div></div><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">持久化配置 vim ~/.vimrc</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">配色方案 hybrid Monokai</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">下载配色方案拷贝到文件夹 mkdir ~/.vim/colors</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px"><br /></span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">vim插件&#xff1a;</span><a href="https://github.com/flazz/vim-colorschemes" target="_blank" rel="noopener noreferrer">https://github.com/flazz/vim-colorschemes</a></p><h1 style="padding:7px 0px;margin:0px;font-weight:700;font-size:28px;line-height:36px">vim配置</h1><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">持久化配置</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">Linux/Unix vim ~/.vimrc</span></p><p style="font-size:14px;color:rgb( 38 , 38 , 38 );line-height:1.74;letter-spacing:0.05em;outline-style:none;margin:0px"><span class="lake-fontsize-12" style="color:rgb( 77 , 77 , 77 );font-size:16px">windows vim $MYVIMRC 环境变量


常用设置

:set nu # 设置行号

colorscheme hybrid # 设置主题

vim映射 noremap w :w 保存文件

自定义vimscript函数 和插件配置

" 常用设置,这是注释
" 设置行号
set number

" 设置主题
colorscheme hybrid

" 按F2进入粘贴模式
set pasttoggle=<F2>

" 高亮搜索
set hlsearch

" 设置折叠方式
set foldmethod=indent

" 一些方便的映射
let mapleader=','
let g:mapleader=','

" 使用 jj 进入 normal 模式 `^上一次插入点
inoremap jj <Esc>`^

" 使用 leader+w 直接保存
inoremap <leader>w <Esc>:w<cr>
noremap <leader>W :w<cr>

" 切换 buffer
nnoremap <silent> [b :bprevious<CR>
nnoremap <silent> [n :bnext<CR>

" 切换窗口
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

映射

设置leader

let mapleader = “,”


重启vim或source生效

:source ~/.vimrc

插件脚本

脚本语言 vimscrip


查看配置选项

:h option-list

自定义函数

func SetTitle()
if &filetype == 'python'
call setline(1, "\#!/usr/bin/env python")
call setline(2, "\# -- coding:utf-8 --")
normal G
normal o
normal o
call setline(5, "if name == '__main__':")
call setline(6, " pass")
endfunc

github: vim-go-tutoria

开源文件:dotfiles

vim映射

:map - x # 将-映射为x

:map viw 空格映射为选中整个单词

:map dd 可以使用ctrl+d执行dd操作

:unmap - 删除映射


模式

nmap normal

vmap visual

imap insert


v 选中 U/u 转大小写

:imap ddi # insert模式下删除一行

nnoremap n no re map normal模式下,非递归映射(任何时候都应该使用)


《笨办法学vimscript》

vim8 Neovim

            </div>
目录
相关文章
|
25天前
|
数据可视化 开发工具 vr&ar
Vim基础
vim基础知识
20 1
|
4月前
|
IDE Shell 开发工具
004.vim入门
vim vim是一个文本编辑程序没有菜单,只有命令,且命令繁多 基本工作模式 命令模式
32 1
|
5月前
|
缓存 开发工具
vim详细学习
vim 分为三种状态 命令模式(Command mode),输入模式(Insert mode)和底线命令模式(Last line mode)
54 1
|
机器学习/深度学习 人工智能 数据可视化
Vim学习笔记-基础篇
Vim学习笔记-基础篇
44 0
|
数据可视化 开发工具 开发者
vim 介绍|学习笔记
快速学习 vim 介绍
53 0
vim 介绍|学习笔记
|
Linux 开发工具 Windows
30分钟入门Vim
30分钟入门Vim
141 0
|
机器学习/深度学习 Java 程序员
【vim基础教程/vim基础命令合集】一文轻松上手vim
【vim基础教程/vim基础命令合集】一文轻松上手vim
【vim基础教程/vim基础命令合集】一文轻松上手vim
|
人工智能 数据可视化 Unix
Vim 高级应用 | 学习笔记
快速学习 Vim 高级应用。
105 0
|
搜索推荐 Linux 开发工具
Vim 介绍 | 学习笔记
快速学习 Vim 介绍。
112 0