-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path.vimrc.tools
More file actions
424 lines (383 loc) · 14.9 KB
/
.vimrc.tools
File metadata and controls
424 lines (383 loc) · 14.9 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
" Requires Nvim 0.5, can be run via docker:
" docker run -it --rm brandoncc/vim-be-good:stable
" Vim games, :VimBeGood
Plug 'ThePrimeagen/vim-be-good'
Plug 'jamessan/vim-gnupg'
" Open files from terminal buffer in current nvim instance.
" Plug 'willothy/flatten.nvim'
" g:unception_open_buffer_in_new_tab = 1
"
" Note: we can get back to terminal after opening the file with Ctrl-O or
" Ctrl-^ (Ctrl-6).
" - It is also an option to setup the autocommand so files will be opened
" in splits and the terminal stays open, see
" https://github.com/samjwill/nvim-unception/issues/61
"
" Note: to have normal git flow, also needs the following in .gitconfig:
" [core]
" editor = nvim --cmd 'let g:unception_block_while_host_edits=1'
Plug 'samjwill/nvim-unception'
if has('nvim')
" terminal scrollback buffer size
" set scrollback=100000
" Find terminal job_id
function! FindOrCreateTerminalJob() abort
let term_buffer_id = -1
let term_job_id = 0
for visible_buffer_id in tabpagebuflist()
try
let term_job_id = nvim_buf_get_var(visible_buffer_id, 'terminal_job_id')
let term_buffer_id = visible_buffer_id
break
catch
let term_job_id = 0
endtry
endfor
"if term_job_id == 0
" echoerr "No terminal job found"
" return
"endif
if term_job_id == 0
new
let term_job_id = termopen($SHELL)
endif
return term_job_id
endfunction
" Maps special keys like <CR>, <Esc>, etc to terminal codes
" This way we can have something like
" <Esc> that will be sent to the terminal as actual Esc key.
" See the list of special keys in :help keycodes
function! MapSpecialKey(key) abort
" Convert Vim-style key notation (<C-c>, <Esc>, etc.) into terminal codes
return nvim_replace_termcodes(a:key, v:true, v:false, v:true)
" Note that the special key should be the only thing on the line,
" something like
" > print('Hello')<Esc>
" will not work.
" But
" > print('Hello')
" > <Esc>
" will work.
"let keymap = {
" \ '<Esc>': "\x1b",
" \ '<C-c>': "\x03",
" \ '<C-d>': "\x04",
" \ '<CR>': "\n",
" \ }
"return get(keymap, a:key, a:key) " return raw string if not in map
endfunction
" Open terminal with ":term" or ":term ipython" (to run the specific command).
" Create the new split and enter / edit the commands there, send to
" terminal with "Enter" (current line or visual selection).
function! SendCommand(commands) abort
" Send the command to the first terminal buffer found in current tab.
" If there is no terminal - splits the current window and runs terminal
" there.
let commands = a:commands
let term_job_id = FindOrCreateTerminalJob()
" This allows using special keys like <CR>, <Esc>, etc from the
" MapSpecialKey.
let cmds = map(copy(a:commands), 'MapSpecialKey(v:val)')
call jobsend(term_job_id, cmds)
endfunction
function! SendCommandWithEnter(commands) abort
" jobsend interprets '' as new line, same as "\n" or enter
call SendCommand(add(a:commands, ''))
" Note that special key should also work
" call SendCommand(add(a:commands, '<CR>'))
" but the version above works even without the MapSpecialKey function.
endfunction
function! SendCommandCurrentLine()
" Send current line to terminal buffer.
let commands = [getline('.')]
call SendCommandWithEnter(commands)
endfunction
function! GetVisualSelection()
" Returns visual selection as list of lines.
" See https://stackoverflow.com/a/6271254/4612064
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
let lines = getline(line_start, line_end)
if len(lines) == 0
return []
endif
let lines[-1] = lines[-1][: column_end - 2]
let lines[0] = lines[0][column_start - 1:]
return lines
endfunction
function! SendCommandVisualSelection()
let commands = GetVisualSelection()
call SendCommandWithEnter(commands)
endfunction
function! SendCommandVisualSelectionAsLine()
let commands = [join(GetVisualSelection(), " ")]
call SendCommandWithEnter(commands)
endfunction
" At the moment this is the same as SendCommand, but
" maybe useful if I find that automatic handling for special keys
" does not work well.
" Then I can revert this to send keys explicitely.
function! SendKey(key) abort
call SendCommand([a:key])
"" Map friendly names to control codes
"let keymap = {
" \ 'esc': "\x1b",
" \ 'ctrl-c': "\x03",
" \ 'ctrl-d': "\x04",
" \ 'enter': "\n",
" \ }
"
" let term_job_id = FindOrCreateTerminalJob()
"if has_key(keymap, a:key)
" call jobsend(term_job_id, [keymap[a:key]])
"else
" echoerr "Unknown key: " . a:key
"endif
endfunction
" <CR> is mapped to folds open / close by default
" to enable term mappings, do :SetupTerm before using them
" autocmd FileType c,cpp,php,python,javascript,sql,sh nnoremap <buffer> <CR> :<C-u>call SendCommandCurrentLine()<CR>
" autocmd FileType c,cpp,php,python,javascript,sql,sh xnoremap <buffer> <CR> :<C-u>call SendCommandVisualSelection()<CR>
" " Useful in ipdb where multi-line input is not supported, this mapping can
" " be used to send multi-line statement,
" " see also discussion here https://github.com/randy3k/SendCode/issues/39,
" " this is what they call "fake multi-line" mode:
" autocmd FileType c,cpp,php,python,javascript,sql xnoremap <buffer> <leader><CR> :<C-u>call SendCommandVisualSelectionAsLine()<CR>
" Do :TermSetup to enable terminal mappings for "Enter" (used for folds by
" default)
function! SetupTermMappings()
nnoremap <buffer> <CR> :<C-u>call SendCommandCurrentLine()<CR>
xnoremap <buffer> <CR> :<C-u>call SendCommandVisualSelection()<CR>
" Useful in ipdb where multi-line input is not supported, this mapping can
" be used to send multi-line statement,
" see also discussion here https://github.com/randy3k/SendCode/issues/39,
" this is what they call "fake multi-line" mode:
xnoremap <buffer> <leader><CR> :<C-u>call SendCommandVisualSelectionAsLine()<CR>
endfunction
command! TermSetup :call SetupTermMappings()
endif
" Filebrowser: vifm
" :EditVifm - select a file or files to open in the current buffer
" :Vifm - alias for :EditVifm to be used like :vert Vifm
" :PeditVifm - select a file in preview window
" :SplitVifm - split buffer and select a file or files to open
" :VsplitVifm - vertically split buffer and select a file or files to open
" :DiffVifm - select a file or files to compare to the current file with :vert diffsplit
" :TabVifm - select a file or files to open in tabs
" :VifmCs - attempts to convert Vim's color scheme to Vifm's one
Plug 'vifm/vifm.vim'
" Filebrowser: oil.nvim
" Allows to browse and edit the filesytem.
" The initialization is in .vimrc, after the plug#end()
Plug 'stevearc/oil.nvim'
" Filebrowser: vim-vinegar + netrw
" Plug 'tpope/vim-vinegar'
" Temporary switch to my fork to test the change for the
" search issue
" https://github.com/serebrov/vim-vinegar/tree/136-fix-search-in-vinegar-up
" Plug 'serebrov/vim-vinegar', {'branch': '136-fix-search-in-vinegar-up'}
" '-' to open it and to go up dir
" I - toggle the usage information
" . - put the file name at the end of a : command line, for example .!chmod +x
" ! to do the same and start command line with !
" y. - yank the absolute path of the file under the cursor
" ~ - go home
"
" CTRL-^ - switch to the previous buffer (vim feature)
"
" Netrw features:
" s - change sort order (name, time, size)
" r - reverse sort order
" i - thin, long, wide, tree listing
" gh - toggle dot files hiding
" % - create new file
" a - normal / hiding / show all
" d - create dir
" D - delete dir or file
" R - rename dir or file
"
" u / U - go back to the previous/next dir
"
" Marking files:
"
" mf / mF - mark/unmark file
" mc - copy marked files to the target dir
" mm - move marked files to the target dir
" me - edit marked files
" md - diff marked files
" mr - mark files with a regexp
" mt - current direction becomes the target dir
" mu - unmark all
" mv - apply vim command to marked files
" mx - apply shell command to marked files
" mX - apply shell command to marked files, en bloc (?)
" mz - compress / decompress marked files
" :MF *.c - mark all *.c files
"
" Note: operations on marked files are a bit strange and
" require a specific order of actions:
" 1. mark files with mf in the source dir
" 2. go to the target dir, use mt to make it the target dir
" 3. go back to the source dir
" 4. use mc or mm to copy or move files
" If we skip step 3 then it will not work, the error message is:
" **error** (netrw) there are no marked files in this window (:help netrw-mf)
"
" Bookmarks:
" mb - bookmark current dir
" gb - go to most recent bookmarked dir
" qb - list bookmarks and history (not it is small Q, not G)
"
" See also :help netrw, many other features
"
"" use Leader-r to refresh (default is Ctrl-L which is used to jump
"" to the left window)
nnoremap <Leader>r <Plug>NetrwRefresh
" Copy files to another netwr window (split):
" Find another netwr widnow, switch to it, use mt to mark it as a target,
" then go back to the source window and use mc to copy files.
function! NetrwRunMarkCommand(cmd)
" Find another vim window with netrw
let l:curwin = winnr()
let l:curbuf = bufnr('%')
" Find another window with netrw filetype in the same tab.
let l:targetbufid = -1
for l:bufid in tabpagebuflist()
if l:bufid == l:curbuf
continue
endif
if getbufvar(l:bufid, '&filetype') == 'netrw'
echo 'Found target window: ' . bufname(l:targetbufid)
let l:targetbufid = l:bufid
" Mark another window as a target.
call netrw#MakeTgt(bufname(l:bufid))
break
endif
endfor
if l:targetbufid == -1
echoerr 'No other netrw window found'
return
endif
echo 'Found target window: ' . bufname(l:targetbufid)
" Execute the command in the source window
execute 'normal ' . a:cmd
echo 'Executed command: ' . a:cmd
endfunction
function! NetrwMapping()
" Note: the autocommand does not work for some reason, so we use the global
" mapping above.
"" use Leader-r to refresh (default is Ctrl-L which is used to jump
"" to the left window)
" nnoremap <buffer> <Leader>r <Plug>NetrwRefresh
"
" Mark files with space.
nmap <buffer> <Space> mfj
" See https://vonheikemen.github.io/devlog/tools/using-netrw-vim-builtin-file-explorer/
" Show a list of marked files
nmap <buffer> fl :echo join(netrw#Expose("netrwmarkfilelist"), "\n")<CR>
" Show a target dir (it is also shown in the netrw banner,
" "Copy/Move Tgt: ..."
nmap <buffer> fq :echo 'Target:' . netrw#Expose("netrwmftgt")<CR>
" Copy files to another netwr window (split):
" Find another netwr widnow, switch to it, use mt to mark it as a target,
" then go back to the source window and use mc to copy files.
nmap <buffer> fc :call NetrwRunMarkCommand('mc')<CR>
nmap <buffer> q :q<CR>
nmap <buffer> <Leader>q :q<CR>
endfunction
augroup netrw_mapping
autocmd!
autocmd filetype netrw call NetrwMapping()
augroup END
" Filebrowser: filebeagle
" '-' to open it and to go up dir
" R - refresh;
" f - define the filter for filenames; F - toggle filter;
" gh - toggle hide/show wildeignored files
" <BS> - go back in dirs history
" q - close
" ~ - go home
" + / % - create file
" :ClipPathname / :ClipDirname - copy the full path of the selected file / current dir
" Plug 'jeetsukumaran/vim-filebeagle'
" let g:filebeagle_suppress_keymaps = 1
" let g:filebeagle_show_hidden = 1
" let g:filebeagle_check_gitignore = 1
" " map <silent> <Leader>f <Plug>FileBeagleOpenCurrentWorkingDir
" map <silent> - <Plug>FileBeagleOpenCurrentBufferDir
" using vim-plug
" Plug 'mcchrish/nnn.vim'
" Dirvish
"
" The '-' mapping is default: open current file dir,
" in file browser goes up one dir.
"
" The filebrowser buffer is editable, but changes are not
" saved. Refresh with R.
"
" File operations can be done with :Shdo, use '.' mapping
" in filebrowser to stat the :Shdo command.
" In the Shdo buffer - edit the shell script and apply with Z!.
"
" For example: select files, then :Shdo mv {} {} - rename files.
" Also '%' represents the current directory, so
" :edit %/new_file.txt in command line will create a new file.
" Plug 'justinmk/vim-dirvish'
" Show directories at the top
let g:dirvish_mode = ':sort ,^.*[\/],'
" How to expand a directory inline (like a tree-style view)?
" There is a hacky solution. Put this in your vimrc and hit "t" on a directory:
" augroup dirvish_config
" autocmd!
" autocmd FileType dirvish
" \ nnoremap <silent><buffer> t ddO<Esc>:let @"=substitute(@", '\n', '', 'g')<CR>:r ! find "<C-R>"" -maxdepth 1 -print0 \| xargs -0 ls -Fd<CR>:silent! keeppatterns %s/\/\//\//g<CR>:silent! keeppatterns %s/[^a-zA-Z0-9\/]$//g<CR>:silent! keeppatterns g/^$/d<CR>:noh<CR>
" augroup END
"
" " Extra commands on top of dirvish
" " Create file a <Plug>(dovish_create_file)
" " Create directory A <Plug>(dovish_create_directory)
" " Delete under cursor dd <Plug>(dovish_delete)
" " Rename under cursor r <Plug>(dovish_rename)
" " Yank under cursor (or visual selection) yy <Plug>(dovish_yank)
" " Copy file to current directory pp <Plug>(dovish_copy)
" " Move file to current directory PP <Plug>(dovish_move)
" Plug 'roginfarrer/vim-dirvish-dovish', {'branch': 'main'}
" let g:broot_default_conf_path = $HOME . '/.config/broot/conf.hjson'
" Plug 'lstwn/broot.vim'
" map <silent> - :BrootCurrentDir<CR>
" noremap <Leader>f :BrootWorkingDir<CR>
" Suggest to open existing file instead of creating new one when there
" are multiple matches
" allow using fzf
let g:dym_use_fzf = 1
Plug 'EinfachToll/DidYouMean'
" Ensure dir exists before save the file
" :e some_new_dir/some_new_file and then :w will work
Plug 'dockyard/vim-easydir'
" Auto CD to project root
" Plug 'airblade/vim-rooter'
" disables certain vim features to speedup large file editing
" g:LargeFile (by default, its 100) - 100Mb
Plug 'vim-scripts/LargeFile'
"""""" Commands
"Vim sugar for the UNIX shell commands that need it the most. Commands
"include:
" :Unlink: Delete a buffer and the file on disk simultaneously.
" :Remove: Like :Unlink, but doesn't require a neckbeard.
" :Move: Rename a buffer and the file on disk simultaneously.
" :Chmod: Change the permissions of the current file.
" :Find: Run find and load the results into the quickfix list.
" :Locate: Run locate and load the results into the quickfix list.
" :SudoWrite: Write a privileged file with sudo.
" :W: Write every open window. Handy for kicking off tools like guard.
Plug 'tpope/vim-eunuch'
" Password generator, :echo Password() or Ctrl-R=Password()
function! RandNum() abort
return str2nr(matchstr(reltimestr(reltime()), '\.\zs\d*'))
endfunction
function! RandChar() abort
return nr2char((RandNum() % 93) + 33)
endfunction
function! Password() abort
return join(map(range(8), 'RandChar()'), '')
endfunction