" Fold Text set foldtext=FoldText() function! FoldText() let line = getline(v:foldstart) return line.' '.string(v:foldend - v:foldstart + 1).' lines ' endfunction " Derived from http://stackoverflow.com/a/1333025 function! CloneHighlightGroupWithAttributes(group, new_group, attributes) " Get group details, resolving group links. redir => group exe "silent hi " . a:group redir END while group =~ "links to" let index = stridx(group, "links to") + len("links to") let linked_group = strpart(group, index + 1) redir => group exe "silent hi " . linked_group redir END endwhile " Get highlight parameters and existing remove attributes. let parameters = matchlist(group, '\\s\+\(.*\)')[1] let parameters = substitute(parameters, '\(c\?term\|gui\)=\w\+', '', 'g') " Create the cloned highlight group with new attributes. exe 'hi '.a:new_group.' '.parameters. \' term='.a:attributes.' cterm='.a:attributes.' gui='.a:attributes endfunction function! Synstack() for id in synstack(line('.'), col('.')) let attributes = synIDattr(id, 'name') let attr = synIDattr(id, 'fg') if attr != '' let attributes = attributes.' fg='.attr endif endfor endfunction command Synstack :call Synstack() " Strip trailing whitespace function! StripWhitespace() let l = line(".") let c = col(".") %s/\s\+$//e call cursor(l, c) endfunction command! StripWhitespace :call StripWhitespace() augroup strip_white_space " Strip whitespace on buffer write autocmd! autocmd BufWritePre * :call StripWhitespace() augroup END " Stringify " Make a code block in to a C string literal function! Stringify() " Escape existing escape characters execute 's/\\/\\\\/ge' " Escape quotes execute 's/"/\\"/ge' " Prepend quote execute 's/^/"/g' " Append carriage return, quote execute 's/$/\\n"/g' noh endfunction map s :call Stringify() " Make a C string literal in to a code block function! Destringify() " Remove final quote and carriage return execute 's/\\n"\s*$//ge' " Remove first quote execute 's:^\(\s*\)":\1:ge' " Remove quote escapes execute 's/\\"/"/ge' " Remove escapes of escapes characters execute 's/\\\\/\\/ge' noh endfunction map S :call Destringify() " Invoke terminal command without prompt and then redraw. command! -nargs=+ Silent execute 'silent ' | redraw! " Set the tab width for the current filetype function! TabWidth(width) execute "set tabstop=".a:width execute "set shiftwidth=".a:width execute "set softtabstop=".a:width echo "Tab width is now ".a:width endfunction command! -nargs=1 TabWidth :call TabWidth() " Toggle GitHub style bullet checkbox function! CheckboxToggle() " Get current line let l:line = getline('.') " Get the ' ' or 'x' character from within the task bullet let l:pattern = '[-\*+] \[\zs[ x]\ze]' let l:char = matchstr(l:line, l:pattern) " Toggle the ' ' or 'x' character if l:char == 'x' let l:char = ' ' else let l:char = 'x' endif " Replace the current line with a new one call setline(line('.'), substitute(l:line, l:pattern, l:char, '')) endfunction command! CheckboxToggle :call CheckboxToggle() nnoremap :CheckboxToggle