forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmappings.vim
348 lines (281 loc) · 9.95 KB
/
mappings.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
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
" Key-mappings
"---------------------------------------------------------
" Non-standard {{{
" ------------
" Window-control prefix
nnoremap [Window] <Nop>
nmap s [Window]
" Fix keybind name for Ctrl+Spacebar
map <Nul> <C-Space>
map! <Nul> <C-Space>
" Disable arrow movement, resize splits instead.
if get(g:, 'elite_mode')
nnoremap <Up> :resize +2<CR>
nnoremap <Down> :resize -2<CR>
nnoremap <Left> :vertical resize +2<CR>
nnoremap <Right> :vertical resize -2<CR>
endif
" Double leader key for toggling visual-line mode
nmap <silent> <Leader><Leader> V
vmap <Leader><Leader> <Esc>
" Change current word in a repeatable manner
nnoremap cn *``cgn
nnoremap cN *``cgN
" Change selected word in a repeatable manner
vnoremap <expr> cn "y/\\V\<C-r>=escape(@\", '/')\<CR>\<CR>" . "``cgn"
vnoremap <expr> cN "y/\\V\<C-r>=escape(@\", '/')\<CR>\<CR>" . "``cgN"
nnoremap cp yap<S-}>p
nnoremap <leader>a =ip
" xnoremap p "0p
" nnoremap x "_x
" Toggle fold
nnoremap <CR> za
" Focus the current fold by closing all others
nnoremap <S-Return> zMza
" Use backspace key for matchit.vim
nmap <BS> %
xmap <BS> %
nmap <Tab> <C-w>w
nmap <S-Tab> <C-w>W
"}}}
" Global niceties {{{
" ---------------
" Start an external command with a single bang
nnoremap ! :!
" Allow misspellings
cnoreabbrev qw wq
cnoreabbrev Wq wq
cnoreabbrev WQ wq
cnoreabbrev Qa qa
cnoreabbrev Bd bd
cnoreabbrev bD bd
" Start new line from any cursor position
inoremap <S-Return> <C-o>o
" Quick substitute within selected area
xnoremap s :s//g<Left><Left>
nnoremap zl z5l
nnoremap zh z5h
" Improve scroll, credits: https://github.com/Shougo
nnoremap <expr> zz (winline() == (winheight(0)+1) / 2) ?
\ 'zt' : (winline() == 1) ? 'zb' : 'zz'
noremap <expr> <C-f> max([winheight(0) - 2, 1])
\ ."\<C-d>".(line('w$') >= line('$') ? "L" : "M")
noremap <expr> <C-b> max([winheight(0) - 2, 1])
\ ."\<C-u>".(line('w0') <= 1 ? "H" : "M")
noremap <expr> <C-e> (line("w$") >= line('$') ? "j" : "3\<C-e>")
noremap <expr> <C-y> (line("w0") <= 1 ? "k" : "3\<C-y>")
" Window control
nnoremap <C-q> <C-w>
nnoremap <C-x> <C-w>x
nnoremap <silent><C-w>z :vert resize<CR>:resize<CR>:normal! ze<CR>
" Select blocks after indenting
xnoremap < <gv
xnoremap > >gv|
" Use tab for indenting in visual mode
vnoremap <Tab> >gv|
vnoremap <S-Tab> <gv
nnoremap > >>_
nnoremap < <<_
" Select last paste
nnoremap <expr> gp '`['.strpart(getregtype(), 0, 1).'`]'
" Navigation in command line
cnoremap <C-j> <Left>
cnoremap <C-k> <Right>
cnoremap <C-h> <Home>
cnoremap <C-l> <End>
cnoremap <C-f> <Right>
cnoremap <C-b> <Left>
cnoremap <C-d> <C-w>
" Switch history search pairs, matching my bash shell
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <Up> <C-p>
cnoremap <Down> <C-n>
" }}}
" File operations {{{
" ---------------
" When pressing <leader>cd switch to the directory of the open buffer
map <Leader>cd :lcd %:p:h<CR>:pwd<CR>
" Fast saving
nnoremap <silent><Leader>w :write<CR>
vnoremap <silent><Leader>w <Esc>:write<CR>
nnoremap <silent><C-s> :<C-u>write<CR>
vnoremap <silent><C-s> :<C-u>write<CR>
cnoremap <silent><C-s> <C-u>write<CR>
" Save a file with sudo
" http://forrst.com/posts/Use_w_to_sudo_write_a_file_with_Vim-uAN
cmap W!! w !sudo tee % >/dev/null
" }}}
" Editor UI {{{
" ---------
" I like to :quit with 'q', shrug.
nnoremap <silent> q :<C-u>:quit<CR>
autocmd MyAutoCmd FileType man nnoremap <silent><buffer> q :<C-u>:quit<CR>
" Macros
nnoremap Q q
nnoremap gQ @q
" Show highlight names under cursor
nmap <silent> gh :echo 'hi<'.synIDattr(synID(line('.'), col('.'), 1), 'name')
\.'> trans<'.synIDattr(synID(line('.'), col('.'), 0), 'name').'> lo<'
\.synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name').'>'<CR>
" Toggle editor visuals
nmap <silent> <Leader>ts :setlocal spell!<cr>
nmap <silent> <Leader>tn :setlocal nonumber!<CR>
nmap <silent> <Leader>tl :setlocal nolist!<CR>
nmap <silent> <Leader>th :nohlsearch<CR>
nmap <silent> <Leader>tw :setlocal wrap! breakindent!<CR>
" Tabs
nnoremap <silent> g0 :<C-u>tabfirst<CR>
nnoremap <silent> g$ :<C-u>tablast<CR>
nnoremap <silent> gr :<C-u>tabprevious<CR>
nnoremap <silent> <A-j> :<C-U>tabnext<CR>
nnoremap <silent> <A-k> :<C-U>tabprevious<CR>
nnoremap <silent> <C-Tab> :<C-U>tabnext<CR>
nnoremap <silent> <C-S-Tab> :<C-U>tabprevious<CR>
" Uses g:lasttab set on TabLeave in MyAutoCmd
let g:lasttab = 1
nmap <silent> \\ :execute 'tabn '.g:lasttab<CR>
" }}}
" Totally Custom {{{
" --------------
" Remove spaces at the end of lines
nnoremap <silent> ,<Space> :<C-u>silent! keeppatterns %substitute/\s\+$//e<CR>
" C-r: Easier search and replace
xnoremap <C-r> :<C-u>call <SID>get_selection('/')<CR>:%s/\V<C-R>=@/<CR>//gc<Left><Left><Left>
" Returns visually selected text
function! s:get_selection(cmdtype) "{{{
let temp = @s
normal! gv"sy
let @/ = substitute(escape(@s, '\'.a:cmdtype), '\n', '\\n', 'g')
let @s = temp
endfunction "}}}
" Background dark/light toggle and contrasts
nnoremap <silent><Leader>b :<C-u>call <SID>toggle_background()<CR>
nmap <silent> s- :<c-u>call <SID>toggle_contrast(-v:count1)<cr>
nmap <silent> s= :<c-u>call <SID>toggle_contrast(+v:count1)<cr>
function! s:toggle_background()
if ! exists('g:colors_name')
echomsg 'No colorscheme set'
return
endif
let l:scheme = g:colors_name
if l:scheme =~# 'dark' || l:scheme =~# 'light'
" Rotate between different theme backgrounds
execute 'colorscheme' (l:scheme =~# 'dark'
\ ? substitute(l:scheme, 'dark', 'light', '')
\ : substitute(l:scheme, 'light', 'dark', ''))
else
execute 'set background='.(&background ==# 'dark' ? 'light' : 'dark')
if ! exists('g:colors_name')
execute 'colorscheme' l:scheme
echomsg 'The colorscheme `'.l:scheme
\ .'` doesn''t have background variants!'
else
echo 'Set colorscheme to '.&background.' mode'
endif
endif
endfunction
function! s:toggle_contrast(delta)
let l:scheme = ''
if g:colors_name =~# 'solarized8'
let l:schemes = map(['_low', '_flat', '', '_high'],
\ '"solarized8_".(&background).v:val')
let l:contrast = ((a:delta + index(l:schemes, g:colors_name)) % 4 + 4) % 4
let l:scheme = l:schemes[l:contrast]
endif
if l:scheme !=# ''
execute 'colorscheme' l:scheme
endif
endfunction
" Location list movement
nmap <Leader>j :lnext<CR>
nmap <Leader>k :lprev<CR>
" Duplicate lines
nnoremap <Leader>d m`YP``
vnoremap <Leader>d YPgv
" Source line and selection in vim
vnoremap <Leader>S y:execute @@<CR>:echo 'Sourced selection.'<CR>
nnoremap <Leader>S ^vg_y:execute @@<CR>:echo 'Sourced line.'<CR>
" Yank buffer's absolute path to X11 clipboard
nnoremap <Leader>y :let @+=expand("%")<CR>:echo 'Relative path copied to clipboard.'<CR>
nnoremap <Leader>Y :let @+=expand("%:p")<CR>:echo 'Absolute path copied to clipboard.'<CR>
" Drag current line/s vertically and auto-indent
vnoremap mk :m-2<CR>gv=gv
vnoremap mj :m'>+<CR>gv=gv
noremap mk :m-2<CR>
noremap mj :m+<CR>
" Last session management shortcuts
" nmap <Leader>se :<C-u>SessionSave last<CR>
nmap <silent> <Leader>se :<C-u>execute 'SessionSave' fnamemodify(resolve(getcwd()), ':p:gs?/?_?')<CR>
nmap <silent> <Leader>os :<C-u>execute 'source '.g:session_directory.'/'.fnamemodify(resolve(getcwd()), ':p:gs?/?_?').'.vim'<CR>
if has('mac')
" Open the macOS dictionary on current word
nmap <Leader>? :!open dict://<cword><CR><CR>
" Use Marked for real-time Markdown preview
if executable('/Applications/Marked 2.app/Contents/MacOS/Marked 2')
autocmd MyAutoCmd FileType markdown
\ nmap <buffer><Leader>P :silent !open -a Marked\ 2.app '%:p'<CR>
endif
" Use Dash on Mac, for context help
if executable('/Applications/Dash.app/Contents/MacOS/Dash')
autocmd MyAutoCmd FileType ansible,go,php,css,less,html,markdown
\ nmap <silent><buffer> K :!open -g dash://"<C-R>=split(&ft, '\.')[0]<CR>:<cword>"&<CR><CR>
autocmd MyAutoCmd FileType javascript,javascript.jsx,sql,ruby,conf,sh
\ nmap <silent><buffer> K :!open -g dash://"<cword>"&<CR><CR>
endif
" Use Zeal on Linux for context help
elseif executable('zeal')
autocmd MyAutoCmd FileType ansible,go,php,css,less,html,markdown
\ nmap <silent><buffer> K :!zeal --query "<C-R>=split(&ft, '\.')[0]<CR>:<cword>"&<CR><CR>
autocmd MyAutoCmd FileType javascript,javascript.jsx,sql,ruby,conf,sh
\ nmap <silent><buffer> K :!zeal --query "<cword>"&<CR><CR>
endif
" }}}
" Display diff from last save {{{
command! DiffOrig vert new | setlocal bt=nofile | r # | 0d_ | diffthis | wincmd p | diffthis
" }}}
" Append modeline to EOF {{{
nnoremap <silent> <Leader>ml :call <SID>append_modeline()<CR>
" Append modeline after last line in buffer
" See: http://vim.wikia.com/wiki/Modeline_magic
function! s:append_modeline() "{{{
let l:modeline = printf(' vim: set ts=%d sw=%d tw=%d %set :',
\ &tabstop, &shiftwidth, &textwidth, &expandtab ? '' : 'no')
let l:modeline = substitute(&commentstring, '%s', l:modeline, '')
call append(line('$'), l:modeline)
endfunction "}}}
" }}}
" s: Windows and buffers {{{
nnoremap <silent> [Window]v :<C-u>split<CR>
nnoremap <silent> [Window]g :<C-u>vsplit<CR>
nnoremap <silent> [Window]t :tabnew<CR>
nnoremap <silent> [Window]o :<C-u>only<CR>
nnoremap <silent> [Window]b :b#<CR>
nnoremap <silent> [Window]c :close<CR>
nnoremap <silent> [Window]x :<C-u>call <SID>BufferEmpty()<CR>
" Split current buffer, go to previous window and previous buffer
nnoremap <silent> [Window]sv :split<CR>:wincmd p<CR>:e#<CR>
nnoremap <silent> [Window]sg :vsplit<CR>:wincmd p<CR>:e#<CR>
function! WipeHiddenBuffers()
let tpbl=[]
call map(range(1, tabpagenr('$')), 'extend(tpbl, tabpagebuflist(v:val))')
for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)==-1')
silent execute 'bwipeout' buf
endfor
endfunction
function! s:BufferEmpty() " {{{
let l:current = bufnr('%')
if ! getbufvar(l:current, '&modified')
enew
silent! execute 'bdelete '.l:current
endif
endfunction " }}}
function! s:SweepBuffers() " {{{
let bufs = range(1, bufnr('$'))
let hidden = filter(bufs, 'buflisted(v:val) && !bufloaded(v:val)')
if ! empty(hidden)
execute 'silent bdelete' join(hidden)
endif
endfunction " }}}
" }}}
" vim: set foldmethod=marker ts=2 sw=2 tw=80 noet :