[plug] vim and programming languages

Peter Wright pete at akira.apana.org.au
Fri Sep 6 14:41:20 WST 2002


On 03/09 16:32:17, Russell Steicke wrote:
> Thanks to Anthony and Jason.  After some experimentation I've settled
> on:
> 
> ~/.vimrc contains
> 
> :syntax on
> :filetype on
> 
> And my .java source files have a modeline:
> 
> // vim: set shiftwidth=2 expandtab smartindent textwidth=95:
> 
> These seem to achieve the same as the emacs local variable settings as
> in my original question.

----- example extract from pete's ~/.vimrc --------
if has("autocmd")
 augroup cprog
  " Remove all cprog autocommands
  "au!
  " When starting to edit a file:
  "   For *.c and *.h files set formatting of comments and set C-indenting on.
  "   For other files switch it off.
  "   Don't change the order, it's important that the line with * comes first.
  autocmd BufRead *       set formatoptions=tcql nocindent comments&
  autocmd BufRead *.c,*.h,*.cpp,*.hpp set formatoptions=croql cindent comments=sr:/*,mb:*,el:*/,://
  autocmd BufRead *.c,*.h,*.cpp,*.hpp so $VIM/vim61/plugin/doxygen.vim
 augroup END
endif
----- example extract from pete's ~/.vimrc --------

You could easily modify this to make a Java-specific zone in you
~/.vimrc :

----- example bit for russell's ~/.vimrc --------
 augroup javaprog
  autocmd BufRead *.java  set formatoptions=croql cindent shiftwidth=2 expandtab textwidth=95 comments=sr:/*,mb:*,el:*/,://
 augroup END
----- example bit for russell's ~/.vimrc --------

You can probably guess what this does - if reading in a file matching
*.java, set up a number of Java-specific options.

Note that most of the above was originally lifted from Bram
Moolenaar's example ~/.vimrc that was accessible from the old
http://www.vim.org/ site. It's not there anymore, unfortunately.

BTW, this is a good Vim option, just as a general tip:

:set laststatus=2

(well, I like it, and have it as the first thing in my ~/.vimrc :)

> smartindent looks smart enough for what I want.

Try

:help C-indenting

for a bit more detail on the differences between the indenting modes:

----------------------------------------------------------------------
  There are in fact four methods available for indentation:
  'autoindent'	uses the indent from the previous line.
  'smartindent'	is like 'autoindent' but also recognizes some C syntax to
  		increase/reduce the indent where appropriate.
  'cindent'	Works more cleverly than the other two and is configurable to
  		different indenting styles.
  'indentexpr'	The most flexible of all: Evaluates an expression to compute
  		the indent of a line.  When non-empty this method overrides
  		the other ones.  See |indent-expression|.
  The rest of this section describes the 'cindent' option.
----------------------------------------------------------------------

Note that you can :set lisp  for a specialised lisp/scheme-appropriate
indentation mode, as well. :)

For just about any language that uses { and } as code block markers
(ie. C, C++, Objective C, Java, Perl,...and quite a lot of others),
cindent is the most appropriate mode.

> Ta.

Pete.
-- 
http://akira.apana.org.au/~pete/
I never made a mistake in my life.  I thought I did once, but I was wrong.
		-- Lucy Van Pelt



More information about the plug mailing list