Vim Tips Wiki
Advertisement

Previous TipNext Tip

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

Created: November 27, 2002 8:50 Complexity: basic Author: Pete Kazmier Version: 6.0 Karma: 19/16 Imported from: Tip#378

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"

Comments

This autocommand generated multiple 'public class..{}' entries sometimes. I have no idea why...


vipie --AT-- ulyssis.org , November 27, 2002 17:25


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"

stelliosk--AT--optushome.com.au , November 27, 2002 19:51


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


lk1--AT--teamten.com , January 10, 2003 10:27


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


bofh--AT--diegeekdie.com , February 12, 2003 5:53


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\/", "", "")



vim--AT--thecrypto.org , July 10, 2004 22:15


Advertisement