How to better edit Java files in vim

Source: Internet
Author: User
Tags definition comments final imap resource linux
This time I'll explain how to better edit Java files in vim.
Author: camry.wu@gmail.com

1. Javabrowser
When editing a class file, the class resource tree is a useful thing to make the class members, methods, and so on very intuitive
Show it. Vim can also be implemented as if VC, JBuilder, such as the class resource tree, of course, than
It would be easier, but it would be easy enough. This is accomplished with the Javabrowser.vim script.

First go to vim online to find this script file. (There are a number of other useful scripts that can be found here)
Then on Google find the Ctags installation method, (General Linux system with this program)

1.1 Copy the Javabrowser.vim to the ~/.vim/plugin/in the Linux system.
1.2 Then configure the ~./VIMRC as follows: (that is, the path to the Ctags program)
Let Javabrowser_ctags_cmd = '/usr/bin/ctags '
1.3 If Vim is not a GUI version, then you can make the following settings, it will not change the window
Let Javabrowser_inc_winwidth = 0
1.4 Re-running VIM
1.5 When you open a Java file, enter: Javabrowser will be able to see the beautiful resource tree.
But this file must be a real-world file.

Javabrowser.vim can be copied to the plugin under the VIM directory in the Windows system.
You also need to set the Javabrowser_ctags_cmd path to the path of the Ctags program.

I usually set up in the Java macro file
Nmap <F11>:javabrowser<cr>
IMAP <F11> <ESC><F11>

So I can press F11 key to see this thing, very convenient.

2. Jcommenter
Programming is sure to write annotations, and writing Java files requires a type of Javadoc annotation.
Jcommenter.vim This script can help you write notes easily.
To vim online you can find this script, download it and put it in your directory, and I'll put it in the
~/.vim/files/directory, this directory really put a lot of things.

You must read the script before you use it, and of course we don't want to read it when we edit any file,
To be further explained in the third section. Read the script and enter it in different places: Call Jcommentwriter ()
You can automatically write comments. It is very comfortable to use the habit.

I generally define <F12> as a write comment.

3. Autocmd
Autocmd is very powerful and I've already mentioned it in the VIM experience article. Now explain
How to get it to support the writing of Java files.

This is how I set up my autocmd in the. VIMRC:
: autocmd!
Autocmd FileType Java source ~/.vim/files/java.vim
Autocmd FileType Java source ~/.vim/files/jcommenter.vim
Autocmd bufnewfile *.java 0r ~/.vim/files/skeletons/java.skel
Autocmd Bufnewfile *.java Normal GNP


The description is as follows:
1. Autocmd prior to cancellation
2. If you are editing a Java file, read the Java.vim macro definition (defined in the following section)
3. If you are editing a Java file, read the JCOMMENTER.VIM definition
4. Copy the contents of the Java.skel in advance when you create a new Java file
5. After reading the Java.skel content, run the GNP Macro command

My Java.skel content is this:

/*
* -----------------------------------------------------------
* File name: _filename_
* Authors:camry (camry.wu@gmail.com)
* Created: _datetime_
* Copyright: (c) Vitular Inc. All Rights Reserved.
*
* Modifications:
*
* -----------------------------------------------------------
*/
Package com.vitular.;

/**
* To-do.
*
* @author Camry (camry.wu@gmail.com)
* @version 1.0
*/
public class _classname_ {

/**
* Default constructor.
*/
Public _classname_ () {
Super ();
}//End: _classname_

/**
* for Test.
* @param args Commond line parameters
public static void Main (final string[] args) {
}//End:main
*/
}//End: _classname_
///:~


The macro command to run GNP is mainly to replace the _classname_, _datetime_, _filename_
These three variables. The GNP command is specified in the JAVA.VIM macro file.

4. Java macro definition
Here is a description of how my Java.vim macro file is defined.
First, the file contains another macro definition, where
Here are some of my own definitions:

The input conversion
abbr sprt System.out.println
abbr psf public static final

"Will _filename_, _datetime_, _classname_ into the correct name
"Note that the following two lines should be a line of text linked together
Map GNP:%s/_filename_/\=bufname ("%")/:%s/_datetime_/\=strftime ("%c")
/go "%p$f.bd0f.dviwx:%s/_classname_/\=strtrans" (@)/ggdd:1/{

"Input <F11> can see the class resource tree
Nmap <F11>:javabrowser<cr>
IMAP <F11> <ESC><F11>

"Input <F12> will be able to write comments directly."
Nmap <F12>: Call Jcommentwriter () <CR>
IMAP <F12> <ESC><F12>

Command mode, rows like private String s; /** Comment * * Type the text on the GSG
"can automatically become a get set.
"Note that the following three lines should be a line of text linked together
Map GSG 0f;dw "Ad0wwviw" Bxwviw "Cx0da" apapublic void Set "
Cpb3~ $a (final "BPA" CPA) {this. " CPA = "CPA;}"
Public ' BPA get ' cpb3~ $a () {return "CPA;} 0


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.