When invoking vim through find | xargs, like this:

find . -name "*.txt" | xargs vim

you get a warning about

Input is not from a terminal

and a terminal with pretty much broken behaviour afterwards. Why is that?

up vote 86 down vote accepted

When you invoke a program via xargs, the program's stdin (standard input) points to /dev/null. (Since xargs doesn't know the original stdin, it does the next best thing.)

$ true | xargs filan -s
    0 chrdev /dev/null
    1 tty /dev/pts/1
    2 tty /dev/pts/1

$ true | xargs ls -l /dev/fd/

Vim expects its stdin to be the same as its controlling terminal, and performs various terminal-related ioctl's on stdin directly. When done on /dev/null (or any non-tty file descriptor), those ioctls are meaningless and return ENOTTY, which gets silently ignored.

  • My guess at a more specific cause: On startup Vim reads and remembers the old terminal settings, and restores them back when exiting. In our situation, when the "old settings" are requested for a non-tty fd (file descriptor), Vim receives all values empty and all options disabled, and carelessly sets the same to your terminal.

    You can see this by running vim < /dev/null, exiting it, then running stty, which will output a whole lot of <undef>s. On Linux, running stty sane will make the terminal usable again (although it will have lost such options as iutf8, possibly causing minor annoyances later).

You could consider this a bug in Vim, since it can open /dev/tty for terminal control, but doesn't. (At some point during startup, Vim duplicates its stderr to stdin, which allows it to read your input commands – from a fd opened for writing – but even that is not done early enough.)

  • 3
    ...with a flourish. Thank you very much! – DevSolar Sep 15 '11 at 16:44
  • 14
    +1, and for TL;DR people just run stty sane – doc_id Feb 11 '15 at 10:33
  • @rahmanisback: The other answers, plus Trevor's comment, all provided ways to avoid terminal breakage in the first place. I accepted grawity's answer, because my question was "why", not "how to avoid" -- that's covered by another question that actually spawned this one. – DevSolar Feb 11 '15 at 10:41
  • @DevSolar Understood, but think about frustrated people like me who just google how to get rid of that behavior while not -unfortunately - have enough time right now to study "why", which is very interesting nonetheless. – doc_id Feb 11 '15 at 18:34
  • 4
    when my terminal breaks, like this, i use reset instead of stty sane and it works fine after that. – Capi Etheriel Apr 21 '15 at 17:19

Following on from grawity's answer, xargs points stdin to /dev/null


From OSX/BSD man xargs

-o      Reopen stdin as /dev/tty in the child process 
        before executing the command.  This is useful 
        if you want xargs to run an interactive application.

Thus the following line of code should work for you:

find . -name "*.txt" | xargs -o vim

For GNU man xargs there is no flag, but we can explicitly pass in /dev/tty to solve the problem:

find . -name "*.txt" | xargs bash -c '</dev/tty vim "$@"' ignoreme

the ignoreme is there to take up $0, so that $@ is all arguments from xargs

  • 2
    How would you create a bash alias out of this? $@ doesn't seem to be translating arguments correctly. – zanegray Aug 31 '15 at 15:47
  • 1
    @zanegray -- you can't make an alias, but you can make it a function. Try: function vimin () { xargs sh -c 'vim "$@" < /dev/tty' vim; } – Christopher Feb 12 '17 at 18:02
  • For a detailed explanation of how the GNU xargs solution works, and why you need the dummy ignoreme string, see vi.stackexchange.com/a/17813 – wisbucky Nov 2 at 20:42
  • @zanegray, You can make it an alias. The quotes are tricky. See solution at vi.stackexchange.com/a/17813 – wisbucky Nov 2 at 20:46

The easiest way:

vim $(find . -name "*foo*")
  • 4
    The main question was "why", not "how to avoid it", and it's been answered to satisfaction two and a half years ago. – DevSolar Mar 8 '14 at 9:35
  • 3
    This, of course, does not work properly when filenames contain spaces or other special characters, and is also a security risk. – Dejay Clayton Jun 17 '15 at 16:14
  • 1
    My favorite answer because it works for every command that lists files, not just "find" or wildcards. It does require a little trust, as Dejay points out. – Travis Wilson Sep 1 '16 at 20:50
  • 1
    This is will not work with many use cases xargs is designed for: e.g., when the number of paths is very high (cc @TravisWilson) – Good Person Nov 30 '16 at 19:47

It should work just fine if you use the -exec option on find rather than piping into xargs. E.g

$ find . -type f -name filename.txt -exec vi {} +

  • 2
    Huh... the trick there is the + (instead of "the usual" \;) to get all the found files into one Vim session -- an option I keep forgetting about. You are right, of course, and +1 for that. I use vim $(find ...) simply out of habit. However, I was actually asking for why the pipe operation screws up the terminal, and grawity nailed that with his explanation. – DevSolar Mar 10 '13 at 18:45
  • 2
    This is the best answer and it works on both BSD/OSX/GNU/Linux. – kevinarpe Feb 24 '14 at 17:17
  • 1
    Also, find is not the only way of getting a list of files that have to be edited simultaneously by vim. I can use grep to find all files with a pattern and try editing them at the same time as well. – Chandranshu Nov 12 '14 at 8:41

Use GNU Parallel instead:

find . -name "*.txt" | parallel -j1 --tty vim

Or if you want to open all the files in one go:

find . -name "*.txt" | parallel -Xj1 --tty vim

It even deals correctly with filenames like:

My brother's 12" records.txt

Watch the intro video to learn more: http://www.youtube.com/watch?v=OpaiGYxkSuQ

  • 1
    Not ubiquitously available. Most of the day I am working on servers where I am not at liberty to install additional tools. But thanks for the hint anyway. – DevSolar Sep 16 '11 at 20:16
  • If your are at liberty to do 'cat > file; chmod +x file' then you can install GNU Parallel: It is simply a perl script. If you want man pages and such, you can install it under your homedir: ./configure --prefix=$HOME && make && make install – Ole Tange Sep 20 '11 at 9:09
  • 2
    OK, tried that - but parallel does not open all the files, it does open them in succession. It's also quite a mouthful for a simple operation. vim $(find . -name "*.txt") is simpler, and you get all files opened at once. – DevSolar Sep 20 '11 at 11:00
  • 5
    @DevSolar: Somewhat unrelated, but both find | xargs and $(find) will have big problems with spaces in file names. – grawity Sep 20 '11 at 18:34
  • 2
    @grawity Correct, but there is no easy way around it (that I know of). You'd have to start fiddling with $IFS, -print0 and stuff, and then you left the realm of a one-shot command line solution and reached a point where you should come up with a script... there's a reason why spaces in filenames are discouraged. – DevSolar Sep 21 '11 at 9:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.