Skip to content

Instantly share code, notes, and snippets.

" Build helptags for all packages even optional ones
command! -nargs=0 -bar Helptags for p in glob('~/.vim/pack/*/opt/*', 1, 1) | exe 'packadd '.fnamemodify(p, ':t') | endfor | silent! helptags ALL
" create parent directories
" http://stackoverflow.com/questions/4292733/vim-creating-parent-directories-on-save
function! s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
" Symbol text objects
for char in [ '_', '.', ':', ';', '<bar>', '/', '<bslash>', '*', '+', '%' ]
execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
endfor
" keep star from jumping
nnoremap <script> <silent> <expr> * <SID>JustHighlight('*')
function! s:JustHighlight(cmd)
" still messes up ' mark
let s:JustHighlight_view = winsaveview()
augroup just_hightlight
autocmd!
autocmd CursorMoved <buffer>
\ execute "autocmd! just_hightlight" |
\ keepjumps call winrestview(s:JustHighlight_view)
@PeterRincker
PeterRincker / var_segment_textobj.vim
Last active May 19, 2021 17:40
Basic textobject support for variable segments
" Basic support for variable segment textobjects
" e.g. "foo" in foo_var and fooBar
xnoremap iv :<c-u>call <SID>segment()<cr>
onoremap iv :normal viv<cr>
xnoremap av :<c-u>call <SID>segment(1)<cr>
onoremap av :normal vav<cr>
function s:segment(...)
let start_pat = '\C\v%(<|_+\zs\w|[a-z0-9]\zs[A-Z])'
@PeterRincker
PeterRincker / diff.md
Last active September 5, 2024 04:12 — forked from romainl/diff.md
:DiffOrig but smarter

:DiffOrig but smarter

This is an enhanced version of the snippet provided under :help diff-original-file.

Where the original :DiffOrig only shows differences between the buffer in memory and the file on disk, :Diff can be used in two ways:

  • against the file on disk, like the original, with:

    :Diff
    
" Cancel searching with `/`, but stay in place
cnoremap <expr> <c-y> <SID>search_cr("\<c-y>", getcmdtype(), getcmdline())
function! s:search_cr(cmd, type, line)
let [key, s:pos, s:search] = [a:cmd, getpos('.'), a:line]
if a:type == '/' || a:type == '?'
let key = "\<c-c>"
augroup search_cr
autocmd!
autocmd CmdlineLeave * execute "autocmd! search_cr" |
@PeterRincker
PeterRincker / voggle.vim
Created April 12, 2018 20:31
Voggle - toggling pairs
" Interpratation of Voggle:
" https://github.com/JasperSmienk/Voggle
"
" Example mapping:
" nmap <cr> <Plug>(voggle)
"
" Customize by creating g:voggle_pairs or b:voggle_pairs Dictionaries
let s:voggle_pairs = {
\ 'true': 'false',
@PeterRincker
PeterRincker / abbrev_complete.vim
Created January 3, 2018 00:20
Complete abbreviations
" Completion for iabbrev's
" example:
" imap <c-x><c-x><c-a> <Plug>(complete-appbrev)
function! s:complete_abbrev()
let lst = split(execute('iabbrev'), '\n')
let lst = map(lst, 'split(v:val, ''\s\+'')[1:]')
let lst = map(lst, '{"word": v:val[0], "menu": substitute(join(v:val[1:], " "), ''^[@*]\s'', "", "")}')
let col = col('.')
let prefix = matchstr(getline('.')[0:col-2], '\k\+$')
@PeterRincker
PeterRincker / Railsabbrev.vim
Created January 2, 2018 23:55
Mimic Rails.vim's :Railsabbrev
" Mimic part of Rails.vim's :Railsabbrev
" aka Abbreviations that will only expand on <tab> or <c-]>
"
" Example usage:
" :Railsabbrev iff if:<left>
" :Railsabbrev fun function!\ ()<cr>endfunction<up><end><left><left>
" Now you can type "iff<tab>" and it will expand to: "if:" with the cursor before the ":"
"
" Supports completion menu of :Railsabbrev via <Plug>(Railsabbrev-complete) mapping