inoremap :call SmartLineBreak()A function! SmartLineBreak() if ( &syntax =~ '^\(c\|cpp\|php\|java\)$' ) let current_line = getline( '.' ) let space_at_end = '\s\+$' if match( current_line, space_at_end ) >= 0 :execute ':s/\s*$//' endif let need_brace_on_same_line = '\<\(if\|while\|switch\|do\|foreach\|forever\|enum\|for\)\>' if match( current_line, need_brace_on_same_line ) >= 0 let brace_at_end = '{$' if match( current_line, brace_at_end ) > 0 if match( current_line, '[^ ]{$' ) > 0 :execute ':s/{$/ {/' endif else :execute ':s/$/ {/' endif if match( current_line, '\' ) >= 0 :execute "normal o};\k" else :execute "normal o}\k" endif else let need_brace_on_next_line = '\<\(class\|namespace\|struct\)\>' if match( current_line, need_brace_on_next_line ) >= 0 let brace_at_end = '{$' if match( current_line, brace_at_end ) > 0 :execute ':s/\s*{$//' endif :execute "normal o{" if match( current_line, '\' ) >= 0 :execute "normal o}\k" else :execute "normal o};\k" endif endif endif endif endfunction