zsh-users
 help / color / mirror / code / Atom feed
* Substituting grep (and other) output to open files in Vim
@ 2011-05-10 13:46 Richard Hartmann
  2011-05-10 14:12 ` Mikael Magnusson
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Richard Hartmann @ 2011-05-10 13:46 UTC (permalink / raw)
  To: zsh-users

Hi all,

I would like to be able to do the following, but I am stuck. Every
pair of lines is what I would execute and what it would be transferred
to. I am assuming that every file "name" (i.e. including colons etc) I
am running Vim on does not exist. If it exists, it should be opened
instead of magic happening. "foo" exists while "foo:" etc do not.

vim foo:
vim foo

vim foo:bar
vim foo

vim foo:123
vim foo +123

vim foo:123:
vim foo +123

vim foo:123:bar
vim foo +123

Ideally, the same would happen for vimdiff. And yes, vimdiff heeds
only one +n and the last one on the command line wins. That's fine.


I am pretty sure this is trivial to do in zsh, but as I said I am at a
loss as to how..


Thanks,
Richard


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 13:46 Substituting grep (and other) output to open files in Vim Richard Hartmann
@ 2011-05-10 14:12 ` Mikael Magnusson
  2011-05-10 14:14 ` Jérémie Roquet
  2011-05-10 15:26 ` Benjamin R. Haskell
  2 siblings, 0 replies; 13+ messages in thread
From: Mikael Magnusson @ 2011-05-10 14:12 UTC (permalink / raw)
  To: Richard Hartmann; +Cc: zsh-users

On 10 May 2011 15:46, Richard Hartmann <richih.mailinglist@gmail.com> wrote:
> Hi all,
>
> I would like to be able to do the following, but I am stuck. Every
> pair of lines is what I would execute and what it would be transferred
> to. I am assuming that every file "name" (i.e. including colons etc) I
> am running Vim on does not exist. If it exists, it should be opened
> instead of magic happening. "foo" exists while "foo:" etc do not.
>
> vim foo:
> vim foo
>
> vim foo:bar
> vim foo
>
> vim foo:123
> vim foo +123
>
> vim foo:123:
> vim foo +123
>
> vim foo:123:bar
> vim foo +123
>
> Ideally, the same would happen for vimdiff. And yes, vimdiff heeds
> only one +n and the last one on the command line wins. That's fine.
>
>
> I am pretty sure this is trivial to do in zsh, but as I said I am at a
> loss as to how..
>
>
> Thanks,
> Richard
>

This only does the foo:123: case, but you should be able to adapt it,
	if [[ $1 =~ ^(.*[^/]):([0-9]+):$ ]]; then
		1=$match[1]
		2=+$match[2]
	fi

I also have a non-regex version commented out, it is arguably less readable,
	if [[ $1 = *[^/]##:(#b)([0-9]##)(#B): ]]; then
		2=+${1[$mbegin[1],$mend[1]]}
		1=${1[1,$mbegin[1]-2]}
	fi

Ahead of this you'll want to check test -f $1 to avoid munging the
argument if the file exists.

Changing the regex to ^(.*[^/]):([0-9]+)[^0-9].*$ should cover two of
your other cases, but you might want to be more restrictive than .*
there, not sure what you want to allow bar to be.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 13:46 Substituting grep (and other) output to open files in Vim Richard Hartmann
  2011-05-10 14:12 ` Mikael Magnusson
@ 2011-05-10 14:14 ` Jérémie Roquet
  2011-05-10 18:16   ` Richard Hartmann
  2011-05-11  1:00   ` Wayne Davison
  2011-05-10 15:26 ` Benjamin R. Haskell
  2 siblings, 2 replies; 13+ messages in thread
From: Jérémie Roquet @ 2011-05-10 14:14 UTC (permalink / raw)
  To: zsh-users; +Cc: Richard Hartmann

Hi Richard,

2011/5/10 Richard Hartmann <richih.mailinglist@gmail.com>:
> I would like to be able to do the following, but I am stuck. Every
> pair of lines is what I would execute and what it would be transferred
> to. I am assuming that every file "name" (i.e. including colons etc) I
> am running Vim on does not exist. If it exists, it should be opened
> instead of magic happening. "foo" exists while "foo:" etc do not.
>
> vim foo:
> vim foo
>
> vim foo:bar
> vim foo
>
> vim foo:123
> vim foo +123
>
> vim foo:123:
> vim foo +123
>
> vim foo:123:bar
> vim foo +123
>
> Ideally, the same would happen for vimdiff. And yes, vimdiff heeds
> only one +n and the last one on the command line wins. That's fine.
>
>
> I am pretty sure this is trivial to do in zsh, but as I said I am at a
> loss as to how..

Something like

vim() {
  if test -r $1; then
    command vim $1
  else
    args=(${(s.:.)1})
    [[ $args[2] = <-> ]] && command vim $args[1] +$args[2] || command
vim $args[1]
  fi
}

?

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 13:46 Substituting grep (and other) output to open files in Vim Richard Hartmann
  2011-05-10 14:12 ` Mikael Magnusson
  2011-05-10 14:14 ` Jérémie Roquet
@ 2011-05-10 15:26 ` Benjamin R. Haskell
  2011-05-10 17:37   ` Mikael Magnusson
  2 siblings, 1 reply; 13+ messages in thread
From: Benjamin R. Haskell @ 2011-05-10 15:26 UTC (permalink / raw)
  To: Richard Hartmann; +Cc: zsh-users

On Tue, 10 May 2011, Richard Hartmann wrote:

> Hi all,
>
> I would like to be able to do the following, but I am stuck. Every 
> pair of lines is what I would execute and what it would be transferred 
> to. I am assuming that every file "name" (i.e. including colons etc) I 
> am running Vim on does not exist. If it exists, it should be opened 
> instead of magic happening. "foo" exists while "foo:" etc do not.
>
> vim foo:
> vim foo
>
> vim foo:bar
> vim foo
>
> vim foo:123
> vim foo +123
>
> vim foo:123:
> vim foo +123
>
> vim foo:123:bar
> vim foo +123
>
> Ideally, the same would happen for vimdiff. And yes, vimdiff heeds 
> only one +n and the last one on the command line wins. That's fine.
>
>
> I am pretty sure this is trivial to do in zsh, but as I said I am at a 
> loss as to how..

I'm at a loss as to "why". :-)

What's the actual problem you're trying to solve?  These don't seem like 
things you would want to type, so it looks like you're joining a bunch 
of parameters with colons only to want to separate them out to separate 
arguments later.  If you keep them in an array in the first place, you 
don't have this problem.  And maybe it'd be easier to get what you want.

e.g. (without handling the ':bar' portion since I don't know what it 
would be)

vimargs=( vim )
(( $+file )) && vimargs+=( $file )
(( $+line )) && vimargs+=( +$line )
$vimargs

-- 
Best,
Ben


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 15:26 ` Benjamin R. Haskell
@ 2011-05-10 17:37   ` Mikael Magnusson
  2011-05-10 18:16     ` Richard Hartmann
  0 siblings, 1 reply; 13+ messages in thread
From: Mikael Magnusson @ 2011-05-10 17:37 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Richard Hartmann, zsh-users

On 10 May 2011 17:26, Benjamin R. Haskell <zsh@benizi.com> wrote:
>
> I'm at a loss as to "why". :-)
>
> What's the actual problem you're trying to solve?  These don't seem like
> things you would want to type,

At least in my case, this is compiler errors/warnings output, and
similar. It's nice to just cut and paste the thing without munging the
:123: to +123 manually.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 17:37   ` Mikael Magnusson
@ 2011-05-10 18:16     ` Richard Hartmann
  2011-05-10 18:58       ` Benjamin R. Haskell
  0 siblings, 1 reply; 13+ messages in thread
From: Richard Hartmann @ 2011-05-10 18:16 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Benjamin R. Haskell, zsh-users

On Tue, May 10, 2011 at 19:37, Mikael Magnusson <mikachu@gmail.com> wrote:

> At least in my case, this is compiler errors/warnings output, and
> similar. It's nice to just cut and paste the thing without munging the
> :123: to +123 manually.

Exactly. And half a dozen other use cases where it's faster to simply
copy & paste.


Richard


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 14:14 ` Jérémie Roquet
@ 2011-05-10 18:16   ` Richard Hartmann
  2011-05-11  1:00   ` Wayne Davison
  1 sibling, 0 replies; 13+ messages in thread
From: Richard Hartmann @ 2011-05-10 18:16 UTC (permalink / raw)
  To: Jérémie Roquet; +Cc: zsh-users

2011/5/10 Jérémie Roquet <arkanosis@gmail.com>:

> vim() {
>  if test -r $1; then
>    command vim $1
>  else
>    args=(${(s.:.)1})
>    [[ $args[2] = <-> ]] && command vim $args[1] +$args[2] || command
> vim $args[1]
>  fi
> }

Perfect, thanks.


Richard


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 18:16     ` Richard Hartmann
@ 2011-05-10 18:58       ` Benjamin R. Haskell
  2011-05-10 22:27         ` Richard Hartmann
  0 siblings, 1 reply; 13+ messages in thread
From: Benjamin R. Haskell @ 2011-05-10 18:58 UTC (permalink / raw)
  To: Richard Hartmann; +Cc: Mikael Magnusson, zsh-users

On Tue, 10 May 2011, Richard Hartmann wrote:

> On Tue, May 10, 2011 at 19:37, Mikael Magnusson wrote:
>
>> At least in my case, this is compiler errors/warnings output, and 
>> similar. It's nice to just cut and paste the thing without munging 
>> the :123: to +123 manually.
>
> Exactly. And half a dozen other use cases where it's faster to simply 
> copy & paste.

Okay.  The reason I asked, and it's validated by the responses, is that 
maybe you can handle this functionality at a different layer than Zsh.

E.g. through your terminal emulator.  rxvt-unicode, for example, allows 
you to write Perl extensions that can do things with the selection.

So, instead of:

1. highlight text w/ mouse
2. type 'vim ' in the shell
3. paste selection (presumably middle-click pasting)
4. hit Enter (Zsh munges pasted selection to what you want)

You could:

1. highlight text w/ mouse
2. Ctrl+right click
3. pick the "launch in vim" option (provided by a hypothetical Perl 
extension)

Generally, the things I've written like this are to pop things up in a 
browser, but popping things up in vim in a terminal wouldn't be too much 
harder.

Or, you could run `make` from within Vim itself which already handles a 
lot of these type of problems.  (That one really depends on the workflow 
and/or source.)

Not really trying to dissuade you from the pretty-straightforward Zsh 
solution.  Just wanted to point out some possibilities (and double-check 
that it wasn't as simple as the array-vs.-string thing).

-- 
Best,
Ben


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 18:58       ` Benjamin R. Haskell
@ 2011-05-10 22:27         ` Richard Hartmann
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Hartmann @ 2011-05-10 22:27 UTC (permalink / raw)
  To: Benjamin R. Haskell; +Cc: Mikael Magnusson, zsh-users

On Tue, May 10, 2011 at 20:58, Benjamin R. Haskell <zsh@benizi.com> wrote:

> Okay.  The reason I asked, and it's validated by the responses, is that
> maybe you can handle this functionality at a different layer than Zsh.

On any given day, I am using Yakuake and Terminator. Every other day,
Konsole as well. At least once a week ttys & gpm. Doing this outside
of zsh is not an option ;)


> 1. highlight text w/ mouse
> 2. Ctrl+right click
> 3. pick the "launch in vim" option (provided by a hypothetical Perl
> extension)

If that works for you, great! Personally, I don't like to touch the
rodent more than necessary and right-clicking and selecting an option
takes longer than typing a few commands.


> Or, you could run `make` from within Vim itself which already handles a lot
> of these type of problems.  (That one really depends on the workflow and/or
> source.)

Does not work for grep etc.


> Not really trying to dissuade you from the pretty-straightforward Zsh
> solution.  Just wanted to point out some possibilities (and double-check
> that it wasn't as simple as the array-vs.-string thing).

Aye; thanks :)


Richard


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-10 14:14 ` Jérémie Roquet
  2011-05-10 18:16   ` Richard Hartmann
@ 2011-05-11  1:00   ` Wayne Davison
  2011-05-27 15:09     ` Richard Hartmann
  1 sibling, 1 reply; 13+ messages in thread
From: Wayne Davison @ 2011-05-11  1:00 UTC (permalink / raw)
  To: Jérémie Roquet; +Cc: zsh-users, Richard Hartmann

2011/5/10 Jérémie Roquet <arkanosis@gmail.com>
> Something like
> vim() {
>  if test -r $1; then
>    command vim $1
>  else
>    args=(${(s.:.)1})
>    [[ $args[2] = <-> ]] && command vim $args[1] +$args[2] || command
> vim $args[1]
>  fi
> }

Nice.  Let's improve that so that it is possible to specify multiple
files and/or options and files (though we don't give anything but a
single arg special treatment) and also to avoid a "parameter not set"
error for folks that like to run interactively with "setopt no_unset":

vim() {
    if test $# != 1 -o -r $1; then
        command vim "${@}"
    else
        local args
        args=(${(s.:.)1})
        [[ $#args == 2 && $args[2] == <-> ]] \
            && command vim $args[1] +$args[2] \
            || command vim $args[1]
    fi
}

..wayne..


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-11  1:00   ` Wayne Davison
@ 2011-05-27 15:09     ` Richard Hartmann
  2011-05-27 22:00       ` Wayne Davison
  0 siblings, 1 reply; 13+ messages in thread
From: Richard Hartmann @ 2011-05-27 15:09 UTC (permalink / raw)
  To: Wayne Davison; +Cc: Jérémie Roquet, zsh-users

2011/5/11 Wayne Davison <wayned@users.sourceforge.net>:

> Nice.  Let's improve that so that it is possible to specify multiple
> files and/or options and files (though we don't give anything but a
> single arg special treatment) and also to avoid a "parameter not set"
> error for folks that like to run interactively with "setopt no_unset":

FWIW, I have been using this all the time now. I love it :)
I just noticed one regression when compared to Jérémie's version:

  vim foo:123:abc

does not act like

  vim foo +123


Thanks to everyone for the help,
Richard


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-27 15:09     ` Richard Hartmann
@ 2011-05-27 22:00       ` Wayne Davison
  2011-05-28 11:51         ` Richard Hartmann
  0 siblings, 1 reply; 13+ messages in thread
From: Wayne Davison @ 2011-05-27 22:00 UTC (permalink / raw)
  To: Richard Hartmann; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 277 bytes --]

On Fri, May 27, 2011 at 8:09 AM, Richard Hartmann <
richih.mailinglist@gmail.com> wrote:
>
> vim foo:123:abc    does not act like    vim foo +123
>

Twiddle the "== 2" line to use "-ge 2":

        [[ $#args -ge 2 && $args[2] == <-> ]] \

That'll fix that right up.

..wayne..

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Substituting grep (and other) output to open files in Vim
  2011-05-27 22:00       ` Wayne Davison
@ 2011-05-28 11:51         ` Richard Hartmann
  0 siblings, 0 replies; 13+ messages in thread
From: Richard Hartmann @ 2011-05-28 11:51 UTC (permalink / raw)
  To: Wayne Davison; +Cc: zsh-users

On Sat, May 28, 2011 at 00:00, Wayne Davison
<wayned@users.sourceforge.net> wrote:

> That'll fix that right up.

Aye. Thanks :)


Richard


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2011-05-28 11:51 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-10 13:46 Substituting grep (and other) output to open files in Vim Richard Hartmann
2011-05-10 14:12 ` Mikael Magnusson
2011-05-10 14:14 ` Jérémie Roquet
2011-05-10 18:16   ` Richard Hartmann
2011-05-11  1:00   ` Wayne Davison
2011-05-27 15:09     ` Richard Hartmann
2011-05-27 22:00       ` Wayne Davison
2011-05-28 11:51         ` Richard Hartmann
2011-05-10 15:26 ` Benjamin R. Haskell
2011-05-10 17:37   ` Mikael Magnusson
2011-05-10 18:16     ` Richard Hartmann
2011-05-10 18:58       ` Benjamin R. Haskell
2011-05-10 22:27         ` Richard Hartmann

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).