Vim logo vim online Vim Book Ad

basic Tip #378: Auto insert Java class template when editing a new Java file

 tip karma   Rating 10/10, Viewed by 523 

created:   November 27, 2002 8:50      complexity:   basic
author:   Pete Kazmier      as of Vim:   6.0

If you are lazy like me, tend to use lengthy and verbose Java class names, then this tip is for you.  When creating a new Java class file, the first thing that I do after creating it is to add the following block of text:

public class ClassName
{
}

Rather than have to type the ClassName twice (once when you first opened the new file, and then again for this block), you can use this autocmd to insert that text for you automatically:

  autocmd BufNewFile *.java
    \ exe "normal Opublic class " .  expand('%:t:r') . "\n{\n}\<Esc>1G"


 rate this tip  Life Changing Helpful Unfulfilling 

<<Microsoft Natural Multimedia Keyboard Scancodes | 1,$ s/^M//g  gets rid of control-Ms (windows carriage returns) >>

Additional Notes

vipie @ ulyssis.org, November 27, 2002 17:25
This autocommand generated multiple 'public class..{}' entries sometimes. I have no idea why...

[email protected], November 27, 2002 19:51
added this to add comments on top, constructor and comments on bottom - no big deal

autocmd BufNewFile *.java
   \ exe "normal O/** " . expand('%:t:r') ".java\n/ \n\npublic class " .  expand('%:t:r') .
   \ "\n{\n\tpublic " . expand('%:t:r') "(){}\n}\n//" . expand('%:t:r') ".java \<Esc>1G"
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo