Vim automatically adds the C language above and after the operator

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.