Skip to content

Instantly share code, notes, and snippets.

@superjer
Last active September 26, 2015 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superjer/1135490 to your computer and use it in GitHub Desktop.
Save superjer/1135490 to your computer and use it in GitHub Desktop.
SuperJer's .vimrc
" .vimrc
syntax on " Use syntax hilighting
set nocompatible " Use Vim defaults, as opposed to Vi defaults
set bs=indent,eol,start " Allow backspacing over everything in insert mode
set viminfo='20,<5000 " Store ' filesworth of marks and < lines in each register in the .viminfo file
set history=5000 " Keep this many lines of command line history
set ruler " Show the cursor position all the time, bottom right corner of the screen
set nowrap " Do not wrap lines
set shiftwidth=2 " Number of spaces to use for each autoindent, by default
set shiftround " Round indentation to a multiple of shiftwidth, e.g. when using >>
set expandtab " Expand tabs to spaces, by default
set autoindent " Keep indentation when starting a new line
set formatoptions-=t " t: auto-wrap using textwidth, but not comments
set lcs=tab:○─,eol:●,nbsp:◇ " visible tabs and eols when :set list
set ignorecase " Ignore case during search, /?
set smartcase " Override ignorecase if search contains upper case characters
set incsearch " Show search matches while typing
set hlsearch " Hilight all matches when searching
set gdefault " When substituting, the g flag is on by default
set cot=menuone,preview " ^X^O should open scratch window even if there is only one match
set whichwrap=~,[,] " List of left-right movements that are allow to wrap lines, [ and ] mean arrow keys in insert mode
set matchpairs+=<:> " Make <...> work with the %, brace matching command
set virtualedit=block " Allow selecting beyond ends of lines in visual block mode
set nocsverb " Stop complaining 'duplicate cscope database not added'
set nowrapscan " Do not search past the end of the file
set title " Sets the title on the term window
set nrformats= " Only allow base10 numbers with ctrl-a and -x, don't trim my leading zeros
set mouse=a " enable mouse always. Hint: use Shift+Click to send clicks to terminal emu)
set guioptions-=m " remove menu
set guioptions-=T " remove toolbar
set gfn=Monospace\ 12 " GUI font
set ttyfast " send more text to the screen faster
set path=.,,.;,; " for gf and related, search in file's dir, then cwd, then file's dir's parents, then cwd's parents
set isfname=@,48-57,/,.,-,_,~ " file names can contain alphas (@), numbers (48-57), and some puncts
" ignore leading slash when using gf
nnoremap gf :exe 'find' substitute(expand('<cfile>'), '^/', '', '')<CR>
" powerline!
set rtp+=~/SJCONF/powerline/powerline/bindings/vim
if ! has('gui_running')
set ttimeoutlen=10
augroup FastEscape
autocmd!
au InsertEnter * set timeoutlen=300
au InsertLeave * set timeoutlen=1000
augroup END
endif
set laststatus=2 " Always display the statusline in all windows
set noshowmode " Hide the default mode text (e.g. -- INSERT -- below the statusline)
" when does wundo and rundo become available?
if version >= 703
set undodir=~/.vim/undo " location to store undofiles
" Always write undo history, but only read it on command
" use <leader>u to load old undo info!
" modified from example in :help undo-persistence
nnoremap <leader>u :call ReadUndo()<CR>
au BufWritePost * call WriteUndo()
func! ReadUndo()
let undofile = undofile(expand('%'))
if filereadable(undofile)
let undofile = escape(undofile,'%')
exec "rundo " . undofile
endif
endfunc
func! WriteUndo()
let undofile = escape(undofile(expand('%')),'%')
exec "wundo " . undofile
endfunc
set conceallevel=1 " Conceal text where syntax rules say to
set concealcursor=niv " Even conceal text while the cursor is on the same line, in normal, insert, visual
endif
colorscheme molokai
hi Special ctermbg=none
" Pathogen bundles enable!
call pathogen#infect()
" Yankstack
call yankstack#setup()
" Syntastic
"set statusline+=%#warningmsg#
"set statusline+=%{SyntasticStatuslineFlag()}
"set statusline+=%*
let g:syntastic_enable_signs=1
let g:syntastic_auto_loc_list=1
let g:syntastic_disabled_filetypes=['c','cpp','javascript','js','json']
filetype on " Automatically detect file types
filetype plugin indent on " Enable filetype plugins, indent plugins
autocmd FileType mail,human set formatoptions+=t textwidth=72 " Wrap mail messages to 72 chars
autocmd FileType c,cpp,slang,php,inc set cindent " Use C-style indenting
autocmd FileType html set formatoptions+=tl " l: do not break long lines
autocmd FileType make set noexpandtab shiftwidth=8 " Makefiles need real tabs
" Set correct filetype when editing the current bash command line in Vim
if expand('%:t') =~?'bash-fc-\d\+'
setfiletype sh
endif
let php_baselib = 1 " Highlight PHP funcs
" Syntax .inc files like .php
augroup inc
autocmd BufRead *.inc set filetype=php
augroup END
" Syntax .mhtm
augroup mhtm
autocmd BufRead *.mhtm set filetype=mustache
augroup END
" run syntax check on :mak in PHP files, and interpret errors
autocmd FileType php set makeprg=php\ %
autocmd FileType php set errorformat=%m\ in\ %f\ on\ line\ %l
" run python with :mak in .py files
autocmd FileType python set makeprg=python\ %
" Autocomplete CSS and PHP stuff
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
" Use S instead of ys for surround.vim, since S is redundant for cc anyways
nmap S ys
nmap SS yss
" Single Ctrl-H / Ctrl-L to switch to navigating the quickfix / location list
" Ctrl-J / Ctrl-K to jump to the next / previous quickfix or location
" Double Ctrl-H / Ctrl-L to go to the first / last quickfix or location
function! CtrlArrowForQuickfix()
nnoremap <C-H><C-H> :cfirst<CR>
nnoremap <C-J> :cnext<CR>
nnoremap <C-K> :cprev<CR>
nnoremap <C-L><C-L> :clast<CR>
echo "Navigating Quickfix List"
endfunction
function! CtrlArrowForLocation()
nnoremap <C-H><C-H> :lfirst<CR>
nnoremap <C-J> :lnext<CR>
nnoremap <C-K> :lprev<CR>
nnoremap <C-L><C-L> :llast<CR>
echo "Navigating Location List"
endfunction
silent call CtrlArrowForQuickfix()
nnoremap <C-H> :call CtrlArrowForQuickfix()<CR>
nnoremap <C-L> :call CtrlArrowForLocation()<CR>
" Ctrl-Q to use Command-T
nnoremap <C-Q> :CommandT<CR>
nnoremap <leader>q :CommandT<CR>
let g:CommandTCancelMap=['<C-Q>', '<C-c>', '<Esc>']
" ZQ should qall!, and zq for softer version
nnoremap ZQ :qall!<CR>
nnoremap zq :qall<CR>
"Prevent single letter commands from stomping the redo . command
function! ForgetNormal(chr) range
exec "normal! ".a:chr
endfu
nnoremap X :cal ForgetNormal("X")<CR>
nnoremap x :cal ForgetNormal("x")<CR>
nnoremap ~ :cal ForgetNormal("~")<CR>
nnoremap <Home> ^
inoremap <Home> <C-O>^
" grxy to replace next x with y, like fxry but REPEATABLY with . thanks to repeat.vim
" Go Replace X with Y
" Stomps nearly useless gr builtin
function! ReplaceNext()
let ch1 = nr2char(getchar())
let ch2 = nr2char(getchar())
exec "normal! f" . ch1 . "r" . ch2
silent! call repeat#set("gr" . ch1 . ch2)
endfu
nnoremap gr :cal ReplaceNext()<CR>
" Spacebar inserts a single space, no redo
nnoremap <space> :cal ForgetNormal("i \e")<CR>
" Insert spaces on multiple during block selection, and keep selection, no redo
vnoremap <space> :cal ForgetNormal("gvI \egv")<CR>
" Backspace removes all extraneos spaces on the line(s)
vnoremap <BS> :<C-U>let lol=@/<CR>:<C-U>let omg=&hls<CR>:<C-U>let &hls=0<CR>:'<,'>s/\v([^ ] ) +\|$/\1/<CR>:'<,'>s/\v *$/<CR>:let &hls=omg<CR>:let @/=lol<CR>
nnoremap <BS> :let lol=@/<CR>:let omg=&hls<CR>:let &hls=0<CR>:s/\v([^ ] ) +\|$/\1/<CR>:s/\v *$/<CR>:let &hls=omg<CR>:let @/=lol<CR>
" When reopening a file, always jump to the last cursor position
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal! g'\"" |
\ exe "normal! z." |
\ endif
func! PhpColors()
syn match phpOperatorS "\$" contained display
syn match phpParentS "[({[\]})]" contained
syn match phpMemberSelectorS "->" contained display
syn match phpVarSelectorS "\$" contained display
syn match phpNumberS "-\=\<\d\+\>" contained display
syn match phpNumberS "\<0x\x\{1,8}\>" contained display
syn match phpFloatS "\(-\=\<\d+\|-\=\)\.\d\+\>" contained display
" Identifier
syn match phpIdentifierS "$\h\w*" contained contains=phpEnvVar,phpIntVar,phpVarSelectorS display
syn match phpIdentifierSimply "${\h\w*}" contains=phpOperatorS,phpParentS contained display
syn region phpIdentifierComplex matchgroup=phpParentS start="{\$"rs=e-1 end="}" contains=phpIdentifierS,phpMemberSelectorS,phpVarSelectorS,phpIdentifierComplexP contained extend
syn region phpIdentifierComplexP matchgroup=phpParentS start="\[" end="]" contains=@phpClInsideS contained
" String
if exists("php_parent_error_open")
syn region phpStringDouble matchgroup=phpQuote start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=phpIdentifierS,phpSpecialChar,phpIdentifierSimply,phpIdentifierComplex contained keepend
syn region phpStringDoubleS matchgroup=phpQuoteS start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=phpIdentifierS,phpSpecialChar,phpIdentifierSimply,phpIdentifierComplex contained keepend
syn region phpBacktick matchgroup=None start=+`+ skip=+\\\\\|\\"+ end=+`+ contains=phpIdentifierS,phpSpecialChar,phpIdentifierSimply,phpIdentifierComplex contained keepend
syn region phpStringSingle matchgroup=phpQuote start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@phpAddStrings contained keepend
syn region phpStringSingleS matchgroup=phpQuoteS start=+'+ skip=+\\\\\|\\'+ end=+'+ contained keepend
else
syn region phpStringDouble matchgroup=phpQuote start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=phpIdentifierS,phpSpecialChar,phpIdentifierSimply,phpIdentifierComplex contained extend keepend
syn region phpStringDoubleS matchgroup=phpQuoteS start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=phpIdentifierS,phpSpecialChar,phpIdentifierSimply,phpIdentifierComplex contained extend keepend
syn region phpBacktick matchgroup=None start=+`+ skip=+\\\\\|\\"+ end=+`+ contains=phpIdentifierS,phpSpecialChar,phpIdentifierSimply,phpIdentifierComplex contained extend keepend
syn region phpStringSingle matchgroup=phpQuote start=+'+ skip=+\\\\\|\\'+ end=+'+ contains=@phpAddStrings contained keepend extend
syn region phpStringSingleS matchgroup=phpQuoteS start=+'+ skip=+\\\\\|\\'+ end=+'+ contained keepend extend
endif
syn cluster phpClConstS contains=phpFunctions,phpIdentifierS,phpConditional,phpRepeat,phpStatement,phpOperator,phpRelation,phpStringSingleS,phpStringDoubleS,phpBacktick,phpNumberS,phpFloatS,phpKeyword,phpType,phpBoolean,phpStructure,phpMethodsVar,phpConstant,phpCoreConstant,phpException
syn cluster phpClInsideS contains=@phpClConstS,phpComment,phpLabel,phpParent,phpParentError,phpInclude,phpHereDoc
syn match phpMethodsVarS "->\h\w*" contained contains=phpMethods,phpMemberSelectorS display containedin=phpStringDouble
syn match phpParentC "[{}]" contained display containedin=phpParent
syn match phpParentP "[()]" contained display containedin=phpParent
syn match phpParentB "[][]" contained display containedin=phpParent
hi String ctermfg=228
"hi Identifier ctermfg=none
hi link phpMethodsVar Identifier
hi link phpLabel Repeat
hi phpVarSelector ctermfg=none
"hi phpSpecialChar ctermfg=29
hi phpMemberSelectorS ctermfg=23
hi phpParentS ctermfg=23
hi phpVarSelectorS ctermfg=29
hi phpIdentifierComplex ctermfg=29
hi phpMethodsVarS ctermfg=29
hi phpOperatorS ctermfg=29
hi phpIdentifierS ctermfg=29
"hi phpIdentifierSimply ctermfg=darkmagenta
"hi phpStringSingleS ctermfg=darkmagenta
"hi phpStringDoubleS ctermfg=darkmagenta
"hi phpNumberS ctermfg=darkmagenta
"hi phpFloatS ctermfg=darkmagenta
"hi phpQuoteS ctermfg=darkmagenta
"hi phpParentC ctermfg=white
"hi phpParentP ctermfg=135
hi phpParentB ctermfg=118
syn match phpMethodsVar "[-─][>▶]\h\w*" contained contains=phpMethods,phpMemberSelector display
if version >= 703
syn match ArrowHead contained ">" conceal cchar=▶
syn match ArrowTail contained "-" conceal cchar=─
endif
syn match phpMemberSelector "[-─][>▶]" contains=ArrowHead,ArrowTail,FakeArrowHead,FakeArrowTail
hi conceal ctermfg=none ctermbg=none guibg=none
syn match FakeArrowHead contained "▶"
syn match FakeArrowTail contained "─"
hi FakeArrowHead ctermfg=1 ctermbg=15
hi FakeArrowTail ctermfg=1 ctermbg=15
"Make brace matching look less confusing!
hi MatchParen ctermfg=12 ctermbg=16
call Color256Hax()
"colorscheme molokai
"hi String ctermfg=228
"hi conceal ctermbg=none
"hi phpParentB ctermfg=118
"if &t_Co == 256
" hi Conditional ctermfg=3
" hi Operator ctermfg=3
" hi phpComparison ctermfg=3
" hi Type ctermfg=3
"endif
endfun
autocmd FileType php call PhpColors()
function! CColors()
syn region cBracket transparent matchgroup=cBracketX start='\[\|<::\@!' end=']\|:>' contains=ALLBUT,@cParenGroup,cErrInParen,cCppParen,cCppBracket,cCppString,@Spell
syn match cParenX '[()]'
if version >= 703
syn match ArrowHead contained ">" conceal cchar=▶
syn match ArrowTail contained "-" conceal cchar=─
endif
syn match ArrowFull "->" contains=ArrowHead,ArrowTail
syn cluster cParenGroup add=ArrowTail,ArrowHead
syn cluster cPreProcGroup add=ArrowTail,ArrowHead
syn cluster cMultiGroup add=ArrowTail,ArrowHead
hi cBracketX ctermfg=118
hi cParenX ctermfg=208
hi conceal ctermfg=20 ctermbg=none
hi Macro ctermfg=191
hi String ctermfg=228
hi Label ctermfg=161
call Color256Hax()
"Make brace matching look less confusing!
hi MatchParen ctermfg=12 ctermbg=16
endfun
autocmd FileType c call CColors()
" Indentation-level text objects with vai, vii, yai, yii, dai, dii, etc.
onoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR>
onoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR>
vnoremap <silent>ai :<C-u>cal IndTxtObj(0)<CR><Esc>gv
vnoremap <silent>ii :<C-u>cal IndTxtObj(1)<CR><Esc>gv
function! IndTxtObj(inner)
let curline = line(".")
let lastline = line("$")
let i = indent(line(".")) - &shiftwidth * (v:count1 - 1)
let i = i < 0 ? 0 : i
if getline(".") =~ "^\\s*$"
return
endif
let p = line(".") - 1
let nextblank = getline(p) =~ "^\\s*$"
while p > 0 && (nextblank || indent(p) >= i )
-
let p = line(".") - 1
let nextblank = getline(p) =~ "^\\s*$"
endwhile
if (!a:inner)
-
endif
normal! 0V
call cursor(curline, 0)
let p = line(".") + 1
let nextblank = getline(p) =~ "^\\s*$"
while p <= lastline && (nextblank || indent(p) >= i )
+
let p = line(".") + 1
let nextblank = getline(p) =~ "^\\s*$"
endwhile
if (!a:inner)
+
endif
normal! $
endfunction
nnoremap ' :<C-u>cal InsertOne()<CR>
function! InsertOne()
let ch = nr2char(getchar())
exec "normal i" . ch . ""
endfun
" <Tab> is a movement like f movement, but for 2 chars!
" noremap <Tab> :<C-u>cal DoubleFind()<CR>
function! DoubleFind()
let start = col(".")
let backup = @"
let ch1 = nr2char(getchar())
let ch2 = nr2char(getchar())
while 1
let wasat = col(".")
exec "normal f" . ch1 . "y2l"
if ch1.ch2 == @"
let @" = backup
return
endif
if wasat == col(".")
break
endif
endwhile
exec "normal |" . start
let @" = backup
endfun
" -A command deletes the next A character
"nnoremap - :<C-u>cal FCharX("f")<CR>
function! FCharX(direction)
let start = col(".")
let ch1 = nr2char(getchar())
exec "normal " . a:direction . ch1
if start != col(".")
exec "normal x"
endif
endfun
" dx command deletes up to and including a [, <, ( or { and the matching }, ), > or ]
nnoremap dx :<C-u>cal DelToBraceAndMatch()<CR>
" d' command deletes up to and including a quote, and then from a second quote to the end of the line
" effectively 'unwrapping' a string, i.e. for removing the echo '...'; around a string
" then removes any trailing whitespace and/or \n's
nnoremap d' :let search_bak=@/<CR>:s/\v%#[^']*'([^"]*)'.*/\1<CR>:s/\v(\\n\|\s)*$<CR>:let @/=search_bak<CR>
nnoremap d" :let search_bak=@/<CR>:s/\v%#[^"]*"([^"]*)".*/\1<CR>:s/\v(\\n\|\s)*$<CR>:let @/=search_bak<CR>
function! DelToBraceAndMatch()
let start = col(".")
let end = 99999
exec "normal! f{" | if start != col(".") && col(".") < end | let end = col(".") | endif | exec "normal! " . start . "|"
exec "normal! f(" | if start != col(".") && col(".") < end | let end = col(".") | endif | exec "normal! " . start . "|"
exec "normal! f<" | if start != col(".") && col(".") < end | let end = col(".") | endif | exec "normal! " . start . "|"
exec "normal! f[" | if start != col(".") && col(".") < end | let end = col(".") | endif | exec "normal! " . start . "|"
if end < 99999
exec "normal! d" . end . "|%xx"
endif
endfun
" dc deletes this and matching brace, but not what's inside
nnoremap dc :<C-u>cal DelBraceAndMatch()<CR>
function! DelBraceAndMatch()
let tmp = &virtualedit
let &virtualedit = "all"
normal! %%
let start = col(".")
let startln = line(".")
normal! %
if startln == line(".") && start == col(".")
echo "DelBraceAndMatch: no % jump available"
elseif startln == line(".") && col(".") < start
normal! xhx
else
normal! xx
endif
let &virtualedit = tmp
endfun
"Save and load sessions 1-9 -- kind of like a quicksave / quickload!
nnoremap z1 :mks! ~/.vim/sessions/1.vim<CR>
nnoremap z2 :mks! ~/.vim/sessions/2.vim<CR>
nnoremap z3 :mks! ~/.vim/sessions/3.vim<CR>
nnoremap z4 :mks! ~/.vim/sessions/4.vim<CR>
nnoremap z5 :mks! ~/.vim/sessions/5.vim<CR>
nnoremap z6 :mks! ~/.vim/sessions/6.vim<CR>
nnoremap z7 :mks! ~/.vim/sessions/7.vim<CR>
nnoremap z8 :mks! ~/.vim/sessions/8.vim<CR>
nnoremap z9 :mks! ~/.vim/sessions/9.vim<CR>
nnoremap g1 :so ~/.vim/sessions/1.vim<CR>
nnoremap g2 :so ~/.vim/sessions/2.vim<CR>
nnoremap g3 :so ~/.vim/sessions/3.vim<CR>
nnoremap g4 :so ~/.vim/sessions/4.vim<CR>
nnoremap g5 :so ~/.vim/sessions/5.vim<CR>
nnoremap g6 :so ~/.vim/sessions/6.vim<CR>
nnoremap g7 :so ~/.vim/sessions/7.vim<CR>
nnoremap g8 :so ~/.vim/sessions/8.vim<CR>
nnoremap g9 :so ~/.vim/sessions/9.vim<CR>
" Hold Ctrl and use the arrow keys to bubble selected or current line(s)
" Works with block selection horizontally
" Bubble single lines
nnoremap <C-Up> :silent! move .-2<CR>
nnoremap <C-Down> :silent! move .+<CR>
" Bubble multiple lines
vnoremap <C-K> :<C-U>try \| silent! '<,'>move .-2 \| exe "normal! `[V`]" \| finally \| exe "normal! gv" \| endtry<CR>
vnoremap <C-J> :<C-U>try \| silent! '<,'>move '>+ \| exe "normal! `[V`]" \| finally \| exe "normal! gv" \| endtry<CR>
" Bubble multiple lines w/ scrolling
vnoremap <C-Y> :<C-U>try \| silent! '<,'>move .-2 \| exe "normal! `[V`]" \| finally \| exe "normal! gv" \| endtry<CR><C-Y>
vnoremap <C-E> :<C-U>try \| silent! '<,'>move '>+ \| exe "normal! `[V`]" \| finally \| exe "normal! gv" \| endtry<CR><C-E>
" Bubble left and right
vnoremap <C-H> d:<C-U>try \| silent! exe "normal! h" \| finally \| exe "normal! P`[<C-V><C-V>`]" \| endtry<CR>
vnoremap <C-L> dp`[<C-V>`]
augroup texdef
autocmd BufRead *.png.txt set filetype=texdef
augroup END
autocmd FileType texdef call TexDefColors()
function! TexDefColors()
syn keyword Keyword anchor pos size cols flipx flipy floor bump
syn match Define '\v\.(grid|also|end|skip|default)?'
syn match Number '\v<[-+]?[0-9]+>'
syn match Constant '\v<(ce(n|nt|nter?)?|bo(t|tt|ttom?)?|mid(d|dle?)?|top?|le(ft?)?|ri(g|ght?)?)>'
syn match Question '\v\^'
syn match Function '\v^\s*[a-zA-Z_][a-zA-Z_0-9]*(\s|$)'
syn match Comment '^\s*#.*$'
endfunction
"///////////////////////////////////////////////////////////////////
augroup texdef "
autocmd BufRead *col-change* set filetype=colchange "
augroup END "
autocmd FileType colchange call ColChangeColors() "
function! ColChangeColors() "
syn match Function '\v^ *\+.*' "
syn match Statement '\v^ *-.*' "
syn match Question '\v^ *\~.*' contains=Number "
syn match Number '\v\~ *\zs[^ ]*' contained "
endfunction "
"///////////////////////////////////////////////////////////////////
" Decide whether to syntax hilite from the top or not
function! LargeFileSmallFile()
if line2byte(line("$")) < 10000000
set incsearch
syntax sync fromstart
else
set noincsearch
syntax sync minlines=50
endif
endfunction
autocmd BufReadPost * call LargeFileSmallFile()
" Readable colors for vimdiff
highlight DiffAdd cterm=none ctermbg=0
highlight DiffChange cterm=none ctermbg=none
highlight DiffDelete cterm=none ctermfg=0 ctermbg=none
highlight DiffText cterm=none ctermbg=0
" Fold and FoldColumn colors
highlight Folded term=standout ctermfg=0 ctermbg=7
highlight FoldColumn term=standout cterm=bold,reverse ctermfg=0 ctermbg=0
" Almost invisible line numbers
hi LineNr ctermfg=234 ctermbg=232
" Use matchit in mustache/htm files
autocmd FileType mustache source ~/.vim/plugin/matchit.vim
" Don't lose the selection when changing indentation
vnoremap > >gv
vnoremap < <gv
" Make I work as expected in linewise visual mode
vnoremap I <ESC>`<<C-V>`>I
" R replaces block with whitespace and then inserts per line
vnoremap R r gvI
" Close the scratch window (if open) when exiting insert mode (but not for the
" command window (or any vimscript buffer really))
autocmd InsertLeave * if pumvisible() == 0|silent! pclose|endif
" Adjustment for 256 color terms
function! Color256Hax()
if &t_Co == 256
"hi Statement ctermfg=3
" Spiffy colors for vimdiff / folding
highlight DiffAdd cterm=none ctermbg=17
highlight DiffDelete cterm=none ctermfg=52 ctermbg=52
highlight DiffChange cterm=none ctermbg=234
highlight DiffText cterm=none ctermbg=17
highlight Folded term=standout ctermfg=0 ctermbg=7
highlight FoldColumn term=standout cterm=bold,reverse ctermfg=0 ctermbg=0
endif
endfunction
call Color256Hax()
" - and _ to jump to next/prev underscore char, or just to it in operator-pending mode
nnoremap - F_
nnoremap _ f_
onoremap - T_
onoremap _ t_
nnoremap d- dF_
nnoremap d_ df_
vnoremap - F_
vnoremap _ f_
" < and > to help del/replace text up to the next tag, or end of current tag?
onoremap > t>
onoremap < t<
nnoremap d> dt>
nnoremap d< dt<
nnoremap >> >>
nnoremap << <<
" insert mode hax!
inoremap <C-H> <Left>
inoremap <C-J> <Down>
inoremap <C-K> <Up>
inoremap <C-L> <Right>
inoremap <C-W> <C-O>w
inoremap <C-B> <C-O>b
" cmdline mode hax!
cnoremap <C-H> <Left>
cnoremap <C-J> <Down>
cnoremap <C-K> <Up>
cnoremap <C-L> <Right>
" restore stomped-over CTRL-K ✓
inoremap <C-Z> <C-K>
cnoremap <C-Z> <C-K>
" :w in insert mode WOO!
inoremap :w<CR> <ESC>:w<CR>
" Q for opposite of J
nnoremap Q i<CR><C-C>
" Figure out files' indentation patterns automatically
let g:detectindent_preferred_expandtab = 1
let g:detectindent_preferred_indent = 2
autocmd BufReadPost * :DetectIndent
" Quick calculator in insert mode
inoremap <C-Q> <C-O>diW<C-R>=<C-R>"<CR>
autocmd BufRead *.c nnoremap <CR> :e <C-R>%<BS>h<CR>
autocmd BufRead *.h nnoremap <CR> :e <C-R>%<BS>c<CR>
" Search for selected text, forwards or backwards.
vnoremap <silent> * :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy/<C-R><C-R>=substitute(
\escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
vnoremap <silent> # :<C-U>
\let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
\gvy?<C-R><C-R>=substitute(
\escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
\gV:call setreg('"', old_reg, old_regtype)<CR>
" Ctrl-O-O in insert mode to get cursor inside block when this: if(...) ¬ { ¬ } <CURSOR>
inoremap <C-O><C-O> <C-O>O
inoremap <C-O><C-A> <C-O>A
inoremap <C-O>a <C-O>A
inoremap <C-O><C-I> <C-O>I
inoremap <C-O>i <C-O>I
" Jump to next / prev ( or ) with ( and )
nnoremap ) :let search_bak=@/<CR>:cal search("[()]")<CR>:let @/=search_bak<CR>
nnoremap ( :let search_bak=@/<CR>:cal search("[()]","b")<CR>:let @/=search_bak<CR>
" Function Keys! F-Keys!
" F1 reload .vimrc and rerun autocmds!
nnoremap <F1> :so $MYVIMRC \| e %<CR>
" F2 format SQL
nnoremap <F2> V:!sqlformat --reindent -<CR>
vnoremap <F2> :!sqlformat --reindent -<CR>
" F3 to search-again in all files in Git
func! GitGrep(...)
let save = &grepprg
set grepprg=git\ grep\ -n\ $*
let s = 'grep'
for i in a:000
let s = s . " " . i
endfor
exe s
let &grepprg = save
endfun
command! -nargs=? GitGrep call GitGrep(<f-args>)
nnoremap <F3> :cope \| GitGrep '<C-R>=substitute(substitute(substitute(@/,"'","'\\\\''","g"),"\|","\\\\\\\\\|","g"),"\\\\v","","g")<CR>'<Left><CR>
" F4 runs git grep for the word under the cursor
nnoremap <F4> :normal! "zyiw<CR>:cope<CR>:GitGrep z<CR><CR>
" F5 runs make
nnoremap <F5> :mak<up><CR>
" F6 to temporarily disable highlighing
noremap <F6> :nohls<CR>
" Press F7 to run the selected PHP code and replace it with the results
nnoremap <F7> gv:!php -a \| tail -n +3<CR>
vnoremap <F7> :!php -a \| tail -n +3<CR>
" F8 toggle paste / nopaste
set pastetoggle=<F8>
" F9 toggle line wrapping
nnoremap <F9> :set wrap!<CR>
" F10 toggle list chars
nnoremap <F10> :set list!<CR>
" F11 counts matches (depends on default /g!)
nnoremap <F11> :%s///n<CR>
" F12 for reformatting a csv file
nnoremap <F12> :%s@\v^([^,]*),([^,]*),([^,]*)$@\1 \2 \3
" C-F12 inserts incrementing numbers in front of all current search matches
nnoremap <C-F12> :let i=1 \| g~\(<C-R>/\)\@=~s~~\=printf("%02d",i)~ \| let i=i+1
" convert from PHP var to Mustache and go to next search hit
nmap <leader>m xhveolS}gvS}n
" g<F12> to view current buffer in Chromium w/ syntax colors
nnoremap g<F12> :TOhtml<CR>:w! /tmp/to.html<CR>:q!<CR>:!chromium-browser /tmp/to.html<CR>
" Columnize
function! Columnize(...) range abort
let firstline = a:firstline
let lastline = a:lastline
if firstline >= lastline
let firstline = line("'<")
let lastline = line("'>")
if firstline >= lastline
let firstline = line('.')
let lastline = firstline + str2nr(input("Number of lines to Columnize: ")) - 1
if firstline >= lastline
echo "Error: Columnizing requires a valid range"
return
endif
endif
endif
let origsearch = @/
exec "normal! :".firstline.",".lastline."s/\\v$/#/\n"
let stcol = 1
let i = 0
while a:0 == 0 || l:i < a:0
let @/ = ""
redraw
if( a:0 > 0 )
let patt = a:000[l:i]
else
let patt = input(stcol."| Enter column delimiter (Esc or empty to finish): ")
endif
if empty(patt) | break | endif
let bestcol = stcol
" find the farthest out delimiter, then push them all out as far
for firstpass in [1,0]
for nl in range(firstline,lastline)
exec "silent! normal! ".nl."G".stcol."|/\\v($|(\\V".patt."\\v))\n"
let thiscol = col('.')
if line('.') != nl | continue | endif
if thiscol == stcol | continue | endif
if firstpass
if thiscol > bestcol | let bestcol = thiscol | endif
else
exec "normal! i" . repeat(' ',bestcol - thiscol)
endif
endfor
endfor
" continue columnizing only right of stcol
let stcol = bestcol + strlen(patt) - 1
let i = l:i + 1
endwhile
exec "normal! :".firstline.",".lastline."s/\\v\\s*#$//\n"
exec ":redraw | echo 'Columnized ".(lastline-firstline+1)." lines, ".(l:i+1)." columns'"
let @/ = origsearch
endfunction
noremap <C-S> :call Columnize()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment