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