Question

I am trying to set up a vim key mapping that will map the key ';' to A; - i.e. I want to auto append ';' to the end of the line. However I am having difficulty in setting this mapping up. I would also like to limit this to only java files if possible. Can this be done?

Thanks

Was it helpful?

Solution

The mapping itself is done this way:

:map ; A;<esc>

I would recommend putting this line in your .vimrc and live with it (it should not bother you, since the mapping only works in command mode). If you really must restrict this behaviour to certain files, you need to look into the autocmd function (:help autocmd)

OTHER TIPS

If you want to restrict this feature to java buffers, have a look at ftplugins. The mapping then becomes:

nnoremap <buffer> ; A;<esc>

BTW, I would advise against mapping on ';' as it's a very useful command that may be used in other badly defined mappings (too many vimmers are using :*map instead of :*noremap).

Use ftplugins as Luc Hermitte said or add the following to your .vimrc

autocmd filetype java :nnoremap <buffer> ; A;<esc>

Put this in ~/.vim/after/ftplugin/java.vim

nnoremap <buffer> ; A;<Esc>

Now this mapping should be local to java buffers only

As a quick implementation, what about:

map ; $a;cntl-vESC

then hit return

I'll have a think about enabling this for Java files only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top