[CentOS] Feed a list of filenames to vim

Tue May 17 16:37:33 UTC 2011
David Goldsmith <dgoldsmith at sans.org>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 5/17/2011 12:19 PM, Jussi Hirvi wrote:
> There are some googlable ways to feed a list of filenames to vim, but I 
> stumble on weird results.
> 
> With my filelist, I try to do
> 
> 	cat list | xargs vim
> 
> ...to edit the files listed in the file "list". Here's what happens:
> 
> [root at lasso2 tempdir]# ls -l
> total 8
> -rw-r--r--  1 root root  0 May 17 18:28 a
> -rw-r--r--  1 root root  0 May 17 18:28 b
> -rw-r--r--  1 root root  3 May 17 18:31 c
> -rw-r--r--  1 root root 12 May 17 18:43 list
> [root at lasso2 tempdir]# cat list
> ./a
> ./b
> ./c
> [root at lasso2 tempdir]# cat list | xargs vim
> 3 files to edit
> Vim: Warning: Input is not from a terminal
> 
> Ok, so far, so good. And after this, the file a opens, as expected. 
> However, the contents show as all uppercase. And everything I write is 
> uppercase too. I can move to the next file (:n) even though the command 
> shows as uppercase (:N). I cannot quit vim, however. When I do ":q", I 
> get blank screen, and I have to close the terminal window.
> 
> If I do instead
> 	cat list | xargs less
> ...it works as expected.
> 
> And with
> 	cat list | xargs vi
> ...(in a fresh terminal window), the editing goes just perfect, but when 
> I quit vi, the terminal will not show the commands I write, and the 
> display gets garbled (no newlines etc.).
> 
> What is happening?

Do this instead:

vi `cat list`

cat list   - gives the output of the file which is the three filenames
`cat list` - executes this command and feeds its output to the input of
             your next command

So the resulting command ends up being "vi ./a ./b ./c" which opens up
the 'a' file and you will be able to move to the next file with the :n
option.

xargs is effectively running a for loop on each unique item in the
output of the previous command (cat list).  vi expects to be run on one
file at a time and needs to be associated with a terminal session in
prder to be able to get input from you (either text or commands) to
apply to the file.

- -- 
David Goldsmith
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.14 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3SpE0ACgkQ417vU8/9QflaoQCdH0YmjkeVG4QypCWRZFPpDBD4
N0QAn3dCourgI97OpthGJFa7FTWS/f5t
=lkQA
-----END PGP SIGNATURE-----