function! Align_space () let current_line = Getline ('. ') Let replacement = substitute (Current_line, ' \ ([() a-za-z0-9_]\) \ ([!| ^&=<>%*/+-]\) ', ' \1 \2 ', ' g ') let newline = substitute (replacement, ' \ ([!| ^&=<>%*/+-]\) \ ([() a-za-z0-9_]\) ', ' \1 \2 ', ' G ') call setline ('. ', newline) "Call cursor" (Line ('. '), Col ('. ') + 1) endfunction autocmd Cursormovedi *.[ CH] Call Align_space ()
Company code specification requires the operator to add spaces before and after the error
To prevent omissions, write a simple vim function
Using the principle of regular expression substitution to automatically add spaces before and after operators when entering operators
Substitution uses the principle of reverse referencing
() a-za-z0-9_ characters that may appear before and after the match operator
!| ^&=<>%*/+-matching operator
A feeling for vim:
1 VIM document is not comprehensive enough, do not know whether the substitute parameters can not move the cursor
2 If you return in the function, the Autocmd cursormovedi configuration function will not be called again unless you exit and enter insert mode.
Legacy issues:
#include <stdio.h> and + + are still adding spaces it feels weird.
Vim automatically adds the C language above and after the operator