[dev] Re: a little bit of vi+zsh magic

From: Sven Guckes <maillist-wmi-announce_AT_guckes.net>
Date: Wed, 4 Nov 2009 11:00:01 +0100

* Kris Maglione <maglione.k_AT_gmail.com> [2009-11-04 08:03]:
> On Sun, Nov 01, 2009 at 02:01:06PM -0500, John Yates wrote:
>> What made this mechanism so pleasant to use was that all
>> text areas in the screen (editing buffers, input panes,
>> and output transcripts) shared a common set of editing key
>> bindings, similar to vi or emacs. A very common idiom was
>> to list a directory, switch the input pane to disconnected
>> mode, copy the directory listing to the input pane, modify
>> that copy of the listing using some regular expression
>> substitutions to turn it into one or more commands on
>> each file, and revert the input pane to connected mode.
>>
>> Obviously in any *nix environment one can do the same thing
>> by redirecting the output from ls to a file, open that file
>> in an editor, modifying it, saving it, and finally sourcing
>> the edited file in one's shell. The input pane mechanism
>> simply made such operations faster and more intuitive:
>> no inventing a file name, no opening a separate editor,
>> no issuing a source command to one's shell.
>
> This actually isn't especially difficult to do
> in vim or some other editor. In vim alone,
> for instance, you can do something like,
>
> :r !ls
> :v/\.js/d|s/^/rm /
> :w !sh

i have used these command so often that i keep wondering
how else you can survive the day without many workarounds. ;-)

for those who havent used vi much,
here is a breakdown of the commands:

      :r !ls
      reads (:r) into buffer via a shell (!)
      the output of a list (ls) command.

      :v/\.js/d|s/^/rm /
      :v for all non-matching lines
            applied to contents ".js"
      d delete
      | command separator
      s substitute
      ^ the beginning of the line
      rm with "rm " (inserting it)

      :w write
      !sh to a shell calling a shell

the last one is a little redundant,
but, hey, it gets the job done.

there you should have your list of "rm" commands
applied to all non-js files, execute by a shell.

mind you, the commands all apply to basic vi -
so *all* vi clones support this. nothing fancy.

the zsh people would do the removal
using patterns with *exeptions8
which requires "extended globbing":

      % setopt extendedglob
      % rm *(.)~*.js

the pattern works like this:
      *(.) all files
      ~ except
      *.js files ending in ".js"

see also:
   % man zshexpn
   /FILENAME GENERATION
   /Glob Operators

   x~y
   (Requires EXTENDED_GLOB to be set.)
   Match anything that matches the
   pattern x but does not match y. ..
   Multiple patterns can be excluded by ‘foo~bar~baz’.

i am pretty sure some of you would
ask for "multiple exclusions". ;-)

> Most shells let you edit your commandline with an editor,
> too. For instance, FreeBSD's sh(1) allows this with <Esc>v
> by default, in vi mode. There's an edit-command-line
> script for zsh, and I assume something similar for bash.

within zsh, you can edit the last command using the "fc" builtin.
and you can use your favourite editor by setting FCEDIT:

    % FCEDIT=vim
    % fc

the fc command is quite powerful. read all about it
with "man zshbuiltins" and "/SHELL BUILTIN COMMANDS"
(where "/foo" is the search command which not only exits
 in vi but also within "less" and "more" as pagers, too).

Sven

-- 
www.vim.org
www.zsh.org
www.zshbuch.org
Received on Wed Nov 04 2009 - 10:00:01 UTC

This archive was generated by hypermail 2.2.0 : Wed Nov 04 2009 - 10:24:08 UTC