LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   i need help with a script for vi (https://www.linuxquestions.org/questions/programming-9/i-need-help-with-a-script-for-vi-304959/)

thtr2k 03-23-2005 03:42 AM

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


druuna 03-23-2005 04:16 AM

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!

dustu76 03-23-2005 05:03 AM

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.

druuna 03-23-2005 05:14 AM

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 ;)

dustu76 03-23-2005 05:27 AM

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 !!!!!!!!!! :D

(Yeah okay, I don't have much work today...)

druuna 03-23-2005 05:35 AM

@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.

thtr2k 03-23-2005 06:24 AM

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

dustu76 03-23-2005 06:46 AM

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.

druuna 03-23-2005 09:47 AM

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.

thtr2k 03-23-2005 07:42 PM

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

thtr2k 03-23-2005 10:44 PM

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 #

dustu76 03-24-2005 12:22 AM

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.

druuna 03-24-2005 04:43 AM

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.


All times are GMT -5. The time now is 07:26 AM.