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>
目录
相关文章
|
Web App开发 弹性计算 负载均衡
阿里云acp考试时间、内容?阿里ACP认证考试有什么经验?
阿里acp认证考试有什么经验?众所周知,阿里云针对不同云产品类别、用户行业、用户成长阶段,精心打造不同的认证考试。阿里云认证分为四级ACF、ACP、ACA、ACM。拿到阿里云证书即使以后从业相关行业都可以拿到高新、以及相关行业的专业资格
2149 0
阿里云acp考试时间、内容?阿里ACP认证考试有什么经验?
|
网络协议 数据安全/隐私保护 网络架构
软路由R4S+iStoreOS实现公网远程桌面局域网内电脑
软路由R4S+iStoreOS实现公网远程桌面局域网内电脑
867 0
|
NoSQL 关系型数据库 数据库
天生一对,当游戏遇上MongoDB
当游戏遇上MongoDB,会碰撞出什么样的火花,本文为您一一道来。MongoDB针对游戏灵活多变需求、一些专有场景-道具自动过期和附近玩家、高可用、高可扩展、回档、滚服、运营数据分析等场景都有非常好的解决方案,可谓是天生一对。
11212 0
|
8月前
|
人工智能
替代你的不是AI,而是会使用AI的人——生成式人工智能(GAI)认证成为职场新宠
在AI技术飞速发展的数字化时代,职场环境正经历深刻变革。生成式人工智能(GAI)认证的出现,为职场人士提供了提升自我、证明能力的新途径。由培生推出的GAI认证,涵盖核心技能与伦理知识,助力求职者脱颖而出。它不仅是职场晋升的加速器,还为企业认可的专业能力背书。拥抱AI、学习AI,通过GAI认证,让自己成为掌握AI技术的领先者,在竞争中保持优势。
|
9月前
|
敏捷开发 监控 数据可视化
产品开发为什么需要看板管理?掌握这些技巧提高效率!
在数字化时代,产品开发是企业创新与竞争的关键。本文探讨了产品开发的核心流程、敏捷管理方法及高效工具的应用,如板栗看板,以优化团队协作和资源配置,加速产品上市并提升市场竞争力。核心流程涵盖市场调研、规划设计、研发迭代、测试优化、上线推广及持续改进。敏捷开发强调短周期迭代、跨团队协作和数据驱动决策。可视化工具如板栗看板能有效提升任务透明度和协作效率,确保产品按时高质量交付。
|
Ubuntu Linux 数据库
【Linux】深入了解Linux磁盘配额:限制用户磁盘空间的利器
【Linux】深入了解Linux磁盘配额:限制用户磁盘空间的利器
|
11月前
|
机器学习/深度学习 人工智能 算法
【AI系统】推理参数
本文介绍了AI模型网络参数的基本概念及硬件性能指标,探讨了模型轻量化的重要性。随着深度学习的发展,模型参数量和计算量不断增大,导致难以直接部署到移动端。文章详细解析了FLOPs、FLOPS、MACCs等计算指标,并对比了标准卷积层、Group卷积、Depth-wise卷积和全连接层的参数计算方法,为模型优化提供了理论基础。
494 1
|
机器学习/深度学习 人工智能 算法
|
JavaScript 开发者
三个小时vue3.x从零到实战(后)(vue3.x配套工具及项目化构建)
文章详细介绍了Vue 3.x相关的配套工具及项目化构建流程,包括构建工具的配置与最佳实践,帮助开发者更好地管理和构建Vue项目。
228 1
|
数据可视化 架构师 测试技术
【计算机三级数据库技术】第5章 UML与数据库应用系统--附思维导图
本文提供了UML在数据库应用系统设计中的应用概览,包括UML建模框架、视图、四大图的介绍,以及如何使用活动图、用例图、类图、顺序图等UML图来表达业务流程、系统需求和内部结构,最后还涉及了系统微观和宏观设计的UML表达方式。
578 4