Skip to content

Instantly share code, notes, and snippets.

@Raimondi
Last active September 28, 2015 10:38
Show Gist options
  • Select an option

  • Save Raimondi/1426433 to your computer and use it in GitHub Desktop.

Select an option

Save Raimondi/1426433 to your computer and use it in GitHub Desktop.
Vim snippets
" Count matches in a string.
function! s:count(str, pat)
let cnt = 1
let index = matchend(a:str, a:pat)
while index >= 0
let cnt += 1
let index = matchend(a:str, a:pat, index)
endwhile
return cnt - 1
endfunction
" Filter duplicates:
call filter(list, 'count(list, v:val) == 1')
" Match double quoted strings:
/"\%(\\.\|[^\\"]\)*"/
" Match Vim's single quoted strings:
/'\%(''\|[^']\)*'/
" Name of syntax item under the cursor
nore <leader>n :echo synIDattr(synID(line("."), col("."), 1), "name")<CR>
" Syntax stack of item under the cursor
nnore <leader>s :<C-U>echo join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), ' => ')<CR>
" Color of syntax item under the cursor
echo synIDattr(synIDtrans(synID(line("."), col("."), 1)), "fg")
" bairui's code, just too good to let it pass.
function! SplitListByCount(list, count)
return map(filter(range(len(a:list)), 'v:val % '.a:count.' == 0'),
\ 'a:list[v:val : a:count -1]')
endfunction
function! SplitListByNumber(list, number)
return !empty(a:list) && len(a:list) % a:number == 0
\ ? map(range(a:number), 'a:list[v:val : v:val + 2]')
\ : []
endfunction
" Is a quickfix window open in any tabpage?
function! QFWinInTab(tabnr) "{{{
return len(filter(range(1, winnr('$')), 'gettabwinvar('.a:tabnr.', v:val, "&buftype") == "quickfix"'))
endfunction "QFWinInTab }}}
function! HasQF() "{{{
return len(filter(range(1, tabpagenr('$')), 'QFWinInTab(v:val)'))
endfunction "HasQF }}}
" Self destructing autogroup
augroup Test
au!
au CursorHold * call append(0, 'apetito')
au VimEnter * exec 'au! Test' | augroup! Test
augroup END
" Get info of a given highlight group
" From jamessan@#vim
" http://vim.pastey.net/148644
let i = 1
while synIDtrans(i) != 0
if synIDattr(i, 'name') == 'Normal'
if synIDattr(i, 'fg') >= 0
" fg is set
endif
if synIDattr(i, 'bg') >= 0
" bg is set
endif
break
endif
let i = i + 1
endwhile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment