# Vim Question

**URL:** <https://www.ruby-forum.com/t/vim-question/65734>\
**Category:** Ruby\
**Created:** [August 21, 2006, 12:37am UTC](https://www.ruby-forum.com/t/vim-question/65734 "2006-08-21T00:37:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 12:37am UTC](https://www.ruby-forum.com/t/vim-question/65734/1 "2006-08-21T00:37:44Z")

</div>

This is for all the Rubyists out there who can really rock the Vim.  
How do you invoke a shell command and pass arguments to said command?

function Put(computer)  
!rsync -v -rulptzCF string(getcwd()) . “/” string(a:computer) . “:”  
. string(getcwd()) . “/”  
endfunction

The idea here is that I want a Put function that takes a computer name  
as an argument. This function will use rsync to copy all modified  
files from the current working directory to the same directory on the  
remote computer.

The reason for this is that I’m developing on Windows ☹ but my code  
needs to run on Linux and Solaris. Pushing code to the machines where  
autotest will run it is the desired outcome. I don’t want to have to  
drop out of vim and into a shell prompt each time I do this.

The function I gave above is total crap, but hopefully it conveys the  
idea I want to accomplish. Any help or thoughts? A simple example,  
maybe?

TwP

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 12:51am UTC](https://www.ruby-forum.com/t/vim-question/65734/2 "2006-08-21T00:51:45Z")

</div>

On Mon, 21 Aug 2006, Tim P. wrote:

> files from the current working directory to the same directory on the
> 
> TwP

here i type

:r !echo arg1 arg2

which says

read in the output of ‘echo arg1 arg2’

yielding

arg1 arg2

into my file. if you don’t want output you can do things like

:! cp % %.bak

which makes a backup of the current file

lastly, you can write nifty ‘filters’ that read stdin. my favourite is  
this  
one

harp:~ \> cat ~/bin/columnize

#!/usr/bin/env ruby  
n = Integer((ARGV.shift || 1))  
while((line = STDIN.gets))  
line.strip!  
fields = line.split(%r/\s+/)

```
 rows = []
 while not fields.empty?
   rows << (row = [])
   n.times do
     field = fields.shift
     row << field if field
   end
 end
 rows.each{|row| puts row.join(' ')}

```

end

now, given a bunch of words like these

foo bar foobar barfoo a b c

i can highlight them using ‘shift-v movement keys (hjkl)’, which  
hightlights  
by line. then, while highlighted, i type ‘:!columnize’ which turns the  
words  
into this

foo  
bar  
foobar  
barfoo  
a  
b  
c

if, instead, i type, ‘:!columnize 2’, the output is

foo bar  
foobar barfoo  
a b  
c

hope that gives you some ideas. btw. there is probably a way to do all  
that  
in vim, writing little ruby scripts let’s me use them from the shell  
too, as  
in

cat list | columnize 3

and it’s easy to do anyhow.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 2:03am UTC](https://www.ruby-forum.com/t/vim-question/65734/3 "2006-08-21T02:03:49Z")

</div>

On Aug 20, 2006, at 5:50 PM, removed\_email\_address@domain.invalid wrote:

> here i type
> 
> :r !echo arg1 arg2
> 
> which says
> 
> read in the output of ‘echo arg1 arg2’

You can also just type !! with no colon to do the same thing (two  
less characters!).

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 11:52am UTC](https://www.ruby-forum.com/t/vim-question/65734/4 "2006-08-21T11:52:48Z")

</div>

Tim,

> This is for all the Rubyists out there who can really rock the Vim.

Well, one tries.

> The idea here is that I want a Put function that takes a computer name  
> as an argument. This function will use rsync to copy all modified  
> files from the current working directory to the same directory on the  
> remote computer.
> 
> The reason for this is that I’m developing on Windows ☹ but my code  
> needs to run on Linux and Solaris. Pushing code to the machines where  
> autotest will run it is the desired outcome. I don’t want to have to  
> drop out of vim and into a shell prompt each time I do this.

Why not just edit the files on the machines where autotest runs?

Vim has a mechanism for editing remote files via ftp, rcp, scp, http  
and others. I’ve used it before and it saved a lot of fiddling  
around with scripts outside vim. Here’s some more information:  
[http://www.vim.org/tips/tip.php?tip\_id=337](http://www.vim.org/tips/tip.php?tip_id=337)

Good luck,  
Andy

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/vim-question/65734/5 "2006-08-21T16:49:35Z")

</div>

On Aug 21, 2006, at 10:27 AM, Tim P. wrote:

> > Andy
> 
> That’s a cool little fact to know, but it won’t work for me ☹
> 
> The version controlled copies live on my laptop, but the code needs to  
> run on a few other machines. Also, when I’m not connected to a  
> network, it would be hard to edit the files. Thanks for the tip though  
> 🙂

Sounds like a job for DSCM such as darcs, git or mercurial. 😉

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/vim-question/65734/6 "2006-08-21T16:29:32Z")

</div>

On 8/21/06, Andy S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why not just edit the files on the machines where autotest runs?
> 
> Vim has a mechanism for editing remote files via ftp, rcp, scp, http  
> and others. I’ve used it before and it saved a lot of fiddling  
> around with scripts outside vim. Here’s some more information:  
> [tips : vim online](http://www.vim.org/tips/tip.php?tip_id=337)
> 
> Good luck,  
> Andy

That’s a cool little fact to know, but it won’t work for me ☹

The version controlled copies live on my laptop, but the code needs to  
run on a few other machines. Also, when I’m not connected to a  
network, it would be hard to edit the files. Thanks for the tip though  
🙂

TwP

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [August 21, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/vim-question/65734/7 "2006-08-21T17:23:40Z")

</div>

On 8/20/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arg1 arg2
> 
> into my file. if you don’t want output you can do things like

Okay, the solution I came up with is to build up the command inside  
one of the paste registers (@) and then execute that register. Here  
is a simple example that will move to the current working directory  
and do a file listing.

function FuncGetls( )  
let @g="!cd " . getcwd() . “; ls”  
@g  
endfunction  
command Getls call FuncGetls( )

My full solution for doing the rsync is this …

command -nargs=1 Put call Put(“”)  
function Put(puter)  
let s:dir=escape(getcwd(), “"”) . “/”  
let @r="!rsync -v -rulptzCF " . s:dir . " " . a:puter . “:” . s:dir  
@r  
endfunction

Now I can send all the files I’ve modified to another computer by  
typing the following command …

:Put panic

where panic is the name of the machine.

TwP
