Vim Tips Wiki
Register
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 24: Line 24:
 
then substituting 'getline(".")', use 'getline(line(".") - 1)' should work.
 
then substituting 'getline(".")', use 'getline(line(".") - 1)' should work.
   
Put the following in your [[vimrc]]:
+
Put the following in your .vimrc:
 
<pre>
 
<pre>
au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap } <Esc>:call CurlyBracket()<CR>a
+
au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap } &lt;ESC&gt;:call CurlyBracket()&lt;CR&gt;a
   
 
function CurlyBracket()
 
function CurlyBracket()
Line 32: Line 32:
 
iunmap }
 
iunmap }
 
sil exe "normal i}"
 
sil exe "normal i}"
imap } <Esc>:call CurlyBracket()<CR>
+
imap } &lt;ESC&gt;:call CurlyBracket()&lt;CR&gt;
 
let l:result1 = searchpair('{', '', '}', 'bW')
 
let l:result1 = searchpair('{', '', '}', 'bW')
if (result1 > 0)
+
if (result1 &gt; 0)
 
let l:my_string = substitute(getline("."), '^\s*\(.*\){', '\1', "")
 
let l:my_string = substitute(getline("."), '^\s*\(.*\){', '\1', "")
 
sil exe ":" . l:my_linenum
 
sil exe ":" . l:my_linenum
Line 57: Line 57:
   
 
[4] I've added the line
 
[4] I've added the line
au BufNewFile,BufRead *.java,*.c,*.cc,*.C,*.h map <A-F1> :call CurlyBracket()<CR>
+
au BufNewFile,BufRead *.java,*.c,*.cc,*.C,*.h map &lt;A-F1&gt; :call CurlyBracket()&lt;CR&gt;
 
to my .vimrc for those times when you started with a small if statement, and ended up with a mammoth one. Simply calls CurleyBracket to comment it.
 
to my .vimrc for those times when you started with a small if statement, and ended up with a mammoth one. Simply calls CurleyBracket to comment it.
   
Line 65: Line 65:
 
iunmap }
 
iunmap }
 
sil exe "normal i}"
 
sil exe "normal i}"
imap } <Esc>:sil call CurlyBracket()<CR>a
+
imap } &lt;ESC&gt;:sil call CurlyBracket()&lt;CR&gt;a
 
let result1=searchpair('{', '', '}', 'bW')
 
let result1=searchpair('{', '', '}', 'bW')
 
let goline=""
 
let goline=""
 
let appendstr=""
 
let appendstr=""
if (result1 > 0)
+
if (result1 &gt; 0)
 
let l:my_startline=line(".")
 
let l:my_startline=line(".")
if (l:my_linenum - l:my_startline > 10)
+
if (l:my_linenum - l:my_startline &gt; 10)
 
let l:my_string=substitute(getline(line(".")-1),"^ *","","g")
 
let l:my_string=substitute(getline(line(".")-1),"^ *","","g")
 
let l:shortstring=strpart(l:my_string,0,30)."..."
 
let l:shortstring=strpart(l:my_string,0,30)."..."
Line 86: Line 86:
 
Okay. I have changed this script. It takes into account the last note and some other things I noticed.
 
Okay. I have changed this script. It takes into account the last note and some other things I noticed.
   
# This was annoying when I had a short if(){}else{} statement, so I changed the mapping to }<CR>.
+
# This was annoying when I had a short if(){}else{} statement, so I changed the mapping to }&lt;CR&gt;.
 
# If there is an else statement, the comment inserted is the if statement. followed by an else {
 
# If there is an else statement, the comment inserted is the if statement. followed by an else {
# >10 lines check put in.
+
# &gt;10 lines check put in.
# < 30 char check put in
+
# &lt; 30 char check put in
   
 
Notes:
 
Notes:
Line 96: Line 96:
   
 
<pre>
 
<pre>
au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap }<CR> <Esc>:call CurlyBracket()<CR>a
+
au BufNewFile,BufRead *.c,*.cc,*.C,*.h imap }&lt;CR&gt; &lt;ESC&gt;:call CurlyBracket()&lt;CR&gt;a
   
 
function CurlyBracket()
 
function CurlyBracket()
 
let l:startline = line(".")
 
let l:startline = line(".")
 
let l:result1 = searchpair('{', '', '}', 'bW', '')
 
let l:result1 = searchpair('{', '', '}', 'bW', '')
if (result1 > 0)
+
if (result1 &gt; 0)
 
let l:linenum = line(".")
 
let l:linenum = line(".")
 
let l:string1 = getline(l:linenum)
 
let l:string1 = getline(l:linenum)
Line 107: Line 107:
 
sil exe "normal 0"
 
sil exe "normal 0"
 
let l:result2 = searchpair('{', '', '}', 'bW', '')
 
let l:result2 = searchpair('{', '', '}', 'bW', '')
if (l:result2 > 0)
+
if (l:result2 &gt; 0)
 
let l:string1 = getline(".") . l:string1
 
let l:string1 = getline(".") . l:string1
 
endif
 
endif
Line 115: Line 115:
 
sil exe ":" . l:startline
 
sil exe ":" . l:startline
 
sil exe "normal i}"
 
sil exe "normal i}"
if ((l:startline - l:linenum) > 10)
+
if ((l:startline - l:linenum) &gt; 10)
 
sil exe "normal a //" . l:my_string
 
sil exe "normal a //" . l:my_string
 
endif
 
endif
Line 151: Line 151:
 
[2] The number of lines is taken form the start of the "if" (in case of if-else)
 
[2] The number of lines is taken form the start of the "if" (in case of if-else)
   
[3] The "..." is added only if the length of the stringis > 30.
+
[3] The "..." is added only if the length of the stringis &gt; 30.
   
 
=======
 
=======
Line 159: Line 159:
 
" Automatically comments {} in C/C++/Java.
 
" Automatically comments {} in C/C++/Java.
   
au BufNewFile,BufRead *.c,*.cc,*.h imap }<CR> <Esc>:call CurlyBracket()<CR>a
+
au BufNewFile,BufRead *.c,*.cc,*.h imap }&lt;CR&gt; &lt;ESC&gt;:call CurlyBracket()&lt;CR&gt;a
au BufNewFile,BufRead *.cpp,*.C imap }<CR> <Esc>:call CurlyBracket()<CR>a
+
au BufNewFile,BufRead *.cpp,*.C imap }&lt;CR&gt; &lt;ESC&gt;:call CurlyBracket()&lt;CR&gt;a
au BufNewFile,BufRead *.java,*.idl imap }<CR> <Esc>:call CurlyBracket()<CR>a
+
au BufNewFile,BufRead *.java,*.idl imap }&lt;CR&gt; &lt;ESC&gt;:call CurlyBracket()&lt;CR&gt;a
   
 
function CurlyBracket()
 
function CurlyBracket()
 
let l:startline = line(".")
 
let l:startline = line(".")
 
let l:result1 = searchpair('{', '', '}', 'bW')
 
let l:result1 = searchpair('{', '', '}', 'bW')
if (result1 > 0)
+
if (result1 &gt; 0)
 
let l:linenum = line(".")
 
let l:linenum = line(".")
 
let l:string1 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "")
 
let l:string1 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "")
Line 178: Line 178:
 
sil exe "normal 0"
 
sil exe "normal 0"
 
let l:result2 = searchpair('{', '', '}', 'bW')
 
let l:result2 = searchpair('{', '', '}', 'bW')
if (l:result2 > 0)
+
if (l:result2 &gt; 0)
 
let l:linenum = line(".")
 
let l:linenum = line(".")
 
let l:string2 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "")
 
let l:string2 = substitute(getline(l:linenum), '^\s*\(.*\)\s*$', '\1', "")
Line 191: Line 191:
 
let l:my_string = substitute(l:string1, '\s*{[^{]*$', '', "")
 
let l:my_string = substitute(l:string1, '\s*{[^{]*$', '', "")
 
let l:my_strlen = strlen(l:my_string)
 
let l:my_strlen = strlen(l:my_string)
if (l:my_strlen > 30)
+
if (l:my_strlen &gt; 30)
 
let l:my_string = strpart(l:my_string,0,30)."..."
 
let l:my_string = strpart(l:my_string,0,30)."..."
 
endif
 
endif
Line 197: Line 197:
 
sil exe ":" . l:startline
 
sil exe ":" . l:startline
 
sil exe "normal i}"
 
sil exe "normal i}"
if ((l:startline - l:linenum) > 10)
+
if ((l:startline - l:linenum) &gt; 10)
 
sil exe "normal a /* " . l:my_string . " */"
 
sil exe "normal a /* " . l:my_string . " */"
 
endif
 
endif
Line 218: Line 218:
   
 
----
 
----
People facing Problems with running this script can try removing the <Space> after "iunmap }" statement.
+
People facing Problems with running this script can try removing the &lt;space&gt; after "iunmap }" statement.
   
 
----
 
----
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)