ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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 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.
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
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.
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
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 #
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.
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")
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.