# vim - config and usage
###### tags: `linux` `vim`
```
# turning off auto indent
:set paste
# turning of auto indent
:set nopaste
# map F3 key to turning of/off paste mode
:set pastetoggle=<F3>
# seto indenting to four spaces
:set shiftwidth=4
# add comment
# show line number first,
# then use the line number to specify line range to comment out the lines.
# for example comment out from line 10 to 20
: set number
:10,20s/^/#/
```
configure ~/.vimrc
```
# enable / disable auto indent
filetype indent off
# if you forget to open a file that you make changes to, and can not save
# ":w!!" will prompt for sudo perms and write it out.
cmap w!! w !sudo tee % > /dev/null
```
```
:w !sudo tee %
```
### change color schema
```
list available vim color in MAC OS
Yu-Jung-MacBook-Pro:~ yu-jungcheng$ ls -l /usr/share/vim/vim80/colors/
total 0
-rw-r--r-- 1 root wheel 2904 7 Aug 09:56 README.txt
-rw-r--r-- 1 root wheel 2476 7 Aug 09:56 blue.vim
-rw-r--r-- 1 root wheel 2990 7 Aug 09:56 darkblue.vim
-rw-r--r-- 1 root wheel 548 7 Aug 09:56 default.vim
-rw-r--r-- 1 root wheel 2522 7 Aug 09:56 delek.vim
-rw-r--r-- 1 root wheel 2812 7 Aug 09:56 desert.vim
-rw-r--r-- 1 root wheel 1666 7 Aug 09:56 elflord.vim
-rw-r--r-- 1 root wheel 2452 7 Aug 09:56 evening.vim
-rw-r--r-- 1 root wheel 1958 7 Aug 09:56 industry.vim
-rw-r--r-- 1 root wheel 3555 7 Aug 09:56 koehler.vim
-rw-r--r-- 1 root wheel 2460 7 Aug 09:56 morning.vim
-rw-r--r-- 1 root wheel 2006 7 Aug 09:56 murphy.vim
-rw-r--r-- 1 root wheel 1037 7 Aug 09:56 pablo.vim
-rw-r--r-- 1 root wheel 2673 7 Aug 09:56 peachpuff.vim
-rw-r--r-- 1 root wheel 1393 7 Aug 09:56 ron.vim
-rw-r--r-- 1 root wheel 2720 7 Aug 09:56 shine.vim
-rw-r--r-- 1 root wheel 2445 7 Aug 09:56 slate.vim
-rw-r--r-- 1 root wheel 1629 7 Aug 09:56 torte.vim
-rw-r--r-- 1 root wheel 1840 7 Aug 09:56 zellner.vim
# to change color setting permentally,
# edit file in ~/.vimrc (create the file if not exist)
colo murphy
colo delek
colo desert
```
### move cursor
```
In normal mode (press ESC)
# jump to end of file
:$
G
# go to first line
:1
gg
1G
1 gg
# move to end of a word, or number of word (# is number)
e
#e
# move forward to begining of a word or or number of word (# is number)
w
W
#b
# move to end of line
$
# move to beginning of line (number zero)
0
# jump forward a line or number of lines (# is number)
j
#j
# jump backward a line or number of lines (# is number)
k
#k
# jump to top of screen
H
# jump to middle of screen
M
# jump to bottom of screen
L
```
### search and replace string
```
In vim editor, some basic tips for searching and replacing string in content.
# find 'search' in all lines and replace it with 'replace'
:%s/search/replace/g
# find 'search' in current line only and replace it with 'replace'
:s/search/replace/g
# find 'search' in all lines and replace it with 'replace' and ask for confirmation first
:%s/search/replace/gc
# change only whole words exactly matching 'search' to 'replace' and ask for confirmation.
:%s/\<search\>/replace/gc
# change each 'search' (case insensitive) to 'replace' and ask for confirmation
:%s/search/replace/gci
:%s/search\c/replace/gc
# change each 'search' (case sensitive) to 'replace' and ask for confirmation
:%s/search/replace/gcI
:%s/search\C/replace/gc
!!!
g - global
c - confirm
i - case insensitive
I - case sensitive
```