Repository navigation
Expand file tree
/
Copy pathkeybinds.vim
More file actions
393 lines (302 loc) · 10.5 KB
/
Copy pathkeybinds.vim
File metadata and controls
393 lines (302 loc) · 10.5 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
function! startup#keybinds#TryToOpenLink() abort
let l:pos = getcurpos()
let l:lnum = l:pos[1]
let l:col = l:pos[2]
let l:line = getline(l:lnum)
" TODO: handle multiple lines on a single line better.
let l:index = match(l:line[0 : l:col + len('https://')], '\vhttps?://')
if l:index >= 0 && l:index < l:col
let l:link = matchstr(l:line[l:index :], '\v[^ )]+')
" Remove CR characters from links.
let l:link = substitute(l:link, '\r', '', 'g')
call job_start(['xdg-open', l:link])
return 1
endif
return 0
endfunction
" This script holds all keybinding settings.
nnoremap <silent> <C-LeftMouse> <LeftMouse> :call startup#keybinds#TryToOpenLink()<CR>
nnoremap <silent> <C-9> :call startup#keybinds#TryToOpenLink()<CR>
nnoremap <C-RightMouse> <Nop>
" Disable replace mode, which turns on in bad terminals for some reason.
nnoremap R <Nop>
" CTRL-A is Select all
" This works better than the default Windows script.
noremap <C-A> ggVG
inoremap <C-A> <Esc>ggvG
vnoremap <C-A> <Esc>ggVG
" Make double-tapping ESC in normal mode quit a file.
nnoremap <ESC><ESC> :q<CR>
" Triple-tapping ESC forces quitting.
nnoremap <ESC><ESC> :q!<CR>
" Make Shift+V switch from selection to visual line mode on Windows, etc.
snoremap V <ESC>gvV
" CTRL-C is copy to the clipboard
vnoremap <C-C> y
snoremap <C-C> <ESC>gv"+y<ESC>
" CTRL-X is cut to the clipboard
vnoremap <C-X> "+x
" CTRL-V pastes from the clipboard
noremap <C-v> p
inoremap <C-v> <C-r><C-o>+
cnoremap <C-v> <C-R>+
" CTRL-P replaces the current line with the buffer without cutting it.
noremap <C-p> "_ddP
" p in visual mode shouldn't cut the text we replace.
vnoremap <p> "_dP
" CTRL-S Saves the file.
noremap <silent> <C-S> :w<CR>
vnoremap <silent> <C-S> <C-C>:w<CR>
inoremap <silent> <C-S> <C-C>:w<CR>
" SHIFT-S Saves the file.
noremap <silent> S :w<CR>
" Quit files by typing fjfj quickly, which requires less stretching.
noremap fjfj :q<CR>
" Make using Ctrl+C do the same as Escape, to trigger autocmd commands
inoremap <C-c> <Esc>
" Bind gV so we can re-select pasted text.
nnoremap <expr> gV "`[".getregtype(v:register)[0]."`]"
" Bind Ctrl + Tab goes forward
noremap <C-Tab> :tabn <Return>
tnoremap <C-Tab> <C-w>N:tabn<Return>
" Bind Ctrl + Shift + Tab goes back
noremap <C-S-Tab> :tabp <Return>
tnoremap <C-S-Tab> <C-w>N:tabp<Return>
" Ctrl + h goes to the tab to the left.
noremap <C-h> :tabp <Return>
" Ctrl + l goes to the tab to the right.
noremap <C-l> :tabn <Return>
" Bind Ctrl + t to opening new tabs.
noremap <C-t> :tabnew <Return>
" Ctrl + j goes down a window.
noremap <C-j> <C-w>j
" Ctrl + k goes up a window.
noremap <C-k> <C-w>k
" Make Ctrl+B do exactly the same thing as Ctrl+U.
nnoremap <C-B> <C-U>
" Map Ctrl-B to delete to the end of line in insert mode.
inoremap <C-b> <Esc>lDa
" Movement left and right in insert mode with Ctrl.
inoremap <C-l> <Esc>la
inoremap <C-h> <Esc>i
" Disable Ex mode, because fuck Ex mode.
noremap Q <Nop>
" Use semicolons for what colon does.
noremap ; :
" Bind keys for moving between warnings.
nmap <silent> , <Plug>(ale_previous_wrap)
nmap <silent> m <Plug>(ale_next_wrap)
" Bind keys for going to next and previous from quickfix
noremap cp :cprev<Return>
noremap cn :cnext<Return>
" Use Ctrl+Tab to autocomplete in all filetypes.
imap <silent> <C-Tab> <Plug>(ale_complete)
" --- Function key bindings in order ---
function! startup#keybinds#RedrawSearch() abort
if get(g:, 'f3_redraw') is# 'ale'
ALERepeatSelection
else
copen
endif
endfunction
function! startup#keybinds#SwitchToProjectRoot() abort
let s:old_search_cwd = getcwd()
let l:dir = file_search#FindProjectRoot()
if !empty(l:dir)
execute 'cd ' . fnameescape(l:dir)
return 1
endif
return 0
endfunction
function! startup#keybinds#SwitchBackToOldCwd() abort
execute 'cd ' . fnameescape(s:old_search_cwd)
endfunction
function! startup#keybinds#PerformGrep(cleaned_args) abort
let g:f3_redraw = 'grep'
let g:f3_redraw_repeat_search = a:cleaned_args
if startup#keybinds#SwitchToProjectRoot()
execute 'silent grep! ' . a:cleaned_args
cwindow
endif
call startup#keybinds#SwitchBackToOldCwd()
endfunction
function! startup#keybinds#Grep(original_pattern) abort
let l:pattern = a:original_pattern
" Try to match strings like ["foo"] and ['bar']
let l:key_match = matchlist(l:pattern, '\v^\[(\s*[''"][^''"]+[''"]\s*)\]$')
if !empty(l:key_match)
" Replace the above example if provided,
" with an exact search for the above, not a very expensive [] search.
let l:pattern = '\[' . l:key_match[1] . '\]'
endif
let l:escaped_pattern = escape(shellescape(l:pattern), '#|%')
call startup#keybinds#PerformGrep(l:escaped_pattern)
endfunction
function! startup#keybinds#RepeatGrep() abort
if exists('g:f3_redraw_repeat_search')
call startup#keybinds#PerformGrep(g:f3_redraw_repeat_search)
else
echoerr 'No search to repeat'
endi
endfunction
command! -nargs=+ Grep call startup#keybinds#Grep(<q-args>)
" Bind F1 to showing details of ALE errors.
noremap <F1> :ALEDetail<CR>
" Toggle the project NERD tree with F2
noremap <F2> :ToggleNERDTree<CR>
" Search for files in the project with Ctrl+H
noremap <F3> :call startup#keybinds#RedrawSearch()<CR>
" Map F4 to copying the current filename to the clipboard.
noremap <F4> :call startup#keybinds#CopyNameToClipboard()<CR>
nmap <F5> <Plug>(ale_find_references)
" Use Vim's built in grep with ripgrep to search in files.
noremap <C-f> :Grep<Space>
" Vim on Windows can't tell Ctrl+Shift+f from Ctrl+f, so mapping it there
" would replace the Ctrl+f mapping above.
if !has('gui_macvim') && !has('win32')
" Ctrl+shift+f repeats the last search.
noremap <C-s-f> :call startup#keybinds#RepeatGrep()<Return>
endif
" Use ripgrep to fuzzy find files.
noremap <C-p> :call file_search#OpenNewSearch()<Return>
" Bind F7 To recording speech with vim-speech.
" noremap <silent> <F7> :SpeechToggle<CR>
" Bind F8 to fixing problems with ALE
nmap <F8> <Plug>(ale_fix)
inoremap <F8> <C-c><Plug>(ale_fix)
" Use Ctrl+y to go to the definition of something.
nmap <C-y> <Plug>(ale_go_to_definition)
" Use Ctrl+u to go to the type definition of something.
nmap <C-u> <Plug>(ale_go_to_type_definition)
if has('syntax')
" Use F12 to resync syntax from the start.
noremap <F12> <Esc>:syntax sync fromstart<CR>
inoremap <F12> <C-o>:syntax sync fromstart<CR>
endif
function! startup#keybinds#CopyNameToClipboard() abort
let l:filename = expand('%:p')
" Look through a configured array of prefixes to remove, and remove
" them from the filename if any match.
for l:regex in g:path_remove_regex_list
let [l:match, l:start, l:end] = matchstrpos(l:filename, l:regex)
if !empty(l:match)
let l:filename = l:start > 0
\ ? l:filename[: l:start - 1] . l:filename[l:end :]
\ : l:filename[l:end :]
break
endif
endfor
let l:text = l:filename
if &filetype =~# 'python'
let l:info = python_tools#cursor_info#GetInfo()
if !empty(get(l:info, 'class', ''))
let l:text .= '::' . l:info.class
endif
if !empty(get(l:info, 'def', ''))
let l:text .= '::' . l:info.def
endif
endif
let @+ = l:text
echo 'Filename and function name copied to clipboard'
endfunction
" Make pressing Enter accept a completion entry.
function! SmartEnter()
if pumvisible()
return "\<C-y>"
endif
return "\<CR>"
endfunction
" Return 1 if the current line is a markdown list line.
" Return 0 if the filtype is not markdown, configuration, plaintext.
"
" Matches:
" * item
" - item
" * item
" - item
" 1. item
" 10. item
function! startup#keybinds#IsMarkdownListLine() abort
if &filetype =~# '^\(conf\|markdown\|\)$'
let l:line = getline('.')
return l:line =~# '^\s*\([*-]\|\d\+\.\)\%(\s\|$\)'
endif
return 0
endfunction
" Use Tab and Shift+Tab for either completion or SnipMate.
function! SmartTab() abort
if pumvisible()
let l:keys = "\<C-n>"
if get(b:, 'ale_last_completion_count') is 1
let l:keys .= "\<Left>\<Right>"
endif
return l:keys
endif
" Indent if we're on a markdown list line.
if startup#keybinds#IsMarkdownListLine()
return "\<C-o>>>\<End>"
endif
return snipMate#TriggerSnippet()
endfunction
function! SmartShiftTab() abort
if pumvisible()
return "\<C-p>"
endif
" Dedent if we're on a markdown list line.
if startup#keybinds#IsMarkdownListLine()
return "\<C-o><<\<End>"
endif
return snipMate#BackwardsSnippet()
endfunction
function! SmartInsertCompletion() abort
if pumvisible()
return "\<C-n>"
endif
return "\<C-c>a\<C-n>"
endfunction
inoremap <silent> <CR> <C-R>=SmartEnter()<CR>
inoremap <silent> <Tab> <C-R>=SmartTab()<CR>
inoremap <silent> <S-Tab> <C-R>=SmartShiftTab()<CR>
inoremap <silent> <C-n> <C-R>=SmartInsertCompletion()<CR>
" Use Ctrl+n to prompt Neural in normal mode
noremap <C-n> <Plug>(neural_prompt)
" Use Ctrl+n in Visual mode to explain code.
vnoremap <C-n> <Plug>(neural_explain)
" Close split windows just by pressing 'q', but record macros if there is only
" one window open.
"
" I never record macros while working with split windows.
function! startup#keybinds#CloseSplitWindowsWithQ() abort
let l:tab_info = gettabinfo(tabpagenr())[0]
if len(l:tab_info.windows) > 1
" Close the terminal window instead of the current one.
for l:win_id in l:tab_info.windows
let l:bufnr = getwininfo(l:win_id)[0].bufnr
let l:bufname = expand('#' . l:bufnr . ':p')
if l:bufname =~# '^!/bin'
execute 'bwipeout ' . l:bufnr
return
endif
endfor
:q
else
call feedkeys('q', 'n')
endif
endfunction
nnoremap <silent> q :call startup#keybinds#CloseSplitWindowsWithQ()<CR>
" Bind // so it sets up an expression for replacing the previous match.
noremap // :%s//
noremap <silent> gs :SplitjoinSplit<CR>:ALEFix<CR>
noremap <silent> gj :SplitjoinJoin<CR>
" Quit terminals with just 'q'
tnoremap q <C-w>N:q!<CR>
function! startup#keybinds#RunTests() abort
if !exists('b:test_command')
echom 'b:test_command is not defined'
return
endif
belowright call term_start(['/bin/sh', '-c', b:test_command])
endfunction
noremap <silent> <F9> :call startup#keybinds#RunTests()<CR>
function! startup#keybinds#Init() abort
endfunction