Skip to content

Instantly share code, notes, and snippets.

@ShabbyX
Last active February 24, 2021 13:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShabbyX/3691170 to your computer and use it in GitHub Desktop.
Save ShabbyX/3691170 to your computer and use it in GitHub Desktop.
My .vimrc of course
set nocompatible
set exrc " enable per-directory .vimrc files
set secure " disable unsafe commands in local .vimrc files
autocmd!
" Enable spell checking for documents
au FileType tex,markdown,pandoc setlocal spell spelllang=en_us
" Set tab width and convert to space in certain files, based on the generally
" accepted format of community.
au FileType config,vim,tex,make,sh,automake setlocal expandtab | setlocal shiftwidth=2 | setlocal softtabstop=2
au FileType haskell,python setlocal expandtab | setlocal shiftwidth=4 | setlocal softtabstop=4
au FileType tex,markdown,pandoc setlocal textwidth=120
" Always jump to the last line being edited
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
" Add capitalized commands for when I release the shift too slowly
command WQ wq
command Wq wq
command W w
command Q q
command Sp sp
command SP sp
command Vs vs
command VS vs
command Cn cn
" Tell vim what files it can't open
set wildignore+=*.pdf,*.o,*.obj,*.bmp,*.jpg,*.png
" In case there is supertab installed, remove its binding from <tab>
let g:SuperTabMappingForward = '<c-space>'
let g:SuperTabMappingBackward = '<s-c-space>'
" In case there is pandoc installed, make it not fold sections
let g:pandoc_no_folding = 1
" Favorite color!
color desert
" Syntax stuff
filetype plugin indent on
syntax on
set cindent
set cino=:0,g0 " :0 means no indent for case X:
" g0 means no indent for public:
" Editting
set bs=start,indent,eol
set display+=lastline " Show parts of last line, even if it continues out of page
set ww=b,s,<,>,[,] " backspace, space and left and right arrows wrap to next/previous line
" Moving around (force myself not to use arrow keys)
map <up> <nop>
imap <up> <nop>
map <down> <nop>
imap <down> <nop>
map <left> <nop>
imap <left> <nop>
map <right> <nop>
imap <right> <nop>
" View
set is " Incremental search
set hls " Highlight search
set ws " Wrap search
set ruler
set showcmd " Show command as it is being written
set novb " No visual bell
set showcmd " Show command being typed
set laststatus=2 " Two lines for status and command
set statusline=\ %t " Tail of the file name
set statusline+=\ [%{strlen(&fenc)?&fenc:'none'},%{&ff}]
" [Encoding,Format]
set statusline+=\ %m " Modified flag
set statusline+=%r " Read only flag
set statusline+=%y " File type
set statusline+=%= " Write the rest of the status line on the right
set statusline+=%{bufnr('$')==1?'':'['.bufnr('%').'/'.bufnr('$').']\ '}
" Show current buffer over total buffers, if more 1 open buffer
set statusline+=%c " Current column
set statusline+=%V " Virtual column
set statusline+=,%l/%L " Current line over total lines
set statusline+=\ %P\ " Percentage of file
" Settings to make Windows bearable
if (has("win32") || has("win64")) && has("gui_running")
set winaltkeys=yes
" GUI windows font (make sure to install Ubuntu fonts)
set guifont=Ubuntu_Mono:h12:cANSI
endif
" Preserve clipboard on exit
autocmd VimLeave * call system("xclip -selection clipboard -i", getreg('+'))
" Misc
set nobackup " Maybe enable later and set directory to a temp
set history=1000
set autochdir " Make the path during edit the same as the file instead of where vim was called from
set pa=.,../include,../../include,..,../..,,
" Look in these directories also when looking for include files (for auto-complete)
" Remapping of keys
" Swap numbers and their symbols so it would be easier on the wrist
function s:swap_keys(a, b, do)
if a:do
for prefix in ['', 'f', 't', 'F', 'T']
exe 'noremap' prefix.a:a prefix.a:b
exe 'noremap' prefix.a:b prefix.a:a
endfor
exe 'inoremap' a:a a:b
exe 'inoremap' a:b a:a
exe 'cnoremap' a:a a:b
exe 'cnoremap' a:b a:a
else
for prefix in ['', 'f', 't', 'F', 'T']
exe 'noremap' prefix.a:a prefix.a:a
exe 'noremap' prefix.a:b prefix.a:b
endfor
exe 'inoremap' a:a a:a
exe 'inoremap' a:b a:b
exe 'cnoremap' a:a a:a
exe 'cnoremap' a:b a:b
endif
endfunction
function s:shabi(reverse)
call s:swap_keys(1, '!', a:reverse)
call s:swap_keys(2, '@', a:reverse)
call s:swap_keys(3, '#', a:reverse)
call s:swap_keys(4, '$', a:reverse)
call s:swap_keys(5, '%', a:reverse)
call s:swap_keys(6, '^', a:reverse)
call s:swap_keys(7, '&', a:reverse)
call s:swap_keys(8, '*', a:reverse)
call s:swap_keys(9, '(', a:reverse)
call s:swap_keys(0, ')', a:reverse)
call s:swap_keys('-', '_', a:reverse)
endfunction
command -bar Shabi call s:shabi(1)
command -bar Noshabi call s:shabi(0)
au FileType c,cpp,cs,haskell call s:shabi(1)
" After searching, center the found line to the middle of the view
map N Nzz
map n nzz
" Break undo during insert with backspace and again after reinsert
" This breaking of undo happens with backspace, delete a word and
" delete a line. For finer granularity of undo, hitting enter also
" sets an undo break.
function! s:break_undo_on_delete(key)
let l:result = a:key
if !s:deleting
let l:result = "\<C-G>u".l:result
endif
let s:deleting = 1
return l:result
endfunction
function! s:break_undo_on_reinsert(char)
if s:deleting
let s:deleting = 0
call feedkeys("\<BS>\<C-G>u".a:char, 'n')
endif
endfunction
augroup smartundo
autocmd InsertEnter * let s:deleting = 0
autocmd InsertCharPre * call s:break_undo_on_reinsert(v:char)
augroup END
inoremap <expr> <BS> <SID>break_undo_on_delete("\<BS>")
inoremap <expr> <C-W> <SID>break_undo_on_delete("\<C-W>")
inoremap <expr> <C-U> <SID>break_undo_on_delete("\<C-U>")
inoremap <CR> <C-G>u<CR>
" If tabs were expanded, add a way out
function UseTab()
setlocal noexpandtab
setlocal shiftwidth=8
setlocal softtabstop=8
endfunction
command -bar UseTab call UseTab()
set diffexpr=MyDiff() " This was here forever
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
" Run other local commands
if filereadable("/home/sciabaz/.vimrc_extra")
source /home/sciabaz/.vimrc_extra
endif
@ShabbyX
Copy link
Author

ShabbyX commented Aug 21, 2016

~/.vimrc_extra could include very specific commands that are necessary for example only on one computer. An example could be:

au BufRead,BufNewFile /path/to/specific/project/* setlocal expandtab | setlocal shiftwidth=4 | setlocal softtabstop=4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment