Vim sets the multi-line comment shortcut

Source: Internet
Author: User

Vim script. (Transfer)
Remember that many ides in the window can annotate the entire code for testing. Restore it after testing. The corresponding functions are also implemented in Linux Vim today.
Comment on a single row: ctrl_c
Comment on multiple lines: "v" first to enter the block selection mode. Select a piece of code. Ctrl_c
Similarly, the restored command is the same as the preceding command. For example, after a row of ctrl_c is commented out, ctrl_c is restored.
Note that the Code should be neat and indented. When selecting a block, make sure that the first line in the block is the leftmost. Otherwise, problems may occur.
For example, in the Code, select:
If (a> 0)
{
Printf ("sssss ");
}
(Ctrl_c) will get:
// If (a> 0)
//{
// Printf ("sssss ");
//}
Select this Code: (ctrl_c) and then add it:

If (a> 0)
{
Printf ("sssss ");
}
The following is a Vim script:
"Function Description: add or delete comments //
"Ing and binding

NMAP: setcomment
IMAP: setcomment
Vmap: setcommentv
Command! -Nargs = 0 setcomment call S: set_comment ()
Command! -Nargs = 0 setcommentv call S: set_commentv ()

"Functions called in non-View Mode
Function! S: set_comment ()
Let lindex = line (".")
Let STR = Getline (lindex)
"Check whether the current Comment row is used
Let commentmsg = s: iscomment (STR)
Call S: set_commentv_line (lindex, commentmsg [1], commentmsg [0])
Endfunction

"Functions called in view mode
Function! S: set_commentv ()
Let lbeginindex = line ("'<") "to get the number of rows in the first row of the view.
Let lendindex = line ("'>") "to get the number of rows in the last row of the View
Let STR = Getline (lbeginindex)
"Check whether the current Comment row is used
Let commentmsg = s: iscomment (STR)
"Set for each row
Let I = lbeginindex
While I <= lendindex
Call S: set_commentv_line (I, commentmsg [1], commentmsg [0])
Let I = I + 1
Endwhile
Endfunction

"Set comments
"Index: number of rows
"POS: column number
"Comment_flag: 0: Add annotator 1: Delete annotator
Function! S: set_commentv_line (index, POs, comment_flag)
Let poscur = [0, 0, 0]
Let poscur [1] = A: Index
Let poscur [2] = A: POS + 1
Call setpos (".", poscur) "to set the cursor position

If a: comment_flag = 0
"Insert //
Exec "normal! I //"
Else
"Delete //
Exec "normal! XX"
Endif
Endfunction

"Check whether the current Comment row is used and return relevant information
"Str: a line of code
Function! S: iscomment (STR)
Let ret = [0, 0] "The first item is whether it is a comment row (0, 1), and the second item is the column to be processed,
Let I = 0
Let strlen = Len (A: Str)
While I "Space and Tab can be the" // "prefix
If! (A: Str [I] = ''| A: Str [I] = '')
Let RET [1] = I
If a: Str [I] = '/' & A: Str [I + 1] = '/'
Let RET [0] = 1
Else
Let RET [0] = 0
Endif
Return ret
Endif
Let I = I + 1
Endwhile
Return [0, 0] "empty string processing
Endfunction

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.