aboutsummaryrefslogtreecommitdiff
path: root/vim/vimrc
blob: 53c524eafd436fdbfb3b4ed703904ecbc88b534c (plain) (blame)
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
" Tom Ryder (tejr)'s vimrc: <https://sanctum.geek.nz/cgit/dotfiles.git>
"
" This file is not truly self-contained. It should run without errors on its
" own without the accompanying plugins to which it refers near its end, but
" you'll get errors for some of the leader maps.

" Undo anything the operating system's vimrc may have broken
runtime system.vim

" Load filetype settings, plugins, and maps
if has('autocmd')
  let g:maplocalleader = ','
  filetype plugin indent on
endif

" Options dependent on the syntax feature
if has('syntax') && !exists('g:syntax_on')

  " Use syntax highlighting
  syntax enable

  " Use my colorscheme if using the GUI or if we have 256 colors
  if has('gui_running') || &t_Co >= 256
    silent! colorscheme sahara
  endif

  " If not sahara, then default with dark background
  if !exists('g:colors_name')
    set background=dark
  endif

endif

" The all-important default indent settings; filetypes to tweak
set autoindent    " Use indent of previous line on new lines
set expandtab     " Use spaces instead of tabs
set shiftwidth=4  " Indent with four spaces

" Spaces to insert on Tab key insert
if v:version > 703 || v:version == 703 && has('patch693')
  set softtabstop=-1  " Refer to 'shiftwidth' if supported
else
  set softtabstop=4   " Otherwise just four spaces
endif

" Let me backspace over pretty much anything
set backspace+=eol     " Line breaks
set backspace+=indent  " Spaces from 'autoindent'
set backspace+=start   " The start of current insertion

" Never use any kind of bell, visual or not
if exists('+belloff')
  set belloff=all
else
  set visualbell t_vb=
endif

" How to deal with lines wrapping beyond the last screen row
if v:version > 704 || v:version == 704 && has('patch2109')
  set display=truncate  " Show '@@@' on the last line, if supported
else
  set display=lastline  " Just let it run off the screen if not
endif

" Don't wait for a key after Escape in insert mode
if exists('+esckeys')  " Not in Neovim
  set noesckeys
endif

" Delete comment leaders when joining lines, if supported
if v:version > 703 || v:version == 703 && has('patch541')
  set formatoptions+=j
endif

" Keep more command and search history
set history=500

" Don't assume I'm editing C; let the filetype set this
set include=

" Don't join lines with two spaces at the end of sentences
set nojoinspaces

" Don't show a statusline if there's only one window
if has('nvim')  " Neovim changed the default to 2
  set laststatus=1
endif

" Don't redraw the screen during batch execution
set lazyredraw

" Define extra 'list' display characters
set listchars+=extends:>   " Unwrapped text to screen right
set listchars+=precedes:<  " Unwrapped text to screen left
set listchars+=tab:>-      " Tab characters, preserve width
set listchars+=trail:_     " Trailing spaces
if v:version >= 700
  set listchars+=nbsp:+    " Non-breaking spaces
endif

" Don't allow setting options via buffer content
set nomodeline

" Treat numbers with a leading zero as decimal, not octal
set nrformats-=octal

" Abbreviate some more regularly displayed messages
set shortmess+=I  " Don't show startup splash screen
set shortmess+=m  " [Modified] -> [+]
set shortmess+=r  " [readonly] -> [RO]
set shortmess+=w  " written -> [w], appended -> [a]

" Clear default 'comments' value, let the filetype handle it
if has('comments')
  set comments=
endif

" Highlight settings for search
if has('extra_search')
  set hlsearch   " Highlight completed searches...
  nohlsearch     " ...but clear it on startup or after re-sourcing
  set incsearch  " Show matches as I type
endif

" More sensible language-agnostic setting for gf/:find
if has('file_in_path')
  set path=.,,**
endif

" Don't load GUI menus; set here before GUI starts
if has('gui_running')
  set guioptions+=M
endif

" Line break behaviour settings for 'wrap'
if has('linebreak')
  set linebreak      " Break lines at word boundaries
  set showbreak=...  " Prefix wrapped rows with three dots
  if exists('+breakindent')
    set breakindent  " Indent wrapped lines, if supported
  endif
endif

" Don't store any options or mappings in sessions
if has('mksession')
  set sessionoptions-=localoptions
  set sessionoptions-=options
endif

" Let me move beyond buffer text in visual block mode
if has('virtualedit')
  set virtualedit+=block
endif

" Nicer completion for command mode
if has('wildmenu')
  set wildmenu               " Use wildmenu
  set wildmode=list:longest  " Tab press completes and lists
  if exists('+wildignorecase')
    set wildignorecase       " Case insensitive, if supported
  endif
endif

" New windows go below or to the right of a split
if has('windows')
  set splitbelow
  if has('vertsplit')
    set splitright
  endif
endif

" Stack normal/visual/select Ctrl-L to clear search highlight
nnoremap <silent> <C-L> :<C-U>nohlsearch<CR><C-L>
vnoremap <silent> <C-L> :<C-U>nohlsearch<CR>gv<C-L>

" Remap insert Ctrl-C to undo the escaped insert operation
" Default to not-quite-correct vim-tiny-compatible map if no plugin
inoremap <Plug>(InsertCancel) <Esc>u
imap <C-C> <Plug>(InsertCancel)

" Remap normal/visual space to scroll down a page, backspace up
nnoremap <Space> <C-F>
nnoremap <BS> <C-B>
if v:version >= 700
  xnoremap <Space> <C-F>
  xnoremap <BS> <C-B>
endif

" Remap normal/visual & to preserve substitution flags
nnoremap <silent> & :&&<CR>
if v:version >= 700
  xnoremap <silent> & :&&<CR>
endif

" Cycle through argument list
nnoremap [a :<C-U>previous<CR>
nnoremap ]a :<C-U>next<CR>
" Cycle through buffers
nnoremap [b :<C-U>bprevious<CR>
nnoremap ]b :<C-U>bnext<CR>
" Cycle through quicklist/:helpgrep items
nnoremap [c :<C-U>cprevious<CR>
nnoremap ]c :<C-U>cnext<CR>
" Cycle through location list items
nnoremap [l :<C-U>lprevious<CR>
nnoremap ]l :<C-U>lnext<CR>
" Cycle through tabs
nnoremap [t :<C-U>tabprevious<CR>
nnoremap ]t :<C-U>tabnext<CR>

" Insert blank lines around current line
nmap [<Space> <Plug>(PutBlankLinesAbove)
nmap ]<Space> <Plug>(PutBlankLinesBelow)

" Normal leader maps; use <Bslash> not <Leader> for vim-tiny

" \a toggles 'formatoptions' 'a' flag using a plugin
nnoremap <Bslash>a :<C-U>ToggleOptionFlagLocal formatoptions a<CR>
" \b toggles copy-pasteable linebreak settings
nmap <Bslash>b <Plug>(CopyLinebreakToggle)
" \c toggles 'cursorline'
nnoremap <Bslash>c :<C-U>setlocal cursorline! cursorline?<CR>
" \C toggles 'cursorcolumn'
nnoremap <Bslash>C :<C-U>setlocal cursorcolumn! cursorcolumn?<CR>
" \d inserts the local date (POSIX date)
nnoremap <Bslash>d :read !date<CR>
" \D inserts the UTC date (POSIX date)
nnoremap <Bslash>D :read !date -u<CR>
" \e forces a buffer to be editable
nnoremap <Bslash>e :setlocal modifiable noreadonly<CR>
" \f shows the current 'formatoptions' at a glance
nnoremap <Bslash>f :<C-U>setlocal formatoptions?<CR>
" \g changes directory to the current file's location
nnoremap <Bslash>g :<C-U>cd %:h<CR>:pwd<CR>
" \h toggles highlighting search results
nnoremap <Bslash>h :<C-U>set hlsearch! hlsearch?<CR>
" \i toggles showing matches as I enter my pattern
nnoremap <Bslash>i :<C-U>set incsearch! incsearch?<CR>
" \j jumps to buffers (jetpack)
nnoremap <Bslash>j :<C-U>buffers<CR>:buffer<Space>
" \k shows my marks
nnoremap <Bslash>k :<C-U>marks<CR>
" \l toggles showing tab, end-of-line, and trailing whitespace
nnoremap <Bslash>l :<C-U>setlocal list! list?<CR>
" \m shows all maps
nnoremap <Bslash>m :<C-U>map<CR>
" \M shows buffer-local maps
nnoremap <Bslash>M :<C-U>map <buffer><CR>
" \n toggles line numbers
nnoremap <Bslash>n :<C-U>setlocal number! number?<CR>
" \N toggles 'ruler'
nnoremap <Bslash>N :<C-U>set ruler! ruler?<CR>
" \o opens a line below in paste mode
nmap <Bslash>o <Plug>(PasteOpenBelow)
" \o opens a line above in paste mode
nmap <Bslash>O <Plug>(PasteOpenAbove)
" \p toggles paste mode
nnoremap <Bslash>p :<C-U>set paste! paste?<CR>
" \q formats the current paragraph
nnoremap <Bslash>q gqap
" \r reloads .vimrc
nnoremap <Bslash>r :<C-U>source $MYVIMRC<CR>
" \R reloads filetype
nnoremap <Bslash>R :<C-U>doautocmd filetypedetect BufRead<CR>
" \s toggles spell checking
nnoremap <Bslash>s :<C-U>setlocal spell! spell?<CR>
" \t shows current filetype
nnoremap <Bslash>t :<C-U>setlocal filetype?<CR>
" \T clears filetype (follow with \R)
nnoremap <Bslash>T :<C-U>setlocal filetype=<CR>
" \u sets US English spelling (compare \z)
nnoremap <Bslash>u :<C-U>setlocal spelllang=en_us<CR>
" \v shows all global variables
nnoremap <Bslash>v :<C-U>let g: v:<CR>
" \V shows all local variables
nnoremap <Bslash>V :<C-U>let b: t: w:<CR>
" \w toggles wrapping
nnoremap <Bslash>w :<C-U>setlocal wrap! wrap?<CR>
" \x strips trailing whitespace via a custom plugin
nmap <Bslash>x <Plug>(StripTrailingWhitespace)
" \y shows all registers
nnoremap <Bslash>y :<C-U>registers<CR>
" \z sets NZ English spelling (compare \u)
nnoremap <Bslash>z :<C-U>setlocal spelllang=en_nz<CR>

" \= runs the whole buffer through =, preserving position
nnoremap <Bslash>= :<C-U>call vimrc#Anchor('1G=G')<CR>
" \+ runs the whole buffer through gq, preserving position
nnoremap <Bslash>+ :<C-U>call vimrc#Anchor('1GgqG')<CR>
" \. runs the configured make program to location list
nnoremap <Bslash>. :<C-U>lmake!<CR>

" \DEL deletes the current buffer
nnoremap <Bslash><Delete> :<C-U>bdelete<CR>
" \INS edits a new buffer
nnoremap <Bslash><Insert> :<C-U>enew<CR>

" Source any .vim files from ~/.vim/config
runtime! config/*.vim

" If we reloaded, reload filetype detection for the active buffer too, so that
" any local settings for it are restored
if exists('g:loaded_vimrc')
  if &filetype !=# ''
    doautocmd filetypedetect BufRead
  endif
  echomsg 'Reloaded vimrc: '.$MYVIMRC
endif
if 1
  let g:loaded_vimrc = 1
endif