aboutsummaryrefslogtreecommitdiff
path: root/base/vimrc
blob: 0db1a1e546e83eb671ed387a883b3aa2729da2b5 (plain)
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
set nocompatible
set encoding=utf-8

set modelines=0 " for security

set mouse=""
set backspace=indent,eol,start

au FocusLost * :wa

" Leader ====----
nnoremap ,, ,
let mapleader=","
noremap <leader>; ;
noremap ; :

" Highlighting ====----
syntax on
filetype plugin indent on

"set t_Co=256 " force enable 256-color mode.
if &t_Co >= 256 || has("gui_running")
  let base16colorspace=256
endif
colorscheme base16-default-dark
if &t_Co > 2 || has("gui_running")
  syntax on
endif

" Indentation ====----
set shiftwidth=2
set tabstop=2
set expandtab
set shiftround " use multiple of shiftwidth with '<' and '>'
set autoindent
set copyindent " copy the previous line's indentation
set nojoinspaces " one space after period on join

xnoremap < <gv
xnoremap > >gv

" Search ====----
set incsearch
set hlsearch
set wrapscan
set ignorecase " ignore case when searching
set smartcase " ignore case if search pattern is all lowercase,
  " case-sensitive otherwise

nnoremap <silent> <leader><space> :noh<cr>

" Case insensitive
nmap * /\<<C-R>=expand('<cword>')<CR>\><CR>
nmap # ?\<<C-R>=expand('<cword>')<CR>\><CR>

set showmatch
noremap <tab> %

" Screen ====----
"set nowrap
set wrap
"set textwidth=79
"set formatoptions=tqrn1
set colorcolumn=85
set number
set relativenumber
set cursorline
set shortmess=aoOstTWAI
set title " change the terminal's title
set scrolloff=3

set visualbell " don't beep
set noerrorbells " don't beep

set list
set listchars=tab:»\ ,extends:›,precedes:‹,nbsp:·,trail:·
"au filetype html,xml set listchars-=tab:>.

set wildmenu
set wildmode=longest:full,full
set wildignore=*.swp,*.bak,*.pyc,*.class

set ttyfast

hi ColorColumn ctermbg=18
hi Folded ctermbg=0 ctermfg=12

" Statusline ====----
function! StatusGitInfo()
  let git = gitbranch#name()
  if git != ''
    return ' '.git.' '
  else
    return ''
endfunction

"set ruler
set showmode
set showcmd
set laststatus=2
set statusline=
set statusline+=%1*\ %{StatusGitInfo()}%2*%<\        " Git branch
set statusline+=%3*%f                                 " Path
set statusline+=%{&modified?'\ +':''}                 " Modified
set statusline+=%{&ro?'\ ':''}                       " Read only
set statusline+=%{&paste?'\ P':''}                    " Paste mode
"set statusline+=[%n%H%M%R%W]\                        " flags and buf no
set statusline+=\ %4*%=%3*\                         " Section break
set statusline+=%{&ff=='unix'?'':toupper(&ff.'\ ')}   " Line ending
set statusline+=%l\ %c\ %P\                           " Line column percent
set statusline+=%2*%1*\                              " End cap
"set statusline+=%2*%1*\ S%{strftime('%R',\ getftime(expand('%')))}\  " Time when last saved
"█ ^Vue0b0

hi StatusLineNC ctermfg=020 ctermbg=019 cterm=NONE
hi User1 ctermfg=015 ctermbg=019
hi User2 ctermfg=019 ctermbg=018
hi User3 ctermfg=020 ctermbg=018
hi User4 ctermfg=018 ctermbg=000

" Stop using arrow keys ====----
" noremap <up> <nop>
" noremap <down> <nop>
" noremap <left> <nop>
" noremap <right> <nop>

" nnoremap j gj
" nnoremap k gk

set whichwrap+=<,>,h,l,[,]

" Splits ====----
nnoremap <leader>w <C-w>v<C-w>l
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" History ====----
set history=1000 " remember more commands and search history
set undolevels=1000 " use many muchos levels of undo

if !isdirectory($HOME."/.vim")
  call mkdir($HOME."/.vim", "", 0770)
endif
if !isdirectory($HOME."/.vim/undo-dir")
  call mkdir($HOME."/.vim/undo-dir", "", 0700)
endif
set undodir=~/.vim/undo-dir
set undofile
" !!!! ADD THIS TO CRONTAB
" 43 0 * * 3 find ~/.vim/undo-dir -type f -mtime +90 -delete

set nobackup
set noswapfile " Note: swap helps large files.

" Custom mappings ====----

" Quickly edit/reload the vimrc file
nnoremap <silent> <leader>ve :tabe $MYVIMRC<CR>
nnoremap <silent> <leader>vr :so $MYVIMRC<CR>

nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>

nnoremap <leader>W :%s/\s\+$//<cr>:let @/=''<CR>
nnoremap <leader>ft Vatzf
nnoremap <leader>S ?{<CR>jV/^\s*\}\?$<CR>k:sort<CR>:noh<CR>
nnoremap <leader>q gqip
nnoremap <leader>vp `[V`]
nnoremap <leader>h :syntax sync fromstart<CR>
nnoremap <leader>l :nohlsearch<cr>:diffupdate<cr>:syntax sync fromstart<cr><c-l>
nnoremap [<space>  :<c-u>put! =repeat(nr2char(10), v:count1)<cr>'[
nnoremap ]<space>  :<c-u>put =repeat(nr2char(10), v:count1)<cr>

noremap <leader>y "+y
noremap <leader>Y "+Y
noremap <leader>p :set paste<CR>:put +<CR>:set nopaste<CR>
vnoremap p "_dP

vnoremap Q gq
nnoremap Q gqap

"cnoremap w!! %!sudo tee % >/dev/null
cnoremap <c-n> <down>
cnoremap <c-p> <up>

nnoremap <leader>qr :.!qrencode -t UTF8<CR>
vnoremap <leader>qr !qrencode -t UTF8<CR>

" helper function to toggle hex mode
function! ToggleHex()
  " hex mode should be considered a read-only operation
  " save values for modified and read-only for restoration later,
  " and clear the read-only flag for now
  let l:modified=&mod
  let l:oldreadonly=&readonly
  let &readonly=0
  let l:oldmodifiable=&modifiable
  let &modifiable=1
  if !exists("b:editHex") || !b:editHex
    " save old options
    let b:oldft=&ft
    let b:oldbin=&bin
    " set new options
    setlocal binary " make sure it overrides any textwidth, etc.
    let &ft="xxd"
    " set status
    let b:editHex=1
    " switch to hex editor
    %!xxd
  else
    " restore old options
    let &ft=b:oldft
    if !b:oldbin
      setlocal nobinary
    endif
    " set status
    let b:editHex=0
    " return to normal editing
    %!xxd -r
  endif
  " restore values for modified and read only state
  let &mod=l:modified
  let &readonly=l:oldreadonly
  let &modifiable=l:oldmodifiable
endfunction

command! -bar Hexmode call ToggleHex()
nnoremap <leader>H :Hexmode<CR>

function! NumberToggle()
  if(&relativenumber == 1)
    set norelativenumber
  else
    set relativenumber
  endif
endfunc

nnoremap <leader>N :call NumberToggle()<cr>
"au InsertEnter * :set norelativenumber
"au InsertLeave * :set relativenumber

" Open to last position ====----

au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif

" Custom functions ====----

if filereadable(expand("~/.vimrc.local"))
  source ~/.vimrc.local
endif

function! SetLocalOptions(fname)
  let dirname = fnamemodify(a:fname, ":p:h")
  while "/" != dirname
    let lvimrc  = dirname . "/.vimrc.local"
    if filereadable(lvimrc)
      execute "source " . lvimrc
      break
    endif
    let dirname = fnamemodify(dirname, ":p:h:h")
  endwhile
endfunction

au BufNewFile,BufRead * call SetLocalOptions(bufname("%"))

" To move elsewhere ====----
au Syntax json normal zR
au BufNewFile,BufRead *.less setl filetype=less
au! BufWritePost $MYVIMRC source $MYVIMRC
au FileType gitcommit setl textwidth=72
au FileType gitcommit setl colorcolumn=73
au! BufRead,BufNewFile gitcommit setl spell
au! BufRead,BufNewFile *.md setl filetype=markdown
au! BufRead,BufNewFile *.md setl spell
pa! matchit