]> git.rmz.io Git - dotfiles.git/blob - vimrc
persistent undo history
[dotfiles.git] / vimrc
1 " My vimrc file.
2 "
3 " Author: Samir Benmendil <samir.benmendil[at]gmail[dot]com>
4 "
5
6 " pathogen.vim runtime path manipulation
7 silent! call pathogen#infect()
8
9 " search {{{1
10 set incsearch
11 set ignorecase
12 set smartcase
13 set hlsearch
14 " use leader-n to unhighlight search
15 nmap <silent> <Leader>n :silent nohl<CR>
16 " use leader-# to display the number of matches for the last search
17 nmap <Leader># :%s:<C-R>/::gn<CR>
18
19 " line wrap {{{1
20 set backspace=indent,eol,start " allow backspacing over everything in insert mode
21 set wrap " wrap lines
22 set linebreak " do not wrap in the middle of a word
23 set showbreak=▒▒ " show these chars for wrapped lines
24
25 " backup/undo {{{1
26 set backup " keep a backup file
27 set backupdir=$HOME/.vim/backupdir
28
29 set undofile " persistent undo history
30 set undodir=$HOME/.vim/backupdir
31
32 " NERDTree {{{1
33 " open/close NERDTree with \e
34 nmap <Leader>e :NERDTreeToggle<CR>
35 " <space> to open files/dirs
36 let NERDTreeMapActivateNode='<space>'
37 " open NERDTree if no files were selected
38 autocmd vimenter * if !argc() | NERDTree | endif
39 " close vim if only NERDTree is open
40 autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
41
42 " airline {{{1
43 let g:airline#extensions#tabline#enabled = 1
44
45 " tabbing {{{1
46 :nmap <Leader>b :set expandtab tabstop=4 shiftwidth=4 softtabstop=4<CR>
47 :nmap <Leader>B :set expandtab tabstop=8 shiftwidth=8 softtabstop=4<CR>
48 :nmap <Leader>M :set noexpandtab tabstop=8 softtabstop=4 shiftwidth=4<CR>
49 :nmap <Leader>m :set expandtab tabstop=2 shiftwidth=2 softtabstop=2<CR>
50
51 " appearance {{{1
52 set background=dark " Dark background, d'uh!
53 set number " show some linenumbers
54 set showmatch " Show matching brackets.
55 set list listchars=tab:»·,trail:· " show these chars for tabs and trailing spaces
56
57 " misc options {{{1
58 set history=500 " keep 500 lines of command line history
59 set ruler " show the cursor position all the time
60 set confirm " Ask what to do when closing unsaved documents
61 set showcmd " Show (partial) command in status line.
62 set autowrite " Automatically save before commands like :next and :make
63 "set hidden " Hide buffers when they are abandoned
64 set splitright " split right when using :vsp
65 set scrolloff=5 " keep at least n lines above/below
66
67 set viminfo='100,<50,s10,h,n~/.vim/viminfo " viminfo defaults but save file in .vim
68
69 " tabs {{{1
70 set softtabstop=4
71 set shiftwidth=4
72 set expandtab
73
74 " misc bindings {{{1
75 " Don't use Ex mode, use Q for formatting
76 map Q gq
77
78 " CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo,
79 " so that you can undo CTRL-U after inserting a line break.
80 inoremap <C-U> <C-G>u<C-U>
81
82 " Press `` to toggle insert and replace mode (no <Insert> key on Mac keyboard)
83 imap `` <Insert>
84
85 " paste toggle {{{1
86 nmap <F11> :set paste! paste?<CR>
87 imap <F11> <C-o>:set paste!<CR>
88 vmap <F11> <Esc>:set paste!<CR>gv
89 set pastetoggle=<F11>
90
91 " mouse {{{1
92 " In many terminal emulators the mouse works just fine, thus enable it.
93 if has('mouse')
94 "xterm mouse with middleclick paste
95 nnoremap <MiddleMouse> i<MiddleMouse>
96 vnoremap <MiddleMouse> s<MiddleMouse>
97 set mouse=rnv
98 "choose either one
99 set ttymouse=xterm
100 "set ttymouse=xterm2
101 endif
102
103 " syntax {{{1
104 " Switch syntax highlighting on, when the terminal has colors
105 " Also switch on highlighting the last used search pattern.
106 if &t_Co > 2 || has("gui_running")
107 syntax on
108 endif
109
110 " autocmd {{{1
111 " Only do this part when compiled with support for autocommands.
112 if has("autocmd")
113 " Enable file type detection.
114 " Use the default filetype settings, so that mail gets 'tw' set to 72,
115 " 'cindent' is on in C files, etc.
116 " Also load indent files, to automatically do language-dependent indenting.
117 filetype plugin indent on
118
119 " Put these in an autocmd group, so that we can delete them easily.
120 augroup vimrcEx
121 au!
122
123 " For all text files set 'textwidth' to 78 characters.
124 autocmd FileType text setlocal textwidth=78
125
126 " When editing a file, always jump to the last known cursor position.
127 " Don't do it when the position is invalid or when inside an event handler
128 " (happens when dropping a file on gvim).
129 " Also don't do it when the mark is in the first line, that is the default
130 " position when opening a file.
131 " blacklist certain filetype, you can get a file type with :echo &ft
132 let blacklist = ['gitcommit']
133 autocmd BufReadPost *
134 \ if index(blacklist, &ft) < 0 && line("'\"") > 1 && line("'\"") <= line("$") |
135 \ exe "normal! g`\"" |
136 \ endif
137
138 augroup END
139 else
140 set autoindent " always set autoindenting on
141 endif " has("autocmd")
142
143 " DiffOrig {{{1
144 " Convenient command to see the difference between the current buffer and the
145 " file it was loaded from, thus the changes you made.
146 " Only define it when not defined already.
147 if !exists(":DiffOrig")
148 command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
149 \ | wincmd p | diffthis
150 endif
151
152 " fold {{{1
153 set foldmethod=marker
154 set foldlevelstart=99
155 " space will toggle current fold in normal mode, if not in a fold, normal
156 " behaviour
157 nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
158 vnoremap <Space> zf
159 " save and restore folds
160 autocmd BufWinLeave *.* mkview
161 autocmd BufWinEnter *.* silent loadview
162
163 function! NextIndent(exclusive, fwd, lowerlevel, skipblanks) " {{{1
164 " Jump to the next or previous line that has the same level or a lower
165 " level of indentation than the current line.
166 "
167 " exclusive (bool): true: Motion is exclusive
168 " false: Motion is inclusive
169 " fwd (bool): true: Go to next line
170 " false: Go to previous line
171 " lowerlevel (bool): true: Go to line with lower indentation level
172 " false: Go to line with the same indentation level
173 " skipblanks (bool): true: Skip blank lines
174 " false: Don't skip blank lines
175 let line = line('.')
176 let column = col('.')
177 let lastline = line('$')
178 let indent = indent(line)
179 let stepvalue = a:fwd ? 1 : -1
180 while (line > 0 && line <= lastline)
181 let line = line + stepvalue
182 if ( ! a:lowerlevel && indent(line) == indent ||
183 \ a:lowerlevel && indent(line) < indent)
184 if (! a:skipblanks || strlen(getline(line)) > 0)
185 if (a:exclusive)
186 let line = line - stepvalue
187 endif
188 exe line
189 exe "normal " column . "|"
190 return
191 endif
192 endif
193 endwhile
194 endfunction
195
196 " Moving back and forth between lines of same or lower indentation.
197 nnoremap <silent> [l :call NextIndent(0, 0, 0, 1)<CR>
198 nnoremap <silent> ]l :call NextIndent(0, 1, 0, 1)<CR>
199 nnoremap <silent> [L :call NextIndent(0, 0, 1, 1)<CR>
200 nnoremap <silent> ]L :call NextIndent(0, 1, 1, 1)<CR>
201 vnoremap <silent> [l <Esc>:call NextIndent(0, 0, 0, 1)<CR>m'gv''
202 vnoremap <silent> ]l <Esc>:call NextIndent(0, 1, 0, 1)<CR>m'gv''
203 vnoremap <silent> [L <Esc>:call NextIndent(0, 0, 1, 1)<CR>m'gv''
204 vnoremap <silent> ]L <Esc>:call NextIndent(0, 1, 1, 1)<CR>m'gv''
205 onoremap <silent> [l :call NextIndent(0, 0, 0, 1)<CR>
206 onoremap <silent> ]l :call NextIndent(0, 1, 0, 1)<CR>
207 onoremap <silent> [L :call NextIndent(1, 0, 1, 1)<CR>
208 onoremap <silent> ]L :call NextIndent(1, 1, 1, 1)<CR>
209 " end of jump indent