(Move categories to tip template) |
JohnBeckett (talk | contribs) m (Reverted edits by 179.125.126.103 (talk | block) to last version by JohnBot) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 25: | Line 25: | ||
endfunction |
endfunction |
||
− | imap |
+ | imap <F4> <Esc>mz:execute FileHeading()<RET>`zjA |
</pre> |
</pre> |
||
− | Where |
+ | Where <Esc> stands for ^V+ESC and <RET> for ^V+ENTER |
+ | |||
==Comments== |
==Comments== |
||
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: |
||
− | # mz is actually marking the first line that is inserted |
+ | # mz is actually marking the first line that is inserted because of this macro. That is, it remembers the position of /****** |
# `z actually takes the cursor to that marker. |
# `z actually takes the cursor to that marker. |
||
# j will take you one line below that marked line. |
# j will take you one line below that marked line. |
||
Line 44: | Line 45: | ||
Put this in your .vimrc file |
Put this in your .vimrc file |
||
− | :autocmd BufNewFile,BufRead *.java |
+ | :autocmd BufNewFile,BufRead *.java <vim command that you want run> |
I use it to read a stored program heading into my programs |
I use it to read a stored program heading into my programs |
||
Line 55: | Line 56: | ||
<pre> |
<pre> |
||
### put the following in you vimrc |
### put the following in you vimrc |
||
− | command -nargs=+ CSprop :call Prop( |
+ | command -nargs=+ CSprop :call Prop(<f-args>) |
## function to do the business! |
## function to do the business! |
||
function Prop(return,property,name) |
function Prop(return,property,name) |
||
− | normal o |
+ | normal o<Esc> |
let currline = line(".") |
let currline = line(".") |
||
call setline(currline, "public " . a:return . " " . a:property) |
call setline(currline, "public " . a:return . " " . a:property) |
||
− | normal o |
+ | normal o<Esc> |
let currline = currline + 1 |
let currline = currline + 1 |
||
call setline(currline, "{") |
call setline(currline, "{") |
||
− | normal o |
+ | normal o<Esc> |
let currline = currline + 1 |
let currline = currline + 1 |
||
call setline(currline, "\tget { return this." . a:name . "; }") |
call setline(currline, "\tget { return this." . a:name . "; }") |
||
− | normal o |
+ | normal o<Esc> |
let currline = currline + 1 |
let currline = currline + 1 |
||
call setline(currline, "\tset { this." . a:name . " = value; }") |
call setline(currline, "\tset { this." . a:name . " = value; }") |
||
− | normal o |
+ | normal o<Esc> |
let currline = currline + 1 |
let currline = currline + 1 |
||
call setline(currline, "}") |
call setline(currline, "}") |
||
Line 97: | Line 98: | ||
---- |
---- |
||
− | Instead of using |
+ | Instead of using <RET> use <CR> |
− | -- |
+ | -->imap <F4> <Esc>mz:execute FileHeading()<CR>`zjA |
---- |
---- |
Latest revision as of 08:02, 14 May 2015
created October 18, 2001 · complexity basic · author Mohit Kalra · version 5.7
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[]
To get this working type F4 in insert mode.
The `zja is not a typo. Observe that:
- mz is actually marking the first line that is inserted because of this macro. That is, it remembers the position of /******
- `z actually takes the cursor to that marker.
- j will take you one line below that marked line.
- 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.
"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
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; } }
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
Remove BufRead
Instead of using <RET> use <CR>
-->imap <F4> <Esc>mz:execute FileHeading()<CR>`zjA