Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make vim indent java annotations correctly?

Tags:

When indenting java code with annotations, vim insists on indenting like this:

@Test     public void ... 

I want the annotation to be in the same column as the method definition but I can't seem to find a way to tell vim to do that, except maybe using an indent expression but I'm not sure if I can use that together with regular cindent.

edit: The filetype plugin was already turned on I just got a bit confused about indenting plugins. The accepted answer may be a bit hackish but works for me as well.

like image 561
wds Avatar asked Oct 14 '08 12:10

wds


People also ask

How do I set an indent in vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).

How do I indent all codes in vim?

Just go to visual mode in vim , and select from up to down lines after selecting just press = , All the selected line will be indented.

What is vim smart indent?

Vim has four methods of indentation, namely: Autoindent – this method uses indent from the previous line for the file type you are editing. smartindent – smartindent works similarly to autoindent but recognizes the syntax for some languages such as C language.

Why does vim auto indent?

autoindent essentially tells vim to apply the indentation of the current line to the next (created by pressing enter in insert mode or with O or o in normal mode. smartindent reacts to the syntax/style of the code you are editing (especially for C). When having it on you also should have autoindent on.


1 Answers

You shouldn't modify the built-in vim settings. Your changes could disappear after a package upgrade. If you copy it to your .vim, then you won't get any java indent bug fixes.

Instead, put the following into a new file called ~/.vim/after/indent/java.vim

function! GetJavaIndent_improved()     let theIndent = GetJavaIndent()     let lnum = prevnonblank(v:lnum - 1)     let line = getline(lnum)     if line =~ '^\s*@.*$'         let theIndent = indent(lnum)     endif      return theIndent endfunction setlocal indentexpr=GetJavaIndent_improved() 

That way it loads the stock java indent and only modifies the indent to remove the annotation indents.

like image 55
idbrii Avatar answered Oct 23 '22 02:10

idbrii