vim入门教程2小时学会vim

Vim学习笔记(爆肝整理)

两小时学会vim

用20%的时间学会80%的内容

介绍:

1.本博文的目的在于用最短的时间入门vim编辑器,适合vim入门者
2.vim编辑器的强大之处和功能还有很多,但这篇博文仅用于入门
3.学习vim不是目的,目的应当是熟练的使用vim,希望读者真正使用起来vim
4.如有错误,请在评论区指出,便于他人学习,谢谢
5.如觉得文章有用,请分享给您的朋友们


注意:
1.该演示过程运行在Windows系统上
2.Linux系统下和Windows系统操作命令相同
2.博主已安装gVim,若未安装gVim请自行搜索教程安装


一.准备工作

因为在使用vim编辑器中需要频繁使用Esc键,
但是Esc键距离较偏僻,所以我们选择将CAPSLK键和Esc键进行互换位置


操作方法

1.新建.txt文件,将下列代码复制粘贴到该文件中,并重命名为.reg格式文件。


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,03,00,00,00,3a,00,01,00,01,00,3a,00,00,00,00,00  

2.双击确定,然后重启电脑即可。


二.修改配置文件

找到vim安装目录下的_vimrc的文件使用文本编辑器打开进行配置文件的修改工作


下面是我目前的配置文件,可供参考


注意:
有三点需要小白注意
1.
vim具有很强的拓展性,但是插件的种类和使用太繁杂,小白如果将时间用来装插件,会消耗大量的时间而装了一堆不需要和不会用的插件
2.
这一步建议大家不要跳过,初始界面下的软件操作起来很不舒服,建议大家直接复制粘贴博主这里配置好的文件(虽然不一定好,但至少可以"跑"起来)
3.
该文件比较重要建议大家定期备份


" Vim with all enhancements
source $VIMRUNTIME/vimrc_example.vim
source ~\vimfiles\autoload\plug.vim





"Plug-ins installed via Vim-Plug will then be installed in the directory ~\vimfiles\plugged '
"Avoid using the directory name 'plugin' to avoid confusion with viM standard plug-ins," says vim-Plug's README
call plug#begin('~\vimfiles\plugged')
" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

"Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multipe Plug commands can be written in a single line using / separators
Plug 'honza/vim-snippets'

" On-demand loading
Plug 'scrooloose/nerdtree',{'on': 'NERDTreeToggle'}

" Using a non-master branch
Plug 'rdnetto/YCM-Generator',{'branch': 'stable'}


call plug#end()
"Initial configuration









"Solve menu clutter
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim

set pythonthreedll=C:\Users\51768\AppData\Local\Programs\Python\Python39/Python39.dll
au BufRead *.py map <buffer> <F5> :w<CR>:!winpty python % <CR>
"Initial configuration







" Use the internal diff if available.
" Otherwise use the special 'diffexpr' for Windows.
if &diffopt !~# 'internal'
  set diffexpr=MyDiff()
endif

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 arg1 = substitute(arg1, '!', '\!', 'g')
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg2 = substitute(arg2, '!', '\!', 'g')
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let arg3 = substitute(arg3, '!', '\!', 'g')
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      if empty(&shellxquote)
        let l:shxq_sav = ''
        set shellxquote&
      endif
      let cmd = '"' . $VIMRUNTIME . '\diff"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  let cmd = substitute(cmd, '!', '\!', 'g')
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  if exists('l:shxq_sav')
    let &shellxquote=l:shxq_sav
  endif
endfunction








"color schemes
"colorscheme desert
colorscheme elflord
"colorscheme koehler
"colorscheme slate "bad
"syntax highlighting
syntax on
"display line numbers
set number
"Detect file type
filetype on
"automatic justification
set autoindent
"Choose the alignment intelligently according to the above alignment format
set smartindent
"Sets the matching mode, similar to when an open bracket is entered to match the corresponding close bracket
set showmatch
"When vim is editing, if the command is wrong, a sound will be emitted, and this setting will remove the sound
set vb t_vb=
"Displays the cursor position status line in the lower right corner during editing
set ruler
"Set cursor
set cursorline
"Set the font and size
set guifont=Courier_new:h20:b:cDEFAULT
"2020.12.17





"install some plug-in
call plug#begin()
" Specify a directory for plugins (for Neovim: ~/.local/share/nvim/plugged)
call plug#begin('~/.vim/plugged')
" Make sure you use single quotes
" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'
" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'
" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'
" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
" Using a non-master branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }
" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go', { 'tag': '*' }
" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }
" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'
" Initialize plugin system
Plug 'https://github.com/jiangmiao/auto-pairs'
" Insert or delete brackets, parens, quotes in pair.
Plug 'https://github.com/Xuyuanp/nerdtree-git-plugin'
" can visually browse complex directory hierarchies, quickly open files for reading or editing, and perform basic file system operations.
" 2020.12.21
Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
" An efficient fuzzy finder that helps to locate files, buffers, mrus, gtags, etc. on the fly.
" 2020.12.21
Plug 'https://github.com/vim-airline/vim-airline'
" When the plugin is correctly loaded, there will be a nice statusline at the bottom of each vim window.
" 2020.12.21
Plug 'https://github.com/vim-airline/vim-airline-themes'
" This is the official theme repository for vim-airline
" 2020.12.21
Plug 'preservim/nerdcommenter'
" Code comment tool
" 2020.12.21
Plug 'phonzia/cppSyntaxCheck'
" A c++ syntax checking plugin
" 2020.12.21
call plug#end()


"auto-pairs
au Filetype FILETYPE let b:AutoPairs = {"(": ")"}
au FileType php      let b:AutoPairs = AutoPairsDefine({'<?' : '?>', '<?php': '?>'})


" vim-airline-themes
set laststatus=2  
let g:airline_powerline_fonts = 1  
let g:airline#extensions#tabline#enabled = 1 
let g:airline_theme='moloai' 
"let g:aireline_theme='bubblegum' 
 
if !exists('g:airline_symbols')
let g:airline_symbols = {}
endif
" There are some wrongs 
" my pc cannot show the pictures 
" to solve the problems i use some strings
let g:airline_left_sep = '|>'
let g:airline_left_alt_sep = '>'
let g:airline_right_sep = '<|'
let g:airline_right_alt_sep = '<'
let g:airline_symbols.linenr = 'p'
let g:airline_symbols.branch = '->'
" 2020.12.21


"ctrl + n use nerdtree
map <C-n> :NERDTreeToggle<CR>
"2020.12.21




" configure  nerdcommenter
" Add spaces after comment delimiters by default
let g:NERDSpaceDelims = 1
 
 
" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs = 1
" Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign = 'left'
 
 
" Set a language to use its alternate delimiters by default
let g:NERDAltDelims_java = 1
" Add your own custom formats or override the defaults
let g:NERDCustomDelimiters = { 'c': { 'left': '/**','right': '*/'} }
 
 
" Allow commenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines = 1
" Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace = 1
 
 
" Enable NERDCommenterToggle to check all selected lines is commented or not 
let g:NERDToggleCheckAllLines = 1
" 2020.12.21




"Set TAB to four Spaces
set ts=4
"2020.12.17



"Set gVIM to be full-screen by default
autocmd GUIEnter * simalt ~x
"2020.12.17


完成了配置文件的修改后直接打开gVim软件
打开gvim
由于我修改过设置所有各位看到的界面和我不一样但这不影响操作
注意:
下面输入如下代码

:PlugInstall

输入后按回车键,如图所示
:PlugInstall
回车后
完成后输入

:wq

可以退出软件,重新进入后即可完成配置


三.vim的基本模式和相关操作

vim一共三种模式,分别为普通模式,插入模式,可视模式

1.普通模式介绍


普通模式是什么?

普通模式就是我们进入vim后所在的模式


普通模式有什么用?

1.可以输入指令来操作vim
2.可以进行浏览文档
3.是插入模式和可视模式之间的桥梁


普通模式下的常用操作

如何进入普通模式?

在任何模式下,按Esc键即可进入,如果已经想博主一样改过键位则按CAPSLOCK键


1.退出vim

输入

:q 或 :wq

即可退出vim
两者的区别:
.

  • :q

:q是直接退出,不保存文件,当文件修改时软件会提示,输入:q!即可强制不保存文件退出

  • :wq

:wq是保存文件并退出vim


2.保存文档

不能使用CTRL+S键保存文件
输入:w按回车后即可保存文件


3.光标移动

注:区分大小写

单字符移动

使用hjkl可以使得光标逐字符移动
其中,h:左移,j:下移,k:上移,l:右移
没必要可以去记,用着用着就会了

  • 单词间移动
    常用:
名称功能
w将光标移动到下一个单词的开头
b将光标移动到上一个单词/当前单词的开头

博主不常用但可以使用的其他操作(用者自取):

名称功能
e将光标正向移动到当前单词/下一单词的结尾
ge将光标反向移动到上一单词的结尾
行内移动
名称功能
$(shift+4)将光标移动到行尾
0(数字零)将光标移动到行首
通过查找移动

常用

名称功能
f{char}光标移动到下一个{char}所在处}
F{char}光标移动到上一个{char}所在处
;重复上次的字符查找命令
,反转方向执行上次的字符查找命令

不常用:

名称功能
t{char}正向移动到下一个{char}所在处的上一个字符上
T{char}反向移动到上一个{char}所在处的后一个字符上
*注:只能在同一行进行移动*

例如:
我们这里想把光标移动到w上,即可在普通模式下输入fw
输入前:
1输入后:
after

快速跳转到文件首尾
名称功能
gg将光标跳转到文件开头
shift+g将光标跳转到文件末尾

4.查找与替换
名称功能
/输入/和想查找的字符后回车 使用n键下一个
:%s/原字符/替换字符/g输入后按回车即可全文替换

5.翻页
名称功能
CTRL+u向上翻半页
CTRL+d向下翻半页

6.设置标记与快速回跳
名称功能
m标记字符用标记字符在当前位置标记
`标记字符返回到字符所标记的位置

注:`符号在键盘Esc键下方,Tab键上方

例如:

如下图我们可以看到光标现在的位置在第十行
1
我们输入ma即可在第十行使用字符a进行标记,然后将光标移至开头,输入

`a 即可跳回至第十行

如图:
跳回前跳回后


7.撤销

在vim中不能用CTRL+z撤销
取而代之的是用u进行撤销(小写)
另外CTRL+r可以撤销u刚刚撤销的操作


8.删除

在vim中删除为x,在可视模式下选中后按x键即可删除


2. 插入模式介绍


插入模式是什么?

插入模式是我们进行编辑文档的模式,在此模式下我们可以自由的打字


插入模式有什么用?

插入模式的作用就是能够让我们修改文档


插入模式的相关操作
如何进入插入模式
  • 在普通模式下按i或a进入插入模式。
    二者区别
    |i|在光标停留字符之前进行插入|
    |–|--|
    |a|在光标停留字符之后进行插入|
    例如:
    如图,光标停留在b上
    1按下i键后,光标在b字符之前
    2
    若按下a键,则在b字符之后
    3

进入到插入模式后,就可以自由编辑文档了


4. 可视模式介绍


什么是可视模式?

可视模式就是进行字符选中的模式


可视模式有什么用?

可视模式可以选取大片的字符


如何进入可视模式?

在普通模式下按v, V, ctrl+v 三种方式均可进入可视模式
下面简单介绍三者的区别:

名称功能
v按字符选中
V按行选中
ctrl+v按块选中

下面是三者的展示:
普通模式下的界面:
1

  • 按下v键进入可视模式如下:
    2
    在该模式下,可以进行上述光标的移动
    包括:hjkl, w, b, ctrl+u, ctrl+d
    其选中的范围也是以普通模式下光标所在的位置为中心进行移动
    如图:
    3

  • 按V键进入可视模式如下:
    在该模式下,只能按行来进行选中即选中的区域只能是某行到某行不能在行中间取消选中
    如图所示:
    5

  • 按ctrl+v进入可视模式如下:
    在该模式下,选中的范围可以近似理解为在Windows桌面下能够用鼠标进行选中图表的范围(读者尝试一下就能理解这个恰当的比喻了)
    如图:
    7


操作符待决模式(vim高效率的核心)

文本对象
文本对象的分类
名称描述
分隔符文本对象被某种形式的符号括起来的对象
范围文本对象字符,字符串,句子,段落等

解释:

分隔符文本对象举例:
	(abc) , {abc},  [abc],    'abc',  "abc"
	上述的都是分隔符文本对象
范围文本对象举例:
	abcdefg   啦啦啦啦啦
	上述的都是范围文本对象
对文本对象的选择操作
  • 基本操作前缀辨析:
名称描述
前缀i符号括起来的对象
前缀a符号本身+其中的对象

解释:
i - in, 例如在(abc)中,为abc
a - around,例如在(abc)中,为(abc)

对于分隔符文本对象的选择操作:
名称描述
ab一对()及其内部文本 或a), a(,
ib一对()中的文本 或i), i(
a{一对{}及其内部文本
a}一对{}中的文本
根据上述以此类推

例如:
想要选择该分隔符文本对象’abc’
a’即选中’abc’
i’即选中abc

对于文本对象的选择操作
名称描述
iw当前单词
aw当前单词和一个空格
iW当前字符串
is当前句子
操作符待决模式 + {motion}
  • 使用环境:普通模式下
  • {motion}指的是上述的文本对象
名称描述
d{motion}删除模式
c{motion}插入模式
v{motion}可视模式
y{motion}复制模式
举例:
  • dab 即为删除当前光标所在位置的()内的文本和()本身

使用dab前
1

使用dab后
2

  • dib 即为删除当前光标所在位置的()内的文本

使用dib前
3
使用dib后
4

  • cab 即为删除当前光标所在位置的()内的文本和()本身后进入插入模式

使用cab前:
5
使用cab后:
6

  • cib 即为删除当前光标所在位置的()内的文本后进入插入模式

使用cib前:
在这里插入图片描述
使用cib后:
7

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值