sponsor Vim development Vim logo Vim Book Ad

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

 tip karma   Rating 19/16, Viewed by 2515 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

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"
[email protected], January 10, 2003 10:27
I use the code below to type in the package name.  Otherwise I often forget to type that in and the class file goes in the wrong place.  It assumes that the top-level directory is "com", which may be wrong but I couldn't think of any other way to do it.

autocmd BufNewFile *.java call InsertJavaPackage()

function! InsertJavaPackage()
    let dir = getcwd()
    let dir = substitute(dir, "^.*\/com\/", "com/", "")
    let dir = substitute(dir, "\/", ".", "g")

    let dir = "package " . dir . ";"

    let result = append(0, "")
    let result = append(1, dir)

    let filename = expand("%")

    let filename = substitute(filename, "\.java", "", "")

    let result = append(2, "")
    let result = append(3, "class " . filename . " {")
    let result = append(4, "}")
endfunction
[email protected], February 12, 2003 5:53
I always have my source code under a src dir and I often stand in the basedir and open new files with the correct path, ie:
vi src/se/hogia/innovation/Test.java

So I changed the function to this:

function! InsertJavaPackage()
    let filename = expand("%")
    let filename = substitute(filename, "\.java$", "", "")

    let dir = getcwd() . "/" . filename
    let dir = substitute(dir, "^.*\/src\/", "", "")
    let dir = substitute(dir, "\/[^\/]*$", "", "")
    let dir = substitute(dir, "\/", ".", "g")

    let filename = substitute(filename, "^.*\/", "", "")

    let dir = "package " . dir . ";"

    let result = append(0, dir)

    let result = append(1, "")
    let result = append(2, "class " . filename . " {")
    let result = append(4, "}")
endfunction
[email protected], July 10, 2004 22:15
If you want to have your packages one level down from a directory like repos, then use
let dir = substitute(dir, "^.*\/repos\/.\\{-}\/", "", "")
instead of
let dir = substitute(dir, "^.*\/repos\/", "", "")


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 the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo