Tuesday, July 3, 2012

Re: vim abbreviate help...

On Tue, 3 Jul 2012, Roberto S. wrote:

> Hi sorry for this stupid question, but I'm learning vim.
> I would like to create a "pkg" abbreviation that create the:
> package org.hello.world;
> row in a file found in directory:
> <current_dir>/src/main/java/org/hello/world/
> I have added this to my .vimrc:
> autocmd BufRead,BufNew *.java iab pkg <esc>:r!echo "package "%:h:s?src/main/java/??:gs?/?.?";"<CR>
>
> The problem is: this abbreviation go on next line. I would like to
> write "package etc..." on the same line. I have tried also with
> register let @a, but I'm unable to obtain a final result. Can you
> help me?

I wouldn't use the shell command to do this. The following works:

autocmd BufRead,BufNew *.java iab pkg <C-R>='package '.substitute(substitute(expand('%:p:h'), '.\{-}src/main/java/', '', ''), '/', '.', 'g')<CR>

With that, you type 'pkg;' (not 'pkg<Enter>') and end up with 'package
com.example.foo;'.

The following is (possibly) a little easier to understand:

function! JavaPackage()
" get the full directory name
let dir = expand('%:p:h')
" strip everything prior to the root of your Java classes
let dir = substitute(dir, '.\{-}src/main/java/', '', '')
" change all slashes to dots
return substitute(dir, '/', '.', 'g')
endfunction
autocmd BufRead,BufNew *.java iab pkg <C-R>='package '.JavaPackage()<CR>


On Tue, 3 Jul 2012, John Little wrote:

>> The problem is: this abbreviation go on next line. I would like to write "package etc..." on the same line.
>
> Quick, barely tested reply: put a 0 in front of the r.
>
> If I
>
> :r!echo foo
>
> in a new, empty buffer, foo goes on line 2, but
>
> :0r!echo bar
>
> gets bar on line 1.

The 0 in your command is the {range} in :help :r's:

:{range}r[ead] !{cmd}

You're telling Vim to literally put it after the 0'th line (which is
before the 1st). Even if you're on line 341, it'd still insert it at
the beginning of the file. Possibly '-1' (after the prior line) would
make sense, but the way it was being used, it'd still end up as a
separate line from the current line being edited.

--
Best,
Ben

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment