Skip to content

Instantly share code, notes, and snippets.

@Dubhead
Created June 5, 2012 05:58
Show Gist options
  • Save Dubhead/2872973 to your computer and use it in GitHub Desktop.
Save Dubhead/2872973 to your computer and use it in GitHub Desktop.
a Vim script to insert spaces around punctuations and equals automatically
"""" Spaces Around Punctuations and Equals """"
let s:sape_no_space_before_equal = split('-!~=+*/&|<>%', '\zs')
let s:sape_followed_by_space = split(',;=', '\zs')
function s:sape(c)
if a:c == ' ' || a:c == '\t' || col(".") == 1
return a:c
endif
let prevchar = getline(".")[col(".") - 2]
if prevchar == ' ' || prevchar == '\t'
return a:c
endif
if a:c == "="
if index(s:sape_no_space_before_equal, prevchar) == -1
return " ="
else
return "="
endif
endif
if index(s:sape_followed_by_space, prevchar) == -1
return a:c
else
return "\<space>" . a:c
endif
endfunction
" autocmd InsertCharPre *.{c,cpp,cc,h} let v:char = s:sape(v:char)
" eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment