Shortcut for System.out.println - ViM Java

1,085 views
Skip to first unread message

Rashmi

unread,
Jul 30, 2009, 12:41:40 PM7/30/09
to v...@vim.org
Hello all,

I'm using ViM version 7.2 to edit Java files.

In Java I have to frequently type this code: System.out.println("
print something ");

Is there a way to create a shortcut so that , the entire line of code
System.out.println(""); would be typed with just a few keystrokes?

For example, something like sout <tab> , would expand to
System.out.println("");

I also came across a plugin for ViM called VJDE, I'm trying to find
out if this plugin may have this feature to print
System.out.println(""); with a few keystrokes.

I appreciate any help.

-Rashmi

Raúl Núñez de Arenas Coronado

unread,
Jul 30, 2009, 12:50:03 PM7/30/09
to vim...@googlegroups.com
Saluton Rashmi :)

Rashmi <r...@gmail.com> dixit:


> Is there a way to create a shortcut so that , the entire line of code
> System.out.println(""); would be typed with just a few keystrokes?
>
> For example, something like sout <tab> , would expand to
> System.out.println("");

I would use an iabbr:

:iabbr sout System.out.println("");

(remove the ":" at the beginning of the line if you put this command
into your .vimrc)

--
Raúl "DervishD" Núñez de Arenas Coronado
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!

KKde

unread,
Jul 31, 2009, 2:38:34 AM7/31/09
to vim_use
This can be achieved by using abbreviations in VIM. This is an inbuilt
feautre and no need of any external VIM script.
see :help abbreviations for more info

Thanks,
Sarath

Raúl Núñez de Arenas Coronado

unread,
Jul 31, 2009, 3:09:47 AM7/31/09
to vim...@googlegroups.com
Sarath (KKde), please bottom post on this list. Quote a small (relevant)
part of the message you are replying to, and put your text underneath.

See the list guidelines:
http://groups.google.com/group/vim_use/web/vim-information

Rashmi

unread,
Jul 31, 2009, 10:36:35 AM7/31/09
to vim...@googlegroups.com
Thank you both for your replies.

I used imap as suggested previously, it works well because it also
allows positioning the cursor to between the double quotes.

Also , if we add this
:imap sout<Press Tab> System.out.println("");<Esc>F"i

to a new file created under
C:\Program Files\Vim\vimfiles\ftplugin\java.vim

the mapping gets set each time ViM is started, so I don't have to set
the mapping each time.
I got this tip from: http://vim.wikia.com/wiki/VimTip1510 (Keep your
vimrc file clean)

I made a newbie mistake by sending this message twice
once to vim...@googlegroups.com and once to v...@vim.org thinking it
might not have reached, so sorry about that.

-Rashmi


2009/7/30 Raúl Núñez de Arenas Coronado <rau...@gmail.com>:

Ben Fritz

unread,
Jul 31, 2009, 11:26:49 AM7/31/09
to vim_use


On Jul 31, 9:36 am, Rashmi <rashmi....@gmail.com> wrote:
> Thank you both for your replies.
>
> I used imap as suggested previously, it works well because it also
> allows positioning the cursor to between the double quotes.
>

Actually, iabbr also allows you to position the cursor:

iabbr sout System.out.println("");<Left><Left><Left>

However, triggering this iabbr with, for example, sout<Tab> will place
a tab inside the "" which I don't think you want. Your mapping may
therefore be better for your purposes.

> Also , if we add this
> :imap sout<Press Tab>      System.out.println("");<Esc>F"i
>

This exits insert mode (albeit briefly) which could have some
unintended side effects. Also, you don't need to insert a literal tab
character, you can use <Tab> (exactly like that, as text with '<' and
'>' characters) in your mapping. This should work in exactly the same
way without the side effects:

imap sout<Tab> System.out.println("");<Left><Left><Left>

> to a new file created under
> C:\Program Files\Vim\vimfiles\ftplugin\java.vim
>
> the mapping gets set each time ViM is started, so I don't have to set
> the mapping each time.

Actually, it gets set each time you edit java code in Vim. Not if you
use Vim to edit a .c file, or a .txt file. Probably a good idea to do
it this way, then.

> I got this tip from:http://vim.wikia.com/wiki/VimTip1510(Keep your
> vimrc file clean)
>

It's great to see people getting useful information from the wiki!
Keep visiting, I hope you learn even more.

Raúl Núñez de Arenas Coronado

unread,
Jul 31, 2009, 1:52:19 PM7/31/09
to vim...@googlegroups.com
Saluton Rashmi :)

Rashmi <r...@gmail.com> dixit:


> I used imap as suggested previously, it works well because it also
> allows positioning the cursor to between the double quotes.

You can do that with iabbr, too, but since the iabbr will be triggered
mostly using <Space> (I mean, "sout<Space>") you will end up with an
space between the "", which may or may not be what you want. The imap is
better in that sense.

> to a new file created under C:\Program
> Files\Vim\vimfiles\ftplugin\java.vim
>
> the mapping gets set each time ViM is started, so I don't have to set
> the mapping each time. I got this tip from:
> http://vim.wikia.com/wiki/VimTip1510 (Keep your vimrc file clean)

I don't know about Windows, but in UNIX you can put that in
".vim/after/ftplugin/java.vim", just in case you need to.

> I made a newbie mistake by sending this message twice once to
> vim...@googlegroups.com and once to v...@vim.org thinking it might not
> have reached, so sorry about that.

Don't worry about that, it's a normal mistake. On the other hand,
please bottom post on this list, quote a small (relevant) part of the


message you are replying to, and put your text underneath.

Thanks :)

scott

unread,
Jul 31, 2009, 2:21:15 PM7/31/09
to vim...@googlegroups.com
On Friday 31 July 2009 12:52:19 Raúl Núñez de Arenas Coronado
wrote:

> You can do that with iabbr, too, but since the iabbr will be
> triggered mostly using <Space> (I mean, "sout<Space>") you
> will end up with an space between the "", which may or may not
> be what you want. The imap is better in that sense.

unless you add the Eatchar function, as documented in

:help iabbreviation

by adding

function! Eatchar(pat)
let c = nr2char(getchar(0))
return (c =~ a:pat) ? '' : c
endfunction

to your .vimrc, and writing your iabbr like this:

iabbrev ibt #!/usr/bin/tclsh<c-r>=Eatchar('\s')<cr>

then when you hit the space the string is inserted but not with a
trailing space, but if you trigger it with <Enter> a carriage
return is inserted

sc

Raúl Núñez de Arenas Coronado

unread,
Jul 31, 2009, 2:50:43 PM7/31/09
to vim...@googlegroups.com
Saluton scott :)

scott <t...@swbell.net> dixit:


> On Friday 31 July 2009 12:52:19 Raúl Núñez de Arenas Coronado
> wrote:
>> You can do that with iabbr, too, but since the iabbr will be
>> triggered mostly using <Space> (I mean, "sout<Space>") you
>> will end up with an space between the "", which may or may not
>> be what you want. The imap is better in that sense.
>
> unless you add the Eatchar function, as documented in
>
>        :help iabbreviation
>
> by adding
>
> function! Eatchar(pat)
>    let c = nr2char(getchar(0))
>    return (c =~ a:pat) ? '' : c
> endfunction

Yes! Thanks a lot for the advice, this is something I've always missed
in the help for iabbr O:) Very useful, since most of the time I prefer
iabbr over imap for certain things.

Reply all
Reply to author
Forward
0 new messages