Automatic code generation in vim


#1

Hi list.

This is an editor question for Ruby. I’ve seen the demo of RoR
where textmate is widely used. Among the others, the most
interesting feature was automatic code generation like

forin

=>

for in

end

I’ve searched for similar functionality in vim community, but I couldn’t
find. There was scriptEmu(
http://www.vim.org/scripts/script.php?script_id=1318),
but it does not work with my vim7.0 installation.

Does anybody have a nice solution for automatic code generation
for Ruby in VIM?

Sincerely,
Minkoo S.


#2

This is an editor question for Ruby. I’ve seen the demo of RoR
where textmate is widely used. Among the others, the most
interesting feature was automatic code generation like

forin

=>

for in

Not sure if pre-existing stuff exists for this (but I think so) but you
can use vim mappings for this.

http://www.vim.org/htmldoc/map.html

-philip


#3

On Sat, May 27, 2006 at 03:56:25AM +0900, Minkoo S. wrote:

Hi list.

This is an editor question for Ruby. I’ve seen the demo of RoR
where textmate is widely used. Among the others, the most
interesting feature was automatic code generation like
[…]
I’ve searched for similar functionality in vim community, but I couldn’t
find. There was scriptEmu(
http://www.vim.org/scripts/script.php?script_id=1318),
but it does not work with my vim7.0 installation.

Does anybody have a nice solution for automatic code generation
for Ruby in VIM?

http://blog.rosejn.net/articles/2005/12/09/snippets-0-01
http://blog.rosejn.net/articles/2006/02/28/snippetmagic-0-02

I read it’s closer to TextMate than snippetsEmu.


#4

On May 26, 2006, at 3:53 PM, James B. wrote:

Incidentally, I tried this put and changed it to
function! s:ifFILE()
return “if FILE == $0 <Esc>o <Esc>oend<Esc>ki”
endfunction

which sticks the cursor where you would expect it to be.


#5

Minkoo S. wrote:

Does anybody have a nice solution for automatic code generation
for Ruby in VIM?

Get the vim stuff for Ruby, and look in ruby-macros.vim

I used that to learn how to write my own simple autotext stuff.

For example:

If I enter ifFILE, then vim should call the ifFILE function

iab ifFILE =ifFILE()

which is

function! s:ifFILE()
return “if FILE == $0 <Esc>o <Esc>oend”
endfunction


James B.

“The use of anthropomorphic terminology when dealing with
computing systems is a symptom of professional immaturity.”

  • Edsger W. Dijkstra

#6

Logan C. wrote:

Incidentally, I tried this put and changed it to
function! s:ifFILE()
return “if FILE == $0 <Esc>o <Esc>oend<Esc>ki”
endfunction

which sticks the cursor where you would expect it to be.

Oh, nice. Thanks for the pointer.

James


#7

Does anybody have a nice solution for automatic code generation
for Ruby in VIM?

you may have a look at the file plugin/imaps.vim from the latex-suite
project. Taken from the header of that file:

Motivation:

this script provides a way to generate insert mode mappings which do not
suffer from some of the problem of mappings and abbreviations while
allowing cursor placement after the expansion. It can alternatively be
thought of as a template expander.
<<<

The script supports place holder and more. I did not try yet, but I am
shure this script might proove quite usefull for ruby editing as well.

HTH,

Steph.


#8

Mauricio F. wrote:

Does anybody have a nice solution for automatic code generation
for Ruby in VIM?

http://blog.rosejn.net/articles/2005/12/09/snippets-0-01
http://blog.rosejn.net/articles/2006/02/28/snippetmagic-0-02

I read it’s closer to TextMate than snippetsEmu.

I haven’t been able to work on it for a while, but snippet magic is for
this exact purpose, in vim. Actually, the TextMate snippet definitions
are all open source and available in a public repository so I converted
them to YAML and now use them as the basis for snippet magic’s default
definitions. Give it a try and see what you think. Lots of people have
sent mail or posted that they are using it, but it does still have some
issues that need to be fixed. (A better binding for ruby-vim would help
a lot here…) I’ll try to get back to it soon, but if anyone wants to
help out that would be great.

-Jeff


#9

On a side note, has anyone got code generation (e.g. adding “end”)
working with Vim in the console on Linux? It only work with GVim for me
– the console version doesn’t recognize the shift key or something…
Anyone?

Daniel


#10

AFAIK, it’s impossible to map ‘shift’ key under console mode of *NIX.
So, try to map it with another key binding like <c+_>.

Sincerely,
Minkoo S.


#11

I’ve tried snippet magic, but I can’t figure out how to use. As an
example,
when I type ‘forin’ it expands like

for element in collection
element.
end

and my cursor is at ‘t’ of ‘elementt’. And the status of vim is
command-mode.
(The mode after key is pressed)

Now, what I have to do? Because my cursor is at ‘t’ of ‘element’, I
can’t
change
the name of element using ‘cw’. I just stuck.

Sincerely,
Minkoo S.


#12

Minkoo S. wrote:

Now, what I have to do? Because my cursor is at ‘t’ of ‘element’, I can’t
change
the name of element using ‘cw’. I just stuck.
caw should do what you want, I think? Surely it’s collection that you
want to change the name of, though? I mean, in a for loop, the inner
loop variable name is less likely to be important than the collection
name?


#13

Minkoo S. wrote:

Now, what I have to do? Because my cursor is at ‘t’ of ‘element’, I can’t
change
the name of element using ‘cw’. I just stuck.

Hmmm. This has been one of the most frustrating things about this
thing. For some it works just fine, and others it falls on its face. I
don’t know if it has to do with vim versions, preset mappings or what.
One problem is that since the ruby bindings are so minimal many commands
on the buffer have to be done by passing vim-script through ruby. I
think this makes it susceptible to mappings and a mess of other junk, as
opposed to a true binding that would have ruby methods directly
operating on the buffer. Anyway…

What should happen in this case is that ‘element’ should be highlighted
in select mode. (Highlight something in visual mode and type -g)
When you type it will change the word to whatever variable name you
want, and when you hit tab it will automatically update the mirrors of
that element, which in this case means the variable in the body. The
idea is that you tab through the snippet, editing the important bits and
tab jumping to where you want to be next.

Once the term is over at the end of this month I’ll get more serious
about this again.

-Jeff