-
Notifications
You must be signed in to change notification settings - Fork 3
/
dwiw2015.vim
256 lines (204 loc) · 7.35 KB
/
dwiw2015.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
" dwiw2015.vim -- sensible defaults for a modern text editor
" Maintainer: Michael Kropat <mail@michael.kropat.name>
" Run-once guard
if exists('g:loaded_dwiw2015') || &compatible
finish
else
let g:loaded_dwiw2015 = 1
endif
" Group script autocmds so they can be identified with `:autocmd dwiw`
augroup dwiw
autocmd!
if has('win32')
let s:vimfiles = $HOME . '/vimfiles'
else
let s:vimfiles = $HOME . '/.vim'
endif
" ###### Settings ######
" backup -- when overwriting a file, take a backup copy
set backup
let &backupdir = s:vimfiles . '/tmp/backup//'
if !isdirectory(expand(&backupdir))
call mkdir(expand(&backupdir), "p")
endif
" cursorline -- highlight the entire line the cursor is on
set cursorline
" guioptions -- disable GUI elements to prevent unintended window resizing
if has("win32") && has("gui_running")
set guioptions-=e
endif
" hidden -- let user switch away from (hide) buffers with unsaved changes
set hidden
" hlsearch -- highlight search term matches
set hlsearch
" ignorecase -- case-insensitive searching, but...
" smartcase -- become case-sensitive when search term contains upper case
set ignorecase smartcase
" lazyredraw -- don't update screen interactively when running macros etc.
set lazyredraw
" linebreak -- wrap long lines at word breaks instead of fixed length
set linebreak
" listchars -- characters for rendering tab character etc. with :list
if !has('win32') && (&termencoding ==# 'utf-8' || &encoding ==# 'utf-8')
let &listchars = "tab:\u21e5 ,trail:\u2423,extends:\u21c9,precedes:\u21c7,nbsp:\u00b7"
endif
" modeline -- whether to run lines like '# vim: noai:ts=4:sw=4' in any file
" Disabled, as it tends to be a source of security vulnerabilities
" Look at the 'securemodelines' plugin for a better alternative
set nomodeline
" number -- show line numbers at the beginning of each line
" Only enabled in GUI mode, where it is less obtrusive
if has("gui_running") == 1
set number
endif
" showmatch -- flash matching pair when typing a closing paren, bracket etc.
" matchtime -- tenths of a second to display a showmatch (default: 5)
set showmatch matchtime=3
" showmode -- outputs the "-- INSERT --", etc. messages on the last line
" Disabled, since vim-airline displays the mode
set noshowmode
" splitbelow -- open horizontal splits below the current window
set splitbelow
" splitright -- open vertical splits to the right of the current window
set splitright
" swapfile -- keep a swap file to help recover from file corruption
" Disabled since it's so rare and any important file is version controlled
" (Avoids annoying "recover swap file??" questions)
set noswapfile
let &directory = s:vimfiles . '/tmp/swap//'
if !isdirectory(expand(&directory))
call mkdir(expand(&directory), "p")
endif
" undofile -- keep a file to persist undo history after file is closed
if has("persistent_undo") == 1
set undofile
let &undodir = s:vimfiles . '/tmp/undo//'
if !isdirectory(expand(&undodir))
call mkdir(expand(&undodir), "p")
endif
endif
" visualbell -- instead of audible bell, output terminal code 't_vb'
" Disable annoying bell by setting 't_vb' to ''
set visualbell t_vb=
" When GUI starts, 't_vb' is reset, so set it again to ''
autocmd GUIEnter * set t_vb=
" wildmode -- behavior of command-line tab completion
" = list -- print possible matches
" = longest -- complete until amibguous (same as bash/readline)
set wildmode=list:longest
" Settings inherited from ** vim-sensible ** [3]:
"
" * autoindent
" * autoread
" * backspace
" * complete
" * display
" * encoding
" * fileformats
" * history
" * incsearch
" * laststatus
" * listchars
" * nrformats
" * ruler
" * scrolloff
" * shell
" * shiftround
" * showcmd
" * sidescrolloff
" * sidescrolloff
" * smarttab
" * tabpagemax
" * ttimeout
" * ttimeoutlen100
" * viminfo
" * wildmenu
" ack.vim: prefer ag over ack (if available)
if !exists('g:ackprg') && executable('ag')
let g:ackprg = 'ag --vimgrep'
endif
" ###### Key Bindings ######
" Load Windows keybindings on all platforms except OS-X GUI
" Maps the usual suspects: Ctrl-{A,C,S,V,X,Y,Z}
if has("gui_macvim") == 0
source $VIMRUNTIME/mswin.vim
" bad, mswin.vim! bad!
silent! nunmap <C-F>
" Let arrow keys work with visual select
set keymodel-=stopsel
endif
" C-V -- Gvim: paste [All Modes]
" Terminal: enter Visual Block mode [Normal / Visual Mode]
" Terminal: insert literal character [Insert Mode]
" C-Q -- Gvim: enter Visual Block mode [Normal / Visual Mode]
" Gvim: insert literal character [Insert Mode]
if has("gui_running") == 0 && ! empty(maparg('<C-V>'))
unmap <C-V>
iunmap <C-V>
endif
" C-Y -- scroll window upwards [Normal / Visual Mode]
if ! empty(maparg('<C-Y>'))
unmap <C-Y>
endif
" C-Z -- Gvim: undo [All Modes]
" Terminal: suspend vim and return to shell [Normal / Visual Mode]
if has("gui_running") == 0 && ! empty(maparg('<C-Z>'))
unmap <C-Z>
" Technically <C-Z> still performs undo in Terminal during insert mode
endif
" C-/ -- Terminal: toggle whether line is commented [Normal / Visual Mode]
" (Only works in terminals where <C-/> is equivalent to <C-_>)
noremap <silent> <C-_> :Commentary<CR>
" & -- repeate last `:s` substitute (preserves flags)
nnoremap & :&&<CR>
xnoremap & :&&<CR>
" Y -- yank to end of line; see `:help Y` [Normal Mode]
nnoremap Y y$
" Enter key -- insert a new line above the current [Normal Mode]
if empty(maparg('<CR>', 'n'))
nnoremap <CR> O<Esc>j
" ...but not in the Command-line window (solution by Ingo Karkat [2])
autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>
" ...nor in the Quickfix window
autocmd BufReadPost * if &buftype ==# 'quickfix' | nnoremap <buffer> <CR> <CR> | endif
endif
" Ctrl-Tab -- Gvim: switch to next tab [Normal Mode]
nnoremap <silent> <C-Tab> :tabnext<CR>
" Ctrl-Shift-Tab -- Gvim: switch to previous tab [Normal Mode]
nnoremap <silent> <C-S-Tab> :tabprev<CR>
" Q{motion} -- format specified lines [Normal / Visual Mode]
noremap Q gq
" j -- move down one line on the screen [Normal / Visual Mode]
nnoremap j gj
vnoremap j gj
" gj -- move down one line in the file [Normal / Visual Mode]
nnoremap gj j
vnoremap gj j
" k -- move up one line on the screen [Normal / Visual Mode]
nnoremap k gk
vnoremap k gk
" gk -- move up one line in the file [Normal / Visual Mode]
nnoremap gk k
vnoremap gk k
" > -- shift selection rightwards (preserve selection) [Visual Mode]
vnoremap > >gv
" < -- shift selection leftwards (preserve selection) [Visual Mode]
vnoremap < <gv
" Tab -- indent at beginning of line, otherwise autocomplete [Insert Mode]
inoremap <silent> <Tab> <C-R>=DwiwITab()<cr>
inoremap <silent> <S-Tab> <C-N>
" Taken from Gary Bernhardt's vimrc [1]
function! DwiwITab()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
augroup END
" ##### References #####
"
" [1] https://github.com/garybernhardt/dotfiles/blob/master/.vimrc
" [2] http://stackoverflow.com/a/11983449/27581
" [3] https://github.com/tpope/vim-sensible/blob/master/plugin/sensible.vim