-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.vimrc.base.mappings
More file actions
510 lines (473 loc) · 18.4 KB
/
Copy path.vimrc.base.mappings
File metadata and controls
510 lines (473 loc) · 18.4 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
" Standard motions {{{
"
" Word motions: w, b, e, ge / W, B, E, gE
" Char search: f, t, F, T and ; / ,
" Line start/end: 0, $, ^ (non-blank start)
" Search: /forward or ?backward
" By sentence: ( , )
" By paragraph: { , }
" By-section: [[ , ]]
" By method: ]m , [m (start), ]M , [M (end)
" Comment start/end: [* , ]* or [/ , ]/
"
" Text objects: Vi{ to select a block in {}
"
" Change list: g; / g, or gi to insert at last change position
" Jump history: CTRL-O / CTRL-I
" Matching pair jump: %
" Line jumps: 50G to line 50 or 20j to 20 lines down
" Visual modes: v, V, <CTRL-V>
" Repeat last change: .
" Record macro / execute macro: q[key], q / @[key]
"
" Insert at start/end: I / A
" Insert line above/below: O / o
" Insert line above/below in insert mode: <CTRL-o>O / <CTRL-o>o
" Join lines: J
" Indent / unindent: > / <
"
" Combos:
" - ciw - change inner word
" - ci( - change inside brackets (works on closed brackets, even if cursor is outside)
" - vi" - select inside quotes
" - d/foo - delete until next "foo"
" - y?bar - yank until previous "bar"
" - c2w - change 2 words
" - 3j - move 3 lines down
" - 3J - join 3 lines
" - xp - swap two characters (x to delete, p to paste, it goes to the right)
"
" Command line history (select, edit and execute previous commands):
" - q: - command line history
" - :<CTRL-F> - command line history (same as above, but in command mode)
" In the history buffer: go to insert mode after the command, Enter to
" execute.
"
" See: http://vimcasts.org/blog/2013/02/habit-breaking-habit-making/
" http://stackoverflow.com/questions/37059399/vim-motion-without-jk-and-hardmode-installed
" https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim
" https://pbrisbin.com/posts/hard_mode/
" :help motion.txt
" }}}
" Mappings {{{
" let mapleader = "\<space>"
" Below is better then `let mapleader` because vim command line will still
" display leader-based mappings as \s
" From http://www.reddit.com/r/vim/comments/1vdrxg/space_is_a_big_key_what_do_you_map_it_to/
map <space> <leader>
" * and # will stay on the first match
nnoremap * *N
nnoremap # #N
" Use CTRL-N to remove search highlight
noremap <C-N> :noh \| call clearmatches()<CR>
vnoremap <C-N> <C-C>:noh \| call clearmatches()<CR>gv
" CTRL-N in insert mode is a completion!!!
" inoremap <C-N> <C-O>:noh<CR>
" Visual search
" select text and hit * / # to find it
" http://got-ravings.blogspot.com/2008/07/vim-pr0n-visual-search-mappings.html
" https://github.com/godlygeek/vim-files/blob/master/plugin/vsearch.vim
" Visual mode search
function! s:VSetSearch()
let temp = @@
norm! gvy
let @/ = '\V' . substitute(escape(@@, '\'), '\n', '\\n', 'g')
" Use this line instead of the above to match matches spanning across lines
"let @/ = '\V' . substitute(escape(@@, '\'), '\_s\+', '\\_s\\+', 'g')
call histadd('/', substitute(@/, '[?/]', '\="\\%d".char2nr(submatch(0))', 'g'))
let @@ = temp
endfunction
" xnoremap - only for visual mode, but not for select mode
xnoremap * :<C-u>call <SID>VSetSearch()<CR>/<CR>N
xnoremap # :<C-u>call <SID>VSetSearch()<CR>?<CR>N
" Usage tip: highlight-select-change pattern
" - use `*` to highlight the word
" - select the area in visual mode
" - execute :'<,'>s//new_word/g or :d or :g, etc.
"
" In this scenario, we can visually see words that will be replaced
" and we limit the operation to the selected visual area.
" The key is that we highlight the word with `*` first, then we can
" skip the first argument for :s or other commands and we see it
" on the screen.
"
" Useful ex commands:
" - :g//t$ - copy all lines with the word to the end of the file
" - :g//d - delete all lines with the word
" - :g!//d or :v//d - delete all lines without the word
" - :g//m$ - move all lines with the word to the end of the file
"
" More complex examples:
" - :v/./,/./-j - replace multiple empty lines with one
" - :'<,'>g/^$/d - delete empty lines in the visual area
" - :'<,'>s/$/\r/g - add new line after each line in the visual area
"
" Ex commands with range:
" - :8,15d - delete lines 8 to 15
" - :8,15co . - copy lines 8 to 15 after the current line
" - :-5,-3co . with relative numbers - copy lines from 5th above to 3rd above
" after the current line (works great with :set relativenumber)
" - :8,15m . - move lines 8 to 15 after the current line
"
" Some ex commands for edititng:
" - :co[py] - copy lines
" :t - same as ":copy"
" - :m[ove] - move lines
" - :c[hange] - change lines (type new text, enter . to finish)
" - :d[elete] - delete line
" - :j[oin] - join lines
"
" Globals, execute commands for matching lines:
" - :g[lobal] - execute commands for matching lines
" - :v[global] - execute commands for not matching lines
" - :norm[al] - execute Normal mode commands
" for example, :%norm I// - add // to the beginning of each line
" or :g/^$/norm J - join empty lines
" - :@ - execute contents of a register
" we can enter the command in a normal buffer, yank it, then execute
"
" Related:
" - :exe[cute] - execute a string as a command
" (for example, :exe "norm ixxx\<Esc>", so we can use special chars like
" \<Esc>, it can be used with any commands,
" like ':exe "e " .. expand("%") .. ".bak"' to edit the file with the same
" name as the current file and .bak extension)
"
" Filter, sort, insert text:
" - :! - filter lines or execute an external command
" can be used to run shell commands over the text,
" like :%!uniq to remove duplicate line
" or :.!date to insert date on the current line
" or :.!xargs curl -s on the line with URL to insert that URL
" contents into the file.
" Related: :.write !xargs curl -s to display the output without inserting
" - :sor[t] - sort lines
" - :r[ead] - read file into the text
" can also be used to execute shell commands, for example:
" :read !date - to insert the date into the file
" :read !curl -s https://example.com - to insert the content of the URL
" - :y[ank] - yank lines into a register
" - :pu[t] - insert contents of register in the text
"
" Formatting:
" - :< - shift lines one 'shiftwidth' left
" - :> - shift lines one 'shiftwidth' right
" - :ce[nter] - format lines at the center
" - :le[ft] - left align lines
"
" References:
" - https://habr.com/ru/articles/131951/
" - http://vim.wikia.com/wiki/Power_of_g
" - :help Ex-commands
" Repeatable "change word" command:
" - set cursor on a word, cg* - change it,
" - now '.' will repeat the change on the next word, n - skip the next word
nnoremap cg* *Ncgn
" gv - select last visual area and go to visual mode
" Visually select the text that was last edited/pasted
nnoremap gV `[v`]
" Don't move the cursor after yanking the visual selection
" (usually it jumps back to the start of the selection)
" http://ddrscott.github.io/blog/2016/yank-without-jank/
vnoremap <expr>y "my\"" . v:register . "y`y"
" Y will yank line-wise even if you have character-wise selection
vnoremap <expr>Y "my\"" . v:register . "Y`y"
" Even better: just change something and then use `g.` to start repeating
" the change on next occurences of the word.
" https://www.reddit.com/r/neovim/comments/sf0hmc/im_really_proud_of_this_mapping_i_came_up_with/
" the mapping lets me use . to repeat a regular c-prefixed command
" as if it were performed using cgn.
" For example, do `ciw` at some word and then go `g.` to change next such
" word.
nnoremap g. /\V\C<C-r>"<CR>cgn<C-a><Esc>
" Note: to replace something with yanked text it's easy to use
" a change operation and then <C-R>0 to insert register '0' content
" in insert mode, like cw<C-R>0 - replace a word with yanked text
" save
noremap <leader>w :w<CR>
noremap <leader>q :q<CR>
" jj or j+k as ESC
" other options: Ctrl-[, Ctrl-C
inoremap jj <Esc>
inoremap jk <Esc>
inoremap kj <Esc>
" Make last word uppercase
inoremap <C-F> <Esc>gUiw`]a
"note: "+ - real clipboard, "* - current selection
"note: p - paste after cursor, P - before
" gp - cursor will be after pasted text
" with just p - on the last pasted char
nnoremap <Leader>y "+y
vnoremap <Leader>y "+y
nnoremap <Leader>p "+p
vnoremap <Leader>p "+p
nnoremap <Leader>P "+P
vnoremap <Leader>P "+P
" Remap p in visual mode to paste yanked text to allow pasting multipe times.
" Otherwise the removed text will be in the clipboard and pasted on the
" next paste.
" For example:
" - Select and copy "word1" - we, y
" - Select "word2" and press p
" - Select "word3" and press p
"
" By default, the last paste will replace `word3` with `word2`. This is
" because we pasted over `word2` which was deleted and got into the
" default register holding the last deleted text.
" With override below, we will paste the `word1` instead.
"
" To do the 'normal' vim paste, use gp (almost the same as p, with cursor
" after the pasted text instead of the last char of it)
"
" Alternative method: do not paste over in visual mode - copy the text,
" then search for the thing to replace, do ct{something}, CTRL-R * to
" paste in the insert mode, then n. to repeat.
"
" Alternative method: xnoremap p "0p
" Note:
" The problem with `xnoremap p "0p` override is that if we delete
" something explicitely, we would expect next "p" to paste it,
" but it pastes the prvious copied text instead:
" - Copy "word1"
" - Select "word1" and press p
" - Delete "new_word" with de
" - Select "word3" and press p
" On the last step, it is expected to paste `new_word`, but actually it
" pastes the `word1`. Workaround: use `gp`.
" Trying the `pgvy` instead - it re-copies just pasted text, so does not
" affect the deletion operation
"
xnoremap p pgvy
" Ctrl-R in insert mode or command line to paste:
" - Ctrl-R + a-z - named register
" - Ctrl-R + " - default register
" - Ctrl-R + % - current file name
" (useful for :r!cat % to insert the file content)
" - Ctrl-R + # - alternate file name
" - Ctrl-R + * / + - clipboard contents
" - Ctrl-R + / - last search
" - Ctrl-R + : - command line
" - Ctrl-R + . - last inserted text
" - Ctrl-R + - - last small delete
"
" - Ctrl-R + = - evaluate expression
" (for example, in insert mode, Ctrl-R =, then 2+2)
"
" Only in command line mode:
" - Ctrl-R Ctrl-W to paste word under cursor
" - Ctrl-R Ctrl-A to paste WORD under cursor
" - Ctrl-R Ctrl-L to paste line under cursor
" - Ctrl-R Ctrl-F filename under cursor
" - Ctrl-R Ctrl-P filename under cursor
" - Ctrl-\ e {expr} - evaluate expression and replace command line with the
" result
"
" See: :help i_CTRL-R and :help c_CTRL-R
" Note about @ command:
" From: https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim
"
" The @ command is probably the most obscure vi command.
" @ executes the contents of a register as if it were a vi or ex command.
" Example: I often use: :r!locate ... to find some file on my system and
" read its name into my document. From there I delete any extraneous hits,
" leaving only the full path to the file I'm interested in.
" Rather than laboriously Tab-ing through each component of the path
" (or worse, if I happen to be stuck on a machine without Tab completion
" support in its copy of vi) I just use:
" - 0i:r (to turn the current line into a valid :r command),
" it becomes something lke :r /path/to/file
" - "cdd (to delete the line into the "c" register) and
" - @c execute that command.
"
" So we execute the `:r /path/to/file` command with @c and the file content
" is inserted into the current buffer.
"
" ---
"
" Practically it allows using main vim buffer to edit and execute commands
" The commaand buffer is not super convenient
"
" For example: do "cdd or "cyy on the line below and then @c to add // to all lines.
"
" :% norm I//
"
" This is also the same as instead of recording the macro, type it out and then execute.
" Or record, paste and edit, for example
"
" - qa - start recording into register a
" - q - stop recording
" - "ap - paste the macro, edit it
" - "ayy - yank back
" - @a - execute
" Move cursor by display lines when wrapping
" http://vim.wikia.com/wiki/Move_cursor_by_display_lines_when_wrapping
" noremap k gk
" noremap j gj
" noremap gk k
" noremap gj j
"noremap 0 g0
"noremap $ g$
" Put ending { to the next line
function! ClosingBracketToNextLine()
normal g$F{i
endfunction
"nmap <leader>bn :call ClosingBracketToNextLine()<CR>
" Standard keys
" Speller shorcuts
" z= - suggest word
" [s - previous wrong word
" ]s - next wrong word
" History
" http://vim.wikia.com/wiki/Using_command-line_history
" q: for commands
" q/ for searches
" or type : or / to start entering a command or search,
" then press the 'cedit' key (default is Ctrl-f :help 'cedit').
nnoremap grl :call ToggleRelativeAbsoluteNumber()<CR>
function! ToggleRelativeAbsoluteNumber()
if &relativenumber
set norelativenumber
else
set relativenumber
endif
endfunction
" ,vv to re-read .vimrc
nnoremap <Leader>vv :call Preserve("source ".$MYVIMRC)<CR>
" ,vc to edit .vimrc
"nnoremap <leader>vc :tabedit $MYVIMRC<CR>
nnoremap <leader>vc :tabedit $HOME/.vim/.vimrc<CR>
" }}}
" Windows navigation {{{
" C-W h|j|k|l - move to left|down|up|right win
" v C-W h|j|k|l - split to left|down|up|right win
" C-W w - cycle
" C-W s|v - split current win horiz | vert
" :on[ly] - leave only current win
" C-W +|- - height +|- 1 px
" C-W > - increase current window width N columns
" C-W _|| - maximize height|width
" moving windows:
" C-W H|J|K|L - move win to the left|down|up|right
" C-W r - rotate
" C-W x - exchange with neighbour
" C-W T - move window to separate tab
" C-W P - go to preview window
" C-W z - close preview window
"
" Window swap plugin: <leader>ww in first window and
" then <leader>ww in second to
" swap them.
" move to and open if not exists
" http://www.agillo.net/simple-vim-window-management/
" Note: the `range` is needed to prevent the function invocation multiple
" times when there is a visual selection (by default, the function is called
" once for every line, which leads to multiple splits created).
function! WinMove(key) range
let t:curwin = winnr()
exec "wincmd ".a:key
if (t:curwin == winnr()) "we havent moved
if (match(a:key,'[jk]')) "were we going up/down
wincmd v
else
wincmd s
endif
exec "wincmd ".a:key
endif
endfunction
" See also an alternative approach to windows navigation
" using submode plugin: http://ddrscott.github.io/blog/2016/making-a-window-submode/
" The plugin: https://github.com/kana/vim-submode
" Basically, the idea is that Ctrl-W starts the sumbode and then you just
" use h/j/k/l and other keys without prefixing each one with Ctrl-W
" move to and open if not exists
map <c-j> :call WinMove('j')<CR>
map <c-k> :call WinMove('k')<CR>
map <c-l> :call WinMove('l')<CR>
map <c-h> :call WinMove('h')<CR>
if has('nvim')
" Testing, map jk/kj as Esc
" Map Ctrl + h,j,k,l to move between windows
" And map Alt + h,j,k,l for the same
" now it is possible to map Alt in terminal
" see also :help nvim-terminal-emulator
:tnoremap jk <C-\><C-n>
:tnoremap kj <C-\><C-n>
:tnoremap <C-h> <C-\><C-n>:call WinMove('h')<CR>
:tnoremap <C-j> <C-\><C-n>:call WinMove('j')<CR>
:tnoremap <C-k> <C-\><C-n>:call WinMove('j')<CR>
:tnoremap <C-l> <C-\><C-n>:call WinMove('j')<CR>
:tnoremap <A-h> <C-\><C-n>:call WinMove('h')<CR>
:tnoremap <A-j> <C-\><C-n>:call WinMove('j')<CR>
:tnoremap <A-k> <C-\><C-n>:call WinMove('j')<CR>
:tnoremap <A-l> <C-\><C-n>:call WinMove('j')<CR>
:nnoremap <A-h> :call WinMove('h')<CR>
:nnoremap <A-j> :call WinMove('j')<CR>
:nnoremap <A-k> :call WinMove('k')<CR>
:nnoremap <A-l> :call WinMove('l')<CR>
endif
"rotate
map <leader>wr <C-W>r
"Shift arrows to resize
nmap <S-left> :3wincmd <<cr>
nmap <S-right> :3wincmd ><cr>
nmap <S-up> :3wincmd +<cr>
nmap <S-down> :3wincmd -<cr>
"arrows to resize
nmap <left> gT
nmap <right> gt
nmap <up> :tablast <cr>
nmap <down> 1gt
"move windows
map <Leader>h :wincmd H<cr>
map <Leader>k :wincmd K<cr>
map <Leader>l :wincmd L<cr>
map <Leader>j :wincmd J<cr>
" format json, see https://coderwall.com/p/faceag
" :%!python -m json.tool
" or with jq http://stedolan.github.io/jq/
" download, mv to ~/bin/jq, chmod +x ~/bin/jq
" :%!jq '.'
" }}}
" Tabs navigation {{{
" New tab and close tab
nnoremap <Leader>tn :tabnew<cr>
nnoremap <Leader>tc :tabclose<cr>
" " Go to tab by number
" noremap <leader>1 1gt
" noremap <leader>2 2gt
" noremap <leader>3 3gt
" noremap <leader>4 4gt
" noremap <leader>5 5gt
" noremap <leader>6 6gt
" noremap <leader>7 7gt
" noremap <leader>8 8gt
" noremap <leader>9 :tablast<cr>
" Go to last active tab
" Note: would be good to have i3-like benavior, where using
" the same number switches to the previous workspace if we
" are on that number already, for example,
" I am on workspace 3 then win+1 will go to space 1 and
" win+1 again will go back to space 3.
au TabLeave * let g:lasttab = tabpagenr()
nnoremap <leader>0 :exe "tabn ".g:lasttab<cr>
vnoremap <leader>0 :exe "tabn ".g:lasttab<cr>
" Native files / buffers navigation
" Also need `set path=.,**` (see below) to search recursively
" See:http://www.reddit.com/r/vim/comments/2ueu0g/which_pluginskeybindsetc_significantly_changed/
" noremap <Leader>i :find<SPACE>
" noremap <Leader>I :find <C-R>=expand('%:p:h').'/**/*'<CR>
" Insert current file name
cnoremap %% <C-r>=expand('%')<CR>
" Insert current file dir
"cabbr <expr> %% expand('%:p:h')
" cnoremap %%f <C-r>=expand('%:p:h')<CR>
cnoremap %%f <C-R>=fnameescape(expand('%:h')).'/'<CR>
" list buffers and start completion
nnoremap <Leader>b :buffer <C-z>
" list buffers and start completion, will open in split
nnoremap <Leader>B :sbuffer <C-z>
" list buffers with ls and start completion
" see: http://www.reddit.com/r/vim/comments/2ueu0g/which_pluginskeybindsetc_significantly_changed/
nnoremap gb :ls<CR>:buffer<Space>
nnoremap gB :ls<CR>:sbuffer<Space>