Skip to content

Instantly share code, notes, and snippets.

@bantya
Last active April 23, 2017 11:59
Show Gist options
  • Save bantya/8b57c0c936a9a6486cc48eed79c643ec to your computer and use it in GitHub Desktop.
Save bantya/8b57c0c936a9a6486cc48eed79c643ec to your computer and use it in GitHub Desktop.
Vim: My custom functions file
function! <SID>StripTrailingWhitespaces()
"Get the save last search and cursor position
let _s=@/
let l = line('.')
let c = col('.')
"Strip trailing whitespaces
%s/\s\+$//e
"Restore prev search history and cursor location
let @/=_s
call cursor(l, c)
endfunction
let s:comment_map = {
\ "c": '\/\/',
\ "cpp": '\/\/',
\ "go": '\/\/',
\ "java": '\/\/',
\ "javascript": '\/\/',
\ "lua": '--',
\ "scala": '\/\/',
\ "php": '\/\/',
\ "python": '#',
\ "ruby": '#',
\ "rust": '\/\/',
\ "sh": '#',
\ "desktop": '#',
\ "fstab": '#',
\ "conf": '#',
\ "profile": '#',
\ "bashrc": '#',
\ "bash_profile": '#',
\ "mail": '>',
\ "eml": '>',
\ "bat": 'REM',
\ "ahk": ';',
\ "vim": '"',
\ "tex": '%',
\ }
function! ToggleComment()
if has_key(s:comment_map, &filetype)
let comment_leader = s:comment_map[&filetype]
if getline('.') =~ "^\\s*" . comment_leader . " "
" Uncomment the line
execute "silent s/^\\(\\s*\\)" . comment_leader . " /\\1/"
else
if getline('.') =~ "^\\s*" . comment_leader
" Uncomment the line
execute "silent s/^\\(\\s*\\)" . comment_leader . "/\\1/"
else
" Comment the line
execute "silent s/^\\(\\s*\\)/\\1" . comment_leader . " /"
end
end
else
echo "No comment leader found for filetype"
end
endfunction
"Show highlighting groups for current word
function! SyntCheck()
if !exists('*synstack')
return
endif
echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc
inoremap <silent> <Bar> <Bar><Esc>:call <SID>align()<CR>a
" https://gist.github.com/tpope/287147
function! s:align()
let p = '^\s*|\s.*\s|\s*$'
if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
Tabularize/|/l1
normal! 0
call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
endif
endfunction
" Generate random number
function! Random()
return str2nr(matchstr(reltimestr(reltime()), '\v\.@<=\d+')[1:])
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment