FANDOM


 
(Added to Automated_Text_Insertion and C Categories ; code reformatted)
Line 11: Line 11:
 
Below is a tip that the C/C++ Newbies may find interesting and handy to use. The following code will add a function heading and position your cursor just after Description so that one can document as one proceeds with code.
 
Below is a tip that the C/C++ Newbies may find interesting and handy to use. The following code will add a function heading and position your cursor just after Description so that one can document as one proceeds with code.
   
+
<pre>
 
 
function FileHeading()
 
function FileHeading()
 
 
let s:line=line(".")
 
let s:line=line(".")
 
 
call setline(s:line,"/***************************************************")
 
call setline(s:line,"/***************************************************")
 
 
call append(s:line,"* Description - ")
 
call append(s:line,"* Description - ")
 
 
call append(s:line+1,"* Author - Mohit Kalra")
 
call append(s:line+1,"* Author - Mohit Kalra")
 
 
call append(s:line+2,"* Date - ".strftime("%b %d %Y"))
 
call append(s:line+2,"* Date - ".strftime("%b %d %Y"))
 
 
call append(s:line+3,"* *************************************************/")
 
call append(s:line+3,"* *************************************************/")
 
 
unlet s:line
 
unlet s:line
 
 
endfunction
 
endfunction
 
 
   
 
imap &lt;F4&gt; &lt;esc&gt;mz:execute FileHeading()&lt;RET&gt;`zjA
 
imap &lt;F4&gt; &lt;esc&gt;mz:execute FileHeading()&lt;RET&gt;`zjA
+
</pre>
 
   
 
Where &lt;esc&gt; stands for ^V+ESC and &lt;RET&gt; for ^V+ENTER
 
Where &lt;esc&gt; stands for ^V+ESC and &lt;RET&gt; for ^V+ENTER
Line 42: Line 32:
   
 
What key has to be typed for this to be working. I am fairly new to this board.
 
What key has to be typed for this to be working. I am fairly new to this board.
imap &lt;F4&gt; mz:execute FileHeading()`zjA
+
imap &lt;F4&gt; mz:execute FileHeading()`zjA
   
 
The last 3 chars (zjA) is it typo ? or I have to include in file?
 
The last 3 chars (zjA) is it typo ? or I have to include in file?
 
   
   
Line 53: Line 42:
 
to get this working type F4 in insert mode.
 
to get this working type F4 in insert mode.
 
the `zja is not a typo. Observe that :
 
the `zja is not a typo. Observe that :
1. mz is actually marking the first line that is inserted becoz of this macro. That is, it remembers the position of /******
+
# mz is actually marking the first line that is inserted becoz of this macro. That is, it remembers the position of /******
2. `z actually takes the cursor to that marker.
+
# `z actually takes the cursor to that marker.
3. j will take you one line below that marked line.
+
# j will take you one line below that marked line.
4. A will put you in Append mode at the end of the line where the cursor is.
+
# A will put you in Append mode at the end of the line where the cursor is.
 
Effectively, you have a cursor that is in insert mode and is positioned right after "Description" so that you can type it then and there itself.
 
Effectively, you have a cursor that is in insert mode and is positioned right after "Description" so that you can type it then and there itself.
   
Line 65: Line 54:
 
----
 
----
 
Very nice tip - it can be used generally everywhere you need some one-stroke-large-block-of-text-inserting. This includes all programming languages and much more.
 
Very nice tip - it can be used generally everywhere you need some one-stroke-large-block-of-text-inserting. This includes all programming languages and much more.
  +
 
I was wondering however if you can insert some block of text as a header for a newly created file? I am thinking about inserting a header like the one show in the tip every time I create a ".java" file.
 
I was wondering however if you can insert some block of text as a header for a newly created file? I am thinking about inserting a header like the one show in the tip every time I create a ".java" file.
   
Line 73: Line 63:
   
 
Put this in your .vimrc file
 
Put this in your .vimrc file
+
:autocmd BufNewFile,BufRead *.java &lt;vim command that you want run&gt;
:autocmd BufNewFile,BufRead *.java &lt;vim command that you want run&gt;
 
   
 
I use it to read a stored program heading into my programs
 
I use it to read a stored program heading into my programs
:autocmd BufNewFile,BufRead *.C r ~/style/mainh
+
:autocmd BufNewFile,BufRead *.C r ~/style/mainh
   
 
D
 
D
Line 93: Line 83:
   
 
here's one I wrote for a friend whose messing around with c&#35; and getting bored of writing!:-
 
here's one I wrote for a friend whose messing around with c&#35; and getting bored of writing!:-
+
<pre>
 
&#35;&#35;&#35; put the following in you vimrc
 
&#35;&#35;&#35; put the following in you vimrc
 
 
command -nargs=+ CSprop :call Prop(&lt;f-args&gt;)
 
command -nargs=+ CSprop :call Prop(&lt;f-args&gt;)
   
 
&#35;&#35; function to do the business!
 
&#35;&#35; function to do the business!
 
 
function Prop(return,property,name)
 
function Prop(return,property,name)
 
normal o&lt;Esc&gt;
 
normal o&lt;Esc&gt;
Line 117: Line 105:
 
call setline(currline, "}")
 
call setline(currline, "}")
 
endfunction
 
endfunction
+
</pre>
 
...so now, with that loaded in when you type the following command in VIM
 
...so now, with that loaded in when you type the following command in VIM
   
:CSprop returnval propval nameval
+
:CSprop returnval propval nameval
   
 
the following text will be inserted in your script:-
 
the following text will be inserted in your script:-
+
<pre>
 
public returnval propval
 
public returnval propval
 
{
 
{
Line 129: Line 117:
 
set { this.nameval = value; }
 
set { this.nameval = value; }
 
}
 
}
+
</pre>
 
hope that helps :)
 
hope that helps :)
   
Line 138: Line 126:
 
----
 
----
 
when i use command similar to the one below to add stored header to my new program file, it works. However every time i reopen the file, the header is getting added, which is not i want. What is going wrong here?
 
when i use command similar to the one below to add stored header to my new program file, it works. However every time i reopen the file, the header is getting added, which is not i want. What is going wrong here?
+
:autocmd BufNewFile,BufRead *.C r ~/style/mainh yahoo.com
:autocmd BufNewFile,BufRead *.C r ~/style/mainh yahoo.com
 
   
 
haritha_gr--AT--
 
haritha_gr--AT--
Line 150: Line 138:
 
Can someone please help me with the errors that I see with Mohit's function? I get the following error when I use the script as is -
 
Can someone please help me with the errors that I see with Mohit's function? I get the following error when I use the script as is -
   
1. When I hit "F4" (one key) in insert mode I see the following line appear in the command mode -
+
# When I hit "F4" (one key) in insert mode I see the following line appear in the command mode -
:execute FileHeading()&lt;RET&gt;`zjA
+
:execute FileHeading()&lt;RET&gt;`zjA
2. Then the lines are added to the file but VIM complains that -
+
# Then the lines are added to the file but VIM complains that -
  +
<pre>
 
E121: Undefined variable: RET
 
E121: Undefined variable: RET
 
E15: Invalid Expression: FileHeading()&lt;RET&gt;`zjA
 
E15: Invalid Expression: FileHeading()&lt;RET&gt;`zjA
+
</pre>
 
TIA
 
TIA
 
-sanjay
 
-sanjay
Line 162: Line 150:
 
fpgabuilder-vim--AT--yahoo.com
 
fpgabuilder-vim--AT--yahoo.com
 
, October 20, 2005 14:32
 
, October 20, 2005 14:32
----
 
Instead of using &lt;RET&gt; user &lt;CR&gt;
 
--&gt;imap &lt;F4&gt; &lt;esc&gt;mz:execute FileHeading()&lt;CR&gt;`zjA
 
 
Eco_15--AT--yahoo.com
 
, November 12, 2005 6:38
 
 
----
 
----
 
Instead of using &lt;RET&gt; use &lt;CR&gt;
 
Instead of using &lt;RET&gt; use &lt;CR&gt;
Line 191: Line 173:
 
----
 
----
 
<!-- parsed by vimtips.py in 0.553465 seconds-->
 
<!-- parsed by vimtips.py in 0.553465 seconds-->
  +
  +
[[Category:Automated Text Insertion]]
  +
[[Category:C]]

Revision as of 13:11, August 14, 2007

Previous TipNext Tip

Tip: #141 - Add your function heading with a keystroke

Created: October 18, 2001 22:36 Complexity: basic Author: Mohit Kalra kmohit--AT--in.ibm.com Version: 5.7 Karma: 84/32 Imported from: Tip#141

Below is a tip that the C/C++ Newbies may find interesting and handy to use. The following code will add a function heading and position your cursor just after Description so that one can document as one proceeds with code.

function FileHeading() 
 let s:line=line(".") 
 call setline(s:line,"/***************************************************") 
 call append(s:line,"* Description - ") 
 call append(s:line+1,"* Author - Mohit Kalra") 
 call append(s:line+2,"* Date - ".strftime("%b %d %Y")) 
 call append(s:line+3,"* *************************************************/") 
 unlet s:line 
endfunction 

imap <F4> <esc>mz:execute FileHeading()<RET>`zjA 

Where <esc> stands for ^V+ESC and <RET> for ^V+ENTER

Comments

Hi:

What key has to be typed for this to be working. I am fairly new to this board.

imap <F4> mz:execute FileHeading()`zjA 

The last 3 chars (zjA) is it typo ? or I have to include in file?


indiaguru100--AT--yahoo.com , November 6, 2002 11:39


to get this working type F4 in insert mode. the `zja is not a typo. Observe that :

  1. mz is actually marking the first line that is inserted becoz of this macro. That is, it remembers the position of /******
  2. `z actually takes the cursor to that marker.
  3. j will take you one line below that marked line.
  4. A will put you in Append mode at the end of the line where the cursor is.

Effectively, you have a cursor that is in insert mode and is positioned right after "Description" so that you can type it then and there itself.

HTH

mohit kalra , November 8, 2002 6:23


Very nice tip - it can be used generally everywhere you need some one-stroke-large-block-of-text-inserting. This includes all programming languages and much more.

I was wondering however if you can insert some block of text as a header for a newly created file? I am thinking about inserting a header like the one show in the tip every time I create a ".java" file.

Filip K. , November 4, 2003 14:44


"I was wondering however if you can insert some block of text as a header for a newly created file?"

Put this in your .vimrc file

:autocmd BufNewFile,BufRead *.java <vim command that you want run> 

I use it to read a stored program heading into my programs

:autocmd BufNewFile,BufRead *.C r ~/style/mainh 

D

appstate.edu , February 18, 2004 21:52


Is there any way to take input from the keyboard when creating these headers?

I have one set up for functions and classes and it would be great if it asked me for the function name when creating it.

stuart.vim--AT--smgsystems.co.uk , March 24, 2004 9:15


Not sure if this is of use to you and I apologise since it's not the nicest or most elegant way of doing things, but a way to get text you type automatically inserted into code is by using a combination of command and functions.

here's one I wrote for a friend whose messing around with c# and getting bored of writing!:-

### put the following in you vimrc 
command -nargs=+ CSprop :call Prop(<f-args>) 

## function to do the business! 
function Prop(return,property,name) 
 normal o<Esc> 
 let currline = line(".") 
 call setline(currline, "public " . a:return . " " . a:property) 
 normal o<Esc> 
 let currline = currline + 1 
 call setline(currline, "{") 
 normal o<Esc> 
 let currline = currline + 1 
 call setline(currline, "\tget { return this." . a:name . "; }") 
 normal o<Esc> 
 let currline = currline + 1 
 call setline(currline, "\tset { this." . a:name . " = value; }") 
 normal o<Esc> 
 let currline = currline + 1 
 call setline(currline, "}") 
endfunction 

...so now, with that loaded in when you type the following command in VIM

:CSprop returnval propval nameval 

the following text will be inserted in your script:-

public returnval propval 
{ 
 get { return this.nameval; } 
 set { this.nameval = value; } 
} 

hope that helps :)

Andy

andysailing--AT--yahoo.com , October 26, 2004 7:35


when i use command similar to the one below to add stored header to my new program file, it works. However every time i reopen the file, the header is getting added, which is not i want. What is going wrong here?

:autocmd BufNewFile,BufRead *.C r ~/style/mainh yahoo.com

haritha_gr--AT-- , January 27, 2005 12:05


Remove BufRead

r.connell--AT--unb.ca , March 29, 2005 17:11


Can someone please help me with the errors that I see with Mohit's function? I get the following error when I use the script as is -

  1. When I hit "F4" (one key) in insert mode I see the following line appear in the command mode -
:execute FileHeading()<RET>`zjA 
  1. Then the lines are added to the file but VIM complains that -
E121: Undefined variable: RET 
E15: Invalid Expression: FileHeading()<RET>`zjA 

TIA -sanjay


fpgabuilder-vim--AT--yahoo.com , October 20, 2005 14:32


Instead of using <RET> use <CR>

-->imap <F4> <esc>mz:execute FileHeading()<CR>`zjA 

Eco_15--AT--yahoo.com , November 12, 2005 6:38


I use this tip and it works fine. However, I have my .vimrc file saved and sourced when modified in an "au" command. I receive an error "E122", this function already exists. I'm new using Vim, and don't understand this error.

Thanks

TonyB


retiredff--AT--direcway.com , January 29, 2006 8:13


It is very very use full to all.

sabari.us--AT--gmail.com , March 25, 2006 1:34


Community content is available under CC-BY-SA unless otherwise noted.