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.
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 ).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With