my dotz
1"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2" => General
3"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4" Sets how many lines of history VIM has to remember
5set history=500
6
7" Enable filetype plugins
8filetype plugin on
9filetype indent on
10
11" Set to auto read when a file is changed from the outside
12set autoread
13au FocusGained,BufEnter * checktime
14
15" With a map leader it's possible to do extra key combinations
16" like <leader>w saves the current file
17let mapleader = ","
18
19" Fast saving
20nmap <leader>w :w!<cr>
21
22" for adding morning notes to notes.md
23nmap <leader>m Go<esc>:put =strftime(\"%Y-%m-%d\")<cr>o----------------<cr><esc>
24
25" :W sudo saves the file
26" (useful for handling the permission-denied error)
27command! W execute 'w !sudo tee % > /dev/null' <bar> edit!
28
29
30"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
31" => VIM user interface
32"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
33" Set 7 lines to the cursor - when moving vertically using j/k
34set so=7
35
36" Avoid garbled characters in Chinese language windows OS
37let $LANG='en'
38set langmenu=en
39source $VIMRUNTIME/delmenu.vim
40source $VIMRUNTIME/menu.vim
41
42" Turn on the Wild menu
43set wildmenu
44
45" Ignore compiled files
46set wildignore=*.o,*~,*.pyc
47if has("win16") || has("win32")
48 set wildignore+=.git\*,.hg\*,.svn\*
49else
50 set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store
51endif
52
53"Always show current position
54set ruler
55
56" Height of the command bar
57set cmdheight=1
58
59" A buffer becomes hidden when it is abandoned
60set hid
61
62" Configure backspace so it acts as it should act
63set backspace=eol,start,indent
64set whichwrap+=<,>,h,l
65
66" Ignore case when searching
67set ignorecase
68
69" When searching try to be smart about cases
70set smartcase
71
72" Highlight search results
73set hlsearch
74
75" Makes search act like search in modern browsers
76set incsearch
77
78" Don't redraw while executing macros (good performance config)
79set lazyredraw
80
81" For regular expressions turn magic on
82set magic
83
84" Show matching brackets when text indicator is over them
85set showmatch
86" How many tenths of a second to blink when matching brackets
87set mat=2
88
89" No annoying sound on errors
90set noerrorbells
91set novisualbell
92set t_vb=
93set tm=500
94
95" Properly disable sound on errors on MacVim
96if has("gui_macvim")
97 autocmd GUIEnter * set vb t_vb=
98endif
99
100
101" Add a bit extra margin to the left
102set foldcolumn=1
103
104
105"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
106" => Colors and Fonts
107"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
108" Enable syntax highlighting
109syntax enable
110
111" Enable 256 colors palette in Gnome Terminal
112if $COLORTERM == 'gnome-terminal'
113 set t_Co=256
114endif
115
116try
117 colorscheme sunbather
118catch
119endtry
120
121set background=dark
122
123" Set extra options when running in GUI mode
124if has("gui_running")
125 set guioptions-=T
126 set guioptions-=e
127 set t_Co=256
128 set guitablabel=%M\ %t
129endif
130
131" Set utf8 as standard encoding and en_US as the standard language
132set encoding=utf8
133
134" Use Unix as the standard file type
135set ffs=unix,dos,mac
136
137
138"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
139" => Files, backups and undo
140"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
141" Turn backup off, since most stuff is in SVN, git etc. anyway...
142set nobackup
143set nowb
144set noswapfile
145
146
147"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
148" => Text, tab and indent related
149"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
150" Use spaces instead of tabs
151set expandtab
152
153" Be smart when using tabs ;)
154set smarttab
155
156" 1 tab == 4 spaces
157set shiftwidth=4
158set tabstop=4
159
160" Linebreak on 500 characters
161set lbr
162set tw=500
163
164set ai "Auto indent
165set si "Smart indent
166set wrap "Wrap lines
167
168
169""""""""""""""""""""""""""""""
170" => Visual mode related
171""""""""""""""""""""""""""""""
172" Visual mode pressing * or # searches for the current selection
173" Super useful! From an idea by Michael Naumann
174vnoremap <silent> * :<C-u>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
175vnoremap <silent> # :<C-u>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
176
177
178"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
179" => Moving around, tabs, windows and buffers
180"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
181" Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
182map <space> /
183map <C-space> ?
184
185" Disable highlight when <leader><cr> is pressed
186map <silent> <leader><cr> :noh<cr>
187
188" Smart way to move between windows
189map <C-j> <C-W>j
190map <C-k> <C-W>k
191map <C-h> <C-W>h
192map <C-l> <C-W>l
193
194" Close the current buffer
195map <leader>bd :Bclose<cr>:tabclose<cr>gT
196
197" Close all the buffers
198map <leader>ba :bufdo bd<cr>
199
200map <leader>l :bnext<cr>
201map <leader>h :bprevious<cr>
202
203" Useful mappings for managing tabs
204map <leader>tn :tabnew<cr>
205map <leader>to :tabonly<cr>
206map <leader>tc :tabclose<cr>
207map <leader>tm :tabmove
208map <leader>t<leader> :tabnext
209
210" Let 'tl' toggle between this and the last accessed tab
211let g:lasttab = 1
212nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
213au TabLeave * let g:lasttab = tabpagenr()
214
215
216" Opens a new tab with the current buffer's path
217" Super useful when editing files in the same directory
218map <leader>te :tabedit <C-r>=expand("%:p:h")<cr>/
219
220" Switch CWD to the directory of the open buffer
221map <leader>cd :cd %:p:h<cr>:pwd<cr>
222
223" Specify the behavior when switching between buffers
224try
225 set switchbuf=useopen,usetab,newtab
226 set stal=2
227catch
228endtry
229
230" Return to last edit position when opening files (You want this!)
231au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
232
233
234""""""""""""""""""""""""""""""
235" => Status line
236""""""""""""""""""""""""""""""
237" Always show the status line
238set laststatus=2
239
240" Format the status line
241set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c
242
243
244"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
245" => Editing mappings
246"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
247" Remap VIM 0 to first non-blank character
248" map 0 ^
249
250" Move a line of text using ALT+[jk] or Command+[jk] on mac
251nmap <M-j> mz:m+<cr>`z
252nmap <M-k> mz:m-2<cr>`z
253vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
254vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z
255
256if has("mac") || has("macunix")
257 nmap <D-j> <M-j>
258 nmap <D-k> <M-k>
259 vmap <D-j> <M-j>
260 vmap <D-k> <M-k>
261endif
262
263" Delete trailing white space on save, useful for some filetypes ;)
264fun! CleanExtraSpaces()
265 let save_cursor = getpos(".")
266 let old_query = getreg('/')
267 silent! %s/\s\+$//e
268 call setpos('.', save_cursor)
269 call setreg('/', old_query)
270endfun
271
272if has("autocmd")
273 autocmd BufWritePre *.txt,*.js,*.py,*.wiki,*.sh,*.coffee :call CleanExtraSpaces()
274endif
275
276
277"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
278" => Spell checking
279"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
280" Pressing ,ss will toggle and untoggle spell checking
281map <leader>ss :setlocal spell!<cr>
282
283" Shortcuts using <leader>
284map <leader>sn ]s
285map <leader>sp [s
286map <leader>sa zg
287map <leader>s? z=
288
289
290"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
291" => Misc
292"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
293" Quickly open a buffer for scribble
294map <leader>q :e ~/buffer<cr>
295
296" Quickly open a markdown buffer for scribble
297map <leader>x :e ~/buffer.md<cr>
298
299" Toggle paste mode on and off
300map <leader>pp :setlocal paste!<cr>
301
302
303"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
304" => Helper functions
305"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
306" Returns true if paste mode is enabled
307function! HasPaste()
308 if &paste
309 return 'PASTE MODE '
310 endif
311 return ''
312endfunction
313
314" Don't close window, when deleting a buffer
315command! Bclose call <SID>BufcloseCloseIt()
316function! <SID>BufcloseCloseIt()
317 let l:currentBufNum = bufnr("%")
318 let l:alternateBufNum = bufnr("#")
319
320 if buflisted(l:alternateBufNum)
321 buffer #
322 else
323 bnext
324 endif
325
326 if bufnr("%") == l:currentBufNum
327 new
328 endif
329
330 if buflisted(l:currentBufNum)
331 execute("bdelete! ".l:currentBufNum)
332 endif
333endfunction
334
335function! CmdLine(str)
336 call feedkeys(":" . a:str)
337endfunction
338
339function! VisualSelection(direction, extra_filter) range
340 let l:saved_reg = @"
341 execute "normal! vgvy"
342
343 let l:pattern = escape(@", "\\/.*'$^~[]")
344 let l:pattern = substitute(l:pattern, "\n$", "", "")
345
346 if a:direction == 'gv'
347 call CmdLine("Ack '" . l:pattern . "' " )
348 elseif a:direction == 'replace'
349 call CmdLine("%s" . '/'. l:pattern . '/')
350 endif
351
352 let @/ = l:pattern
353 let @" = l:saved_reg
354endfunction
355
356" Transparent editing of age encrypted files.
357augroup encrypted
358au!
359" First make sure nothing is written to ~/.viminfo while editing
360" an encrypted file.
361autocmd BufReadPre,FileReadPre *.age set viminfo=
362" We don't want a swap file, as it writes unencrypted data to disk
363autocmd BufReadPre,FileReadPre *.age set noswapfile
364" Switch to binary mode to read the encrypted file
365autocmd BufReadPre,FileReadPre *.age set bin
366autocmd BufReadPre,FileReadPre *.age let ch_save = &ch|set ch=2
367autocmd BufReadPre,FileReadPre *.age let shsave=&sh
368autocmd BufReadPre,FileReadPre *.age let &sh='sh'
369autocmd BufReadPre,FileReadPre *.age let ch_save = &ch|set ch=2
370autocmd BufReadPost,FileReadPost *.age '[,']!age -i ~/.age/key.txt --decrypt 2> /dev/null
371autocmd BufReadPost,FileReadPost *.age let &sh=shsave
372" Switch to normal mode for editing
373autocmd BufReadPost,FileReadPost *.age set nobin
374autocmd BufReadPost,FileReadPost *.age let &ch = ch_save|unlet ch_save
375autocmd BufReadPost,FileReadPost *.age execute ":doautocmd BufReadPost " . expand("%:r")
376" Convert all text to encrypted text before writing
377autocmd BufWritePre,FileWritePre *.age set bin
378autocmd BufWritePre,FileWritePre *.age let shsave=&sh
379autocmd BufWritePre,FileWritePre *.age let &sh='sh'
380autocmd BufWritePre,FileWritePre *.age '[,']!age -r "$(sed -n 's/.*\(age\)/\1/p' ~/.age/key.txt)" 2>/dev/null
381autocmd BufWritePre,FileWritePre *.age let &sh=shsave
382" Undo the encryption so we are back in the normal text, directly
383" after the file has been written.
384autocmd BufWritePost,FileWritePost *.age silent u
385autocmd BufWritePost,FileWritePost *.age set nobin
386augroup END
387
388nnoremap 9 :let _s=@/<Bar>:%s/\s\+$//e<Bar>:let @/=_s<Bar><CR>