vim 基础学习
auggie

Getting Familiar

Vim modes

  • Command mode
  • insert mode
  • Command line mode
  • Visual mode: switch from command mode to visual node type v

operate with file

create new file or load file

1
:edit message.txt

open file in read-only mode

use -R option

1
2
3
$ vim -R message.txt
# or
$ view message.txt

edit existing file

  • quit editor without saving changes

    1
    :q!
  • save changes and quit editor

    1
    :wq

editing

insert text before cursor

i switch to insert mode

insert text at the beginning of line

problem: you want to insert text at the beginning of current line

solve: use I

append text after cursor

a

append text at the end of line

A

open new line below cursor

o

open new line above cursor

O

change text

problem: want to change text in current line then insert

solve: cc or C

replace text

problem: want to replace character or entire line

solve: r or R

Join text

problem: want to merge two line (this line and next line)

solve: J

Navigating

problem: how to quickly move cursor

Basic navigation (move cursor)

  • h: left
  • l: right
  • j: up
  • k: down

more: navigation use number with those basic command, like 10j

  • 0: move cursor to the beginning of current line. like I but not to insert mode
  • $: move cursor to the end of current line
  • ctrl + f: scroll down entire page
  • ctrl + b: scroll up entire page
  • :n: jump to the nth line
  • :0: jump to the start of file
  • :$: jump to the end of file

word navigation

  • w: move cursor to the beginning of next word
  • b: move cursor to the end of previous word
  • e: move cursor to the end of current word

revisiting editing

cut, copy and paste

  • x: delete character from cursor position
  • X: delete previous character from cursor position
  • y: copy single character
  • p: paste character after cursor position
  • P: paste character before cursor position

multi-position command

  • dw: Delete word from cursor position
  • D: Delete entire line from cursor position
  • dd: Delete entire line
  • Y or yy: Copies entire line

more: 3dw or d3w delete 3 words

  • u: undo like ctrl + z

  • :red or ctrl + r: redo like ctrl + shift + z

searching

search in current file

search in forward direction

  • /<expression>
  • n: next
  • N: previous
  • //: repeat search

search in backward direction

  • ?<expression>
  • n: previous
  • N: next
  • ??: repeat

search word under cursor

  • *: next
  • #: previous

macros

start recording

  1. press q start
  2. use single character as a macros name

stop recording

press q again

use macros

@{macros name} or number@{name}

list macros

:registers

vimrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
let mapleader=" "
syntax on

set number

set cursorline

set ignorecase
set smartcase
set hlsearch
set incsearch
exec "nohlsearch"

nnoremap = nzz
nnoremap - Nzz

nnoremap <LEADER><CR> :nohlsearch<CR>
nnoremap sj :set splitright<CR>:vsplit<CR>
nnoremap sk :set nosplitright<CR>:vspilt<CR>

set noshowcmd
set wrap
set autowrite

set smarttab
set tabstop=4
set shiftwidth=4
set expandtab

set fileencodings=utf-9,gb2312,gbk,gb18030

nnoremap tt :NERDTreeToggle<CR>

nnoremap J 5j
nnoremap K 5k

call plug#begin()

Plug 'connorholyday/vim-snazzy'

Plug 'wakatime/vim-wakatime'

Plug 'preservim/nerdtree'

Plug 'tpope/vim-sensible'

Plug 'vim-airline/vim-airline'

Plug 'Raimondi/delimitMate'

Plug 'ycm-core/YouCompleteMe'

Plug 'tpope/vim-fugitive'

Plug 'dracula/vim', {'as': 'dracula'}

call plug#end()

colorscheme dracula

let g:airline#extensions#tabline#enabled=1
let g:airline#extensions#branch#enabled=1
let g:airline_powerline_fonts=1