TODO
:h caddexpr (:h cad) (write into quickfix) :help curly-braces-names :ilist or [I :colder :cnewer vim setreg() (google keep) ctrl-e and ctrl-y alternative buffer (#) [count]:command :e # reopen closed buffer
!Important!
- :g gx
-
<i_C-F>
: auto adjust indentation
Tricks
highlight word under cursor
/\k*\%#\k*
How to search for a URL without backslashing
use backwards search ?
skip the escaping, isn't it clever?!
?http://www.vim.org/
-
amazing words split trick from vimgolf (http://vimgolf.com/challenges/525ee6a5de92470002000039)
UseVim'sspellcheckfeaturetosplitthissentenceintowordsusingspaces. Vim: set spell:
change above into:
Use Vim's spell check feature to split this sentence into words using spaces. Vim: set spell:
- my solution (30) : 3fsi <ESC>2;2h.3;.fu.5@='1z=[s'<CR>ZZ - amazing solution (18) : A[sE<C-V>dgvpP@.<Esc>U@.ZZ
reverse text
-
option 1:
set ri then cut (C/D/S) the text, and i_ctrl-r "
-
option 2:
$ (move to the end)qq<c-v>}xPq then x@q x is the columns
separate letters
change abcde into -a-b-c-d-e- :s/\_A*/-/g using \A* won't add the - to the end.
scrolling jump
set sj to set the scrolling jump manner. :h 'sj'
"= register
:h "=
for details
change:
- One number per line - ----------------------- 2,3,5,7, 11,13,17, 19,23,29,
into:
2 3 5 7 11 13 17 19 23 29
this will do it: 5gJV"=[<C-R><C-A>]<CR>pZZ
regex
-
\%23c
Matches in a specific column. -
\%<23c
Matches before a specific column. -
\%>23c
Matches after a specific column. -
\%[]
A sequence of optionally matched atoms. This always matches. It matches as much of the list of atoms it contains as possible. Thus it stops at the first atom that doesn't match. For example: >/r\%[ead]
matches "r", "re", "rea" or "read". The longest that matches is used. To match the Ex command "function", where "fu" is required and "nction" is optional, this would work:/\<fu\%[nction]\>
-
/.*foo\&.*bar
: search "foo" AND "bar" In any ORDER
handle search result
-
/foo<CR>
: do the search -
v//e<CR>
: visual select the current match (cusor location) -
d//e<CR>
: delete the current match -
c//e<CR>
: change the current match -
//<CR>
: move to the beginning of next match
!Important! ends
Settings
-
:set display = lastline
"display complete content -
:set list
"show hidden chars -
:set revins
"insert text reverse, (paste text works only in insert (by i,a,c) mode by C-R [reg]). :set ri :set nori
Windows
Normal mode
-
[count]@:
: re-exec last command -
[count]@=
: do a quick macro (will give 'sequence') -
[count]@.
: re-insert last inserted text
Delete
-
X
: remove one character before the cursor, stay in Normal mode -
cc
: with [count] clear line then switch to Insert mode -
S
: same ascc
Motion
-
g_
: last character in line, before the linebreak -
gv
: re-visual select last selection area -
g0,gm, g$
: begin/middle/end of the current line, based on the screenwidth -
`
[ and
`]
: start and end of just yanked text -
`
< and
`>
: start and end of visual selected text
Format
- gq with 77<c-w>v : gq re-format text with window view. recreate a window to make the format precise.
Insert mode
-
<C-W>
: remove the word before cursor -
<C-U>
: remove the word before cursor till line beginning -
<C-G>u
: break undo sequence, start new change -
<C-\><C-O>
: same as CTRL-O, but don't move cursor -
<C-R><C-R> reg
: insert reg content literally -
<C-@>
: insert pre-inserted text -
<C-A>
: insert pre-inserted text -
<C-F>
: auto adjust indentation
Command mode
-
<C-F>
: enter cedit mode, edit cmd with Vim. :h cedit -
<C-R><C-W>
: insert the word under cursor. or type<cword>
or<cWORD>
Commands
- :scriptnames
: show loaded plugin
- :g/foo/y A
: copy all matched line to "a"
Execute and normal!
-
normal
cannot contain special keys e.g.<cr>, <esc>
-
:execute "normal! mqA;\<esc>
`q"
this works. (add ";" to the end of line)
useful atoms for matching
Search and Regex
Search
-
/foo/+3
: find "foo", move cursor 3 lines down -
/foo/e
: cursor set to the end of match -
/foo/s
: cursor set to the start of match -
3/foo/e+1
: find the 3rd "foo", cursor set to the end of match +1 -
/.*foo\&.*bar
: search "foo" AND "bar" In any ORDER
Patterns
Visual area/block
:h \%V
could be used for :s
for example, only replace pattern in visual selected area.
for example:
:s /\%Vfoo\%V/bar/g
lines
-
\%23l
: Matches in a specific line. -
\%<23l
: Matches above a specific line (lower line number). -
\%>23l
: Matches below a specific line (higher line number). - search in current line:
-
1
noremap
<expr>
<Leader>
/
'/\%'
. line(
'.'
) .
'l'
columns
\%23c
Matches in a specific column.
\%<23c
Matches before a specific column.
\%>23c
Matches after a specific column.
substitution trick
Do something like sed/awk's /pattern1/,/pattern2/ s/foo/bar/
1 2 3 | g/pattern1/.,/pattern2/s/foo/bar/g .,/pattern2/ is a range, from current line to the next /pattern2/ matching line |
Char to Hex
Encode all non-ascii characters into Hex
1 | %s/[^\x00-\x7f]/\=printf( '\x%x' ,char2nr(submatch(0)))/g |
Regex
character class
-
[\x00-\x7f]
any ascii chars -
\s
whitespace character: <Space> and <Tab> /\s -
\S
non-whitespace character; opposite of \s /\S -
\d
digit: [0-9] /\d -
\D
non-digit: [^0-9] /\D -
\x
hex digit: [0-9A-Fa-f] /\x -
\X
non-hex digit: [^0-9A-Fa-f] /\X -
\o
octal digit: [0-7] /\o -
\O
non-octal digit: [^0-7] /\O -
\w
word character: [0-9A-Za-z_] /\w -
\W
non-word character: [^0-9A-Za-z_] /\W -
\h
head of word character: [A-Za-z_] /\h -
\H
non-head of word character: [^A-Za-z_] /\H -
\a
alphabetic character: [A-Za-z] /\a -
\A
non-alphabetic character: [^A-Za-z] /\A -
\l
lowercase character: [a-z] /\l -
\L
non-lowercase character: [^a-z] /\L -
\u
uppercase character: [A-Z] /\u -
\U
non-uppercase character: [^A-Z] /\U
NOTE: Using the atom is faster than the [] form.
some special character class
useful for highlighting
-
\i
identifier character (see 'isident' option) /\i -
\I
like "\i", but excluding digits /\I -
\k
keyword character (see 'iskeyword' option) /\k -
\K
like "\k", but excluding digits /\K -
\f
file name character (see 'isfname' option) /\f -
\F
like "\f", but excluding digits /\F -
\p
printable character (see 'isprint' option) /\p -
\P
like "\p", but excluding digits */\P
Ordinary atoms
-
\_^
start of line, any position, zero-width1\_s*\_^foo :white space and blank lines and then "foo" at start-of-line
-
\_$
end of line, any position, zero-width1foo\_$\_s* :"foo" at end-of-line and following white space and blank lines
-
\< and \>
begining/end of a word -
\%$
end of file
-
\%'m
Matches with the position of mark m. -
\%<'m
Matches before the position of mark m. -
\%>'m
Matches after the position of mark m.
Other Patterns
Handle search result (by / or ?)
-
/foo<CR>
: do the search -
v//e<CR>
: visual select the current match (cusor location) -
d//e<CR>
: delete the current match -
c//e<CR>
: change the current match -
//<CR>
: move to the beginning of next match
Copy/Move lines
:copy or :t :move or :m
some hints
-
<C-O>
: move cursor back to last location -
.
: the current line -
:9y
: yank 9th line
Examples
-
:9t.
: copy 9th line and paste below current line -
:3,9t.
: copy from 3-9 lines and paste below current line -
:3t18
: copy 3rd line, copy to 18th line -
:t34
: copy current line to 34th line
replace t with m will do move
script
get character under cursor
1 | matchstr(getline( '.' ), '\%' . col ( '.' ) . 'c.' ) |
get visual selection
-
if selection in oneline:
:echo getline("'<")[getpos("'<")[2]-1:getpos("'>")[2]]
- if multilines are selected
1 2 3 4 5 6 7 8 | function ! s:get_visual_selection() let [lnum1, col1] = getpos( "'<" )[1:2] let [lnum2, col2] = getpos( "'>" )[1:2] let lines = getline(lnum1, lnum2) let lines [-1] = lines [-1][: col2 - 2] let lines [0] = lines [0][col1 - 1:] return join ( lines , "\n" ) endfunction |
or use a register:
1 2 3 4 5 6 7 8 9 10 | function ! GetVisual() try let a_save = @a normal! gv"ay return @a finally let @a = a_save endtry endfunction |
Misc Tips
convert escaped codevalue to unicode character
s#\\u[0-9a-f]*#\=eval('"'.submatch(0).'"')# or s#\v\\u([0-9a-f]{4})#\=nr2char(str2nr(submatch(1),16))#
color hi
-
test highlighting
1
:
so
$VIMRUNTIME/
syntax
/hitest.
vim
Mapping
-
map dynamic single character
1
nnoremap
<expr>
go
'/^'
. nr2char(getchar()) .
'<CR>'
About !
-
:join!
: don't add space between joined lines -
:map!
: map for insert and command mode -
:normal!
: doesn't follow mapping -
:Ack/vimgrep!
doesn't auto open the first item in quickfix
silent
-
map <silent>
: hide message in command line window -
:silent[!] command
: hide messages. with!
, the errMsg will be skipped too.