Vim study notes - Basics

Disclaimer: This article is a blogger original article, welcome to reprint, please indicate the source https://blog.csdn.net/mouday/article/details/91473598

vi vim is an improved version of
windows trial version gvim

Basic Commands

$ 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 modes

1, Normal Normal mode (browse)
the Esc insert mode back to normal mode
Normal mode command operations and various mobile
In most cases, using a browser

2, Insert insert mode (edit)
A / I / O cursor insert mode
A / I / O line in insert mode

3, Command command mode (operation)
Normal mode: command mode
and: q: W Save
: vs (vertical split) approximately vertical split screen: sp (split) below the level of split screen
: set nu set the line number
:% s / java / python / g replace all Java-> Python
: ON syntax syntax highlighting

4, Visual Visual mode (select)
the next mode Nomal mode Visual
v character selection
V selected row
ctrl + v selection block

Insert mode Tips

ctrl + h to delete a character on
ctrl + w to delete the word
ctrl + u delete the current row

Terminal:
Ctrl + A moves to the beginning
ctrl + e is moved to the end of the line

Switch to Normal mode:
the Esc, Ctrl + C, Ctrl + [

gi back to just insert position and insert into the state

move quickly

Normal mode

1, the character moves
on the lower right h k l j left

     ^
     k
< h     l >
     j
     v

2, the mobile word
w / W moves to the next word / WORD at the beginning of
the e / E a word / WORD tail
b / B Back / beginning of the previous word WORD, backword

Non-word blank delimited word
WORD blank delimited word

3, the search moves between the lines
f {char} char character moves to the
t {char} char moves to the front until a character on
the first never found, the search may continue row
semicolon; next
comma, a
F trans to search

4, the horizontal moving
0 moves to the beginning of the first character
^ moves to the first non-blank character
$ is moved to the end of the line
g_ moved to the end of the line non-blank

5, vertical movement
between brackets () the sentence Mobile
view Help: help (
move between paragraphs {}

6, the screen moving
at the beginning of the screen H head
intermediate M middle
end Lower L
ZZ is set to the middle of the screen, the intermediate discharge current row

7, pages move
gg beginning of the document
G End of file
ctrl + o quick return

Half-page ctrl + turn on upword U
ctrl + f forword the turn half page

CRUD

1, increasing the
Normal mode a / i / o A / I / O

2, delete the
Normal mode
x delete a character
4x deletes four characters

D (Delete)
DAW delete the word and the surrounding space (Delete around Word)
DIW delete words (DW)
dd deleting rows
dt {char} deleted until
d $ deleted to the end
d0 to delete the beginning
2dd delete two lines

u undo Undo
ctrl + r recovery revocation

3, modified
in Normal mode
r replace replace one character
eg: RA cursor G- G> A
C Change
CW delete words Word Insert mode Change
CT} {char character to delete, insert mode
s substitute remove and insert mode eg : 4s delete four characters into insert mode

Alternatively a plurality of character constantly R
S delete whole line insert
C delete whole line insert

4, query
/ forward search
? Reverse Lookup
n / a N or the next on a
forward and backward * / # to match the current word

Search results highlight: the SET HLS (High Light Search)
: the SET incsearch

Search and replace

substitute regular support

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

range range
EG:
10,20 lines 10-20 indicates
% represents the total
pattern replacement mode
after replacement text string
replacement flag the flags
G Global global alternative
c confirm acknowledgment
n number matching queries without replacing

eg:

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

Multiple Documents

Open a file buffer Buffer
Window visualization of the divided region
Tab window to a workspace organization

. 1, Buffer
: Buffer LS include
: bn jumps to n-th buffer
: BPRE: bNext: bfirst: BLAST
: Tab completion B buffer_name

: E filename Open file

2, Window
a buffer may be divided into a plurality of windows
each window may be different buffers open
windows divided unlimited

(1) window partition
<ctrl + w> + s horizontal split
<ctrl + w> + v Vertical segmentation
: sp [filename] split level (open the file)
: VS [filename] vertical division (file open)

(2) the window switch
<ctrl + w> + w Cycle
<ctrl + w> + h is switched to the left
<ctrl + w> + j is switched to the lower
<ctrl + w> + k is switched to the upper
<ctrl + w> + l switched to the right

(3) moving window
<ctrl + w> + H is moved to the left
<ctrl + w> + J to move to the lower
<ctrl + w> + K moves to the top
<ctrl + w> + L moves to the right

(4) rearrangements window
: H window-size
<Ctrl + W> + = all window width contour
<ctrl + w> + _ maximize the active window height
<ctrl + w> + | maximize the active window width
n + <ctrl + w> + _ height of the n-th row of the active window is set to
n + <ctrl + w> + | width of the row of the active window is n

3, Tab tab
container of a series of windows: H TabPage
: a tabnew {filename} open a new tab
: tabe [dit] {filename} open a new tab
<ctrl + w> + T the current window is moved to a new tab
: tabc [lose] Close current tab and all windows
: tabo [nly] leaving only the current tab, close other tabs
: tabn [ext] {N} {N} gt switch to the number N tab
: tabn [ ext] gt switch to the next tab
: tabp [revious] gT switch to a tab

Plug: ctrlp nerdtree

Text object text object

Format:

[number]<command>[text object]

number number
command command D (Delete), C (Change), Y (yank), V (Visual)
text the text object Object w (English word), s (sentence sentence), p (paragraph paragraph)

eg:

iw Insert word
viw selection mode insert the word
space after vaw around word to select the current word and the word
vis / vas / vip / vap

vi(/)/</>/{/}/"/"/’/’
va(/)/</>/{/}/"/"/’/’

ciw delete and insert the word
ci {{delete content

Plug-vim-go

copy and paste

Normal mode:
the y-(yank) p (PUT)
dd delete a line p paste
Visual mode: p Paste after the selected
text object: yiw copy a word line yy Copy

vimrc
: set up automatic indenting the SET autoindent

Paste code format confusion solve:
: Paste the SET settings
: set nopaste canceled

Insert Mode
ctrl + v / command + v

Vim register

Unnamed register
dw-> p cutting and pasting a word
x-> p characters cut and paste

Specified register "{} 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 reload without saving the current file

Vim macro macro batch operations

Recording operation, playback operation
q q end recording
q {register} stored in the register
@ {register} playback operation

v select the mode
: normal @a macro playback operation

eg:
before and after each row plus "
V Select All Select All text line G
: normal I" A "

Vim code completion

<Ctrl + n> ordinary keyword
<Ctrl + x> <Ctrl + n> current buffer keyword
<Ctrl + x> <Ctrl + i> include file name keyword
<Ctrl + x> <Ctrl + ]> tag file keyword
<Ctrl + x> <Ctrl + k> field Find
<Ctrl + x> <Ctrl + l> entire line completion
<Ctrl + x> <Ctrl + f> filename completion
<Ctrl + x> <Ctrl + o> Omni completion versatile

Common:
<the Ctrl + n-> <the Ctrl + P> complete words
<Ctrl + x> <Ctrl + f> filename completion
<Ctrl + x> <Ctrl + o> completion code, to open the document type checking, the installation plug

ON syntax
: the SET NU
: the SET indent
: r echo% file name!
: echo% r:! the p-file path

Replace the color

: colorscheme displays the current default color theme default
: colorscheme <Ctrl + d> show all colors
: colorscheme color name to modify the color

$ vim duck.go duck.py -O  # 两个窗口打开

Persistent configuration vim ~ / .vimrc
color scheme hybrid Monokai
Download folder to copy the color scheme mkdir ~ / .vim / colors

vim plugin: https://github.com/flazz/vim-colorschemes

vim configuration

Persistence Configuration
Linux / Unix vim ~ / .vimrc
Windows environment variables vim $ MYVIMRC

Common settings
: set nu # Set the line number
colorscheme hybrid # set a theme
vim mapping noremap w: w to save the file
to customize functions and plug-in configuration 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

Mapping

Set Leader
the let mapleader = ","

Restart vim or source into force
: source ~ / .vimrc

Plug script

Scripting language vimscrip

View configuration options
: h option-list

Custom Functions

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
open source file: dotfiles

vim mapping

: map - x # to - mapped to the X-
: the Map viw space mapped to select the entire word
: map dd can use ctrl + d dd perform the operation
: unmap - Delete Mapping

Mode
nmap Normal
vmap Visual
imap INSERT

v Select U / u transfer case
: delete a row under imap ddi # insert mode
under nnoremap n no re map normal mode, non-recursive mapping (all times should be used)

"Awkward school vimscript"

vim8 unauthorized

Guess you like

Origin blog.csdn.net/mouday/article/details/91473598