-
Notifications
You must be signed in to change notification settings - Fork 0
/
vim-commands.vim
193 lines (148 loc) · 6.35 KB
/
vim-commands.vim
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#
# some memos and tips for vim commands
#
##############################################################################
#### buffer navigation
Ctrl-o - to the previous buffer.
Ctrl-i - to the next buffer
##############################################################################
#### Search in current buffer
* - will search the current word (under the cursor) forward
# - will search the current word backward
/ - will search the user input forward
? - will search the user input backward
# search and replace: "gc" global and confirm
:%s/from string/to string/gc
# to search for visually selected text, we will need create a
# map in visual mode.
:vnoremap // y/\V<C-R>=escape(@",'/\')<CR><CR>
# This post (https://vim.fandom.com/wiki/Search_for_visually_selected_text)
# has a lot more advanced way to serch visually selected text
##############################################################################
#### some tips for the grep.
# some good post to read:
# - https://phoenixnap.com/kb/grep-multiple-strings
# Example to ind a java class and exclude all java files from the target folder.
# There are some generated java source code in the target folder.
:grep -r --include=*.java --exclude="*/target/*" 'class MLotMessage' .
# search bothe class and interface
# using the extended regular expressions, -E
grep -rE --include=*.java --exclude='*/target/*' '(interface\|class)\sLotService' .
grep -rE --include=*.java --exclude='*/target/*' '(interface\|class) LotService' .
# if we put the -l option, the grep search result will NOT be able to
# load the match file from the quickfix list panel (:copen)
:grep -lr --include=*.java --exclude="*/target/*" 'class MLotMessage' .
# the exclude-dir option works better.
# the following command will exclude both target and classes directories
:grep -lr --include=*.java --exclude-dir={target,classes} 'class MLotMessage' .
# find all methods in current file:
:vimgrep /public\|protected\|private\s\w\s\w(/ %
# find all sections parts in an ini file
# buildout config .cfg file is using ini format.
# for example:
# [buildout]
#
:vimgrep /\[.\+\]/ %
# search a line starts with a whitespace,
# for example search a line starts with at less one whitespace and ServerName:
:vimgrep /^ \+ServerName/ %
# navigate iterate through the search result:
cn - next match
cp - previous match
copen - load the quickfix to show all matches
# Exampl to find a file and exclude a the foler node_modules.
:!find . -name *.md ! -path "*/node_modules/*"
# read the output to current line.
:r!find . -name *.md ! -path "*/node_modules/*"
##############################################################################
#### map
# map will show current map function, or bookmark
:map
# NOTE: The ending "<CR>" will add the return keyboard at the end.
# Without "<CR>" will hwlp debug the map.
# create map to grep current word.
# we need the double escape "\\" in map!
# this map will hook "gs" to search all java files to find the class and interface defination.
:nnoremap gs :grep -rE --include=*.java --exclude='*/target/*' '<cword>' .
:nnoremap gs :grep -rE --include=*.java --exclude='*/target/*' '<C-R><C-W>' .
:nnoremap gS :grep -rE --include=*.java --exclude='*/target/*' '<cword>' .<CR>
# map "gc" to the grep search which has the class/interface definition
:nnoremap gc :grep -rE --include=*.java --exclude='*/target/*' '(class\\|interface) <cword>' .
:nnoremap gc :grep -rE --include=*.java --exclude='*/target/*' '(class\\|interface) <C-R><C-W>' .
:nnoremap gC :grep -rE --include=*.java --exclude='*/target/*' '(class\\|interface) <cword>' .<CR>
# map "gm" to find all method in current java file.
# we need the "\\" here!
:nnoremap gm :vimgrep /public\\|protected\\|private\s\w\s\w(/ %<CR>
:nnoremap gm :grep -E '(public\\|protected\\|private) ' %
:nnoremap gM :grep -E '(public\\|protected\\|private) ' %<CR>
# map "gf" to find the file match the current word, exclude the "target" folder.
# we don't need escapt the first "!"
:nnoremap gf :!find . -name <cword>.java \! -path "*/target/*"
:nnoremap gf :!find . -name <C-R><C-W>.java \! -path "*/target/*"
:nnoremap gF :!find . -name <cword>.java \! -path "*/target/*"<CR>
# map "ge" to edit the file name
:nnoremap ge :e **/*<C-R><C-W>
# map "sb" to generate the buffer search pattern:
# the "<C-R><C-W>" is the same with "<cword>" in here.
# The difference is the command such as "grep" will expand "<cword>" to current word.
:nnoremap sb :b <C-R><C-W>
# map "sc" to search current word in current buffer.
:nnoremap ,sc :vimgrep /<C-R><C-W>/ %
####################
# How to unmap a map.
# unmap will remove the map.
:unmap gf
:unmap gc
##############################################################################
#### work with bash
# execute current line as bash command.
# . will get the current line
:.w !bash
# we could map this to ,br
# . is used to repeat previous action.
:nnoremap ,br :.w !bash<CR>
# copy current line to clipboard
# this is working for MacOS
:.w !pbcopy
# w works perfectly with visual mode.
# after select anything in visual mode, we could write them to the clipboard:
:w !pbcopy
# For example:
# read current date time to next line by executing the date bash command.
:r!date
##############################################################################
#### vim window management.
# split buffer
:sp
# vertically split buffer
:vsp
# resize the current window
:resize +5
# resize the current window vertically.
:vertical resize +20
# switch windows:
C-w hjkl # switch to the window of the direction: hjkl
C-w C-w # switch to the next window and looping
# To change two vertically split windows to horizonally split
Ctrl-w t Ctrl-w K
# Horizontally to vertically:
Ctrl-w t Ctrl-w H
# Explanations:
# Ctrl-w t makes the first (topleft) window current
# Ctrl-w K moves the current window to full-width at the very top
# Ctrl-w H moves the current window to full-height at far left
##############################################################################
#### fold and expand
zf - create fold, it can be used in virtual mode,
zc - collapse fold
zo - expand fold
zd - delete fold at the cursor
##############################################################################
#### session management
# save session.
# maps will b saved when save session.
:mksession ~/.vim/session-name.vim
# override the existing session file.
:mksession! ~/.vim/session-name.vim
# load existing session.
:sourc ~/.vim/session-name.vim