LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-23-2005, 03:42 AM   #1
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Rep: Reputation: 15
i need help with a script for vi


Does anyone know how to write a script to generate a file in vi. What i mean is this
Example:
in the command mode: vi filename.java
in vi texteditor: should appear something like this
Code:
/*##############################################
#                                              #
# File: filename.java                          #
# Date: date created                           #
#                                              #
# Author: yourname                             #
#                                              #
##############################################*/

class filename
{
    public static void main (String [] args)
    {
 
       statements

     }
}


// End of file

Last edited by thtr2k; 03-23-2005 at 04:36 AM.
 
Old 03-23-2005, 04:16 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi,

If I understand you correctly I don't think it is possible to do what you want.

It is possible to check the extension of the given file and do some actions. In your case the extension would be .java and the action would be inserting the java framework into the document. But how would vi know that this is a fresh start and not a piece of java code that has already been edited? (Ok, this could also be done, but would complicate things).

The following could be a solution:

1) create a file with the java frame (called java_frame in this example),
2) create a map to a key that will insert this code.

In your ~/.vimrc put the following line:

map <F12> : 0 r /path/to/java_frame^M

The F12 function key is 'mapped' to read the java_frame file and insert it at position 0 (first line) of the document you are in. The ^M is a carrige return. One way of getting that there is: ctrl-v <enter>

Once you saved your .vimrc and started a vi session (vi filename.java), pressing F12 will insert this frame into your vi session.

Hope this helps.

PS: Do not crosspost!

Last edited by druuna; 03-23-2005 at 06:46 AM.
 
Old 03-23-2005, 05:03 AM   #3
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Simply append the following line in your vimrc:

Code:
au BufNewFile *.java 0:r /path_to_comment_file/comment_file
Here "/path_to_comment_file/comment_file" contains whatever you wish to put in your .java file. The contents of the file would be automatically inserted when you edit a non-existant file with .java extension. When you open existing files, this would have no effect.

To find out more, try :help autocommand inside vim.

HTH.
 
Old 03-23-2005, 05:14 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi,

dustu76 gave a much more elegant solution then I did. Use his solutiuon!

Will leave my 'solution', you never know if it comes in handy
 
Old 03-23-2005, 05:27 AM   #5
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Hey drunna, you know how your solution is better than mine:

1. To enter the text you only press one button & I press none.
2. To avoid the text, you press no buttons & I have to press atleast 4-5.

Total count:
Drunna - 1 keys
Me - 4-5 keys

Drunna WINS !!!!!!!!!!

(Yeah okay, I don't have much work today...)
 
Old 03-23-2005, 05:35 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
@dustu76 ROFL

If you look at it that way, I do 'win'.......

I was anly looking at the existing vs non-existing issue But I do like the 'On demand' part of my solution (Nope I'm not working for big blue....).

BTW: It's a slow day here too.
 
Old 03-23-2005, 06:24 AM   #7
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
i must say thank you to both of you "druuna & dustu76" you make my life easier. but if you could do a liltle more to work on the lines:
File: filename.java -----> filename is the name that is copied when the user entered (ie user type vi xxx.java then xxx is put in File: xxx.java) and the same for "class filename"

Date: date ----> date is automaticly generated when the user create the file

i really appreciate your efforts,
thtr2k
 
Old 03-23-2005, 06:46 AM   #8
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
I suggest you write a shell script which would check for file existence etc. and accordingly create a file (with date, filename values pre-populated). Then you can use alias vi=shell_script ...

This solution would be more portable/flexible and should work with any editor.

HTH.
 
Old 03-23-2005, 09:47 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi again,

I must agree that it would be easier to create a shell script, but it can be done with vim. Expanding on my previous example. Ie: java_frame file should be present and look like this:

java_frame
Code:
/*##############################################
#                                              #
# File:
# Date:
#                                              #
# Author: yourname                             #
#                                              #
##############################################*/

class filename
{
    public static void main (String [] args)
    {

       statements

     }
}


// End of file
Put this in your .vimrc:
Code:
map <F12> :call Java_Frame()<CR>
fun! Java_Frame()
  : 0 r /path/to/java_frame
  " insert current file
  exe "%s/File:/File: " .
\ expand("%t")
  " insert date/time stamp
  exe "%s/Date:/Date: " .
\ strftime("%a %b %d, %Y  %I:%M%p")
endfun
After opening a file, pressing F12 will insert the java_frame and update the File: and Date: fields. Only thing that I did not look at is the missing # at the end of those two lines.

Gave me something to do
Hope this shows (some of) the power of vim.
 
Old 03-23-2005, 07:42 PM   #10
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
hi druuan,
i tried the code
Code:
map <F12> :call Java_Frame()<CR>
fun! Java_Frame()
  : 0 r /path/to/java_frame
  " insert current file
  exe "%s/File:/File: " .
\ expand("%t")
  " insert date/time stamp
  exe "%s/Date:/Date: " .
\ strftime("%a %b %d, %Y  %I:%M%p")
endfun
and it doesn't work.

you said "write a shell script" i wonder how to write it and does it mean that we have to put the new script into the .vimrc file as well?
plus: i tried with the uni account, uni uses unix (Sun Os) and it seems to me that it doesn't have .vimrc file.
thtr2k

Last edited by thtr2k; 03-23-2005 at 07:50 PM.
 
Old 03-23-2005, 10:44 PM   #11
thtr2k
Member
 
Registered: Mar 2005
Location: Australia
Posts: 71

Original Poster
Rep: Reputation: 15
so happy this really is an awesome code i figured out from reading helps, but i still want to edit a little more
Code:
autocmd BufNewFile *.java ks|call JavaHeader()|'s
fun JavaHeader()
   call append(0,"/*###########################################################")   
   call append(1," *# Class Name:    " . substitute(bufname("%"),".java$","",""))
   call append(2," *#                                                         #")
   call append(3," *# Author:        Your Name                                #")
   call append(4," *# Creation Date: " . strftime("%A, %B %d %Y, %H:%M "))  
   call append(5," *# Last Modified: " . strftime("%A, %B %d %Y, %H:%M "))    
   call append(6," *#                                                         #")
   call append(7," *# Class Description:                                      #")
   call append(8," *#                                                         #")
   call append(9," *###########################################################*/")
   call append(10,"")
   call append(11," // code starts here")
   call append(12,"public class " . substitute(bufname("%"),".java$","",""))
   call append(13,"{")
   call append(14,"   {")
   call append(15,"    puclic static void main(String[] args)")
   call append(16,"    {")
   call append(17," ")
   call append(18," //insert your code here")
   call append(19," ")
   call append(20,"    }")
   call append(21,"}")
   call append(22,"//Code ends here")
endfun
if anyone can help me work on lines:

1) call append(1,"...") the # place at the end of line
2) call append(3,"...") the "Your Name" is replaced by the name of user automatically
3) call append(4,"... ") the #

Last edited by thtr2k; 03-23-2005 at 11:08 PM.
 
Old 03-24-2005, 12:22 AM   #12
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
thtr2k:

If you dont mind, in druuna's script, try the following changes.
Open your vimrc and replace <F12> (angle signs included) with actual F12. To do this, remove <F12>, press Control+v & then press F12.
Similarly for <CR> press Control+V & then Enter.

Should work fine on Sun OS.

HTH.
 
Old 03-24-2005, 04:43 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi,

If it's not done on a pc, there is the possibilitie that the higher function keys are not used/accessible. Try it with another (lower) function key or use another key combo.

Also: Not all *NIX flavors come with vim (in linux vi is linked to vim, most of the time). And vi != vim.......

About the # on the end of dynamic lines:
You need to check the length of the string that is automatically added and adjust the whitespace behind it accordingly. Sounds easier than it is, especially if you are not a vim programmer (like me )

About inserting the username:
Change this line
call append(3," *# Author: Your Name #")
With this:
call append(3," *# Author: " . expand($USER))

Using my code, insert this above "insert current date/time stamp"
" insert username
exe "%s/Author :/Author : " .
\ expand("$USER")


Hope this helps.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 11:23 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 05:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration