Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use vim to open every .txt file under a directory (with Bash)

I am trying the following to use a vim to open every txt file under current directory.

find . -name "*.txt" -print | while read aline; do
  read -p "start spellchecking fine: $aline" sth
  vim $aline
done

Running it in bash complains with

Vim: Warning: Input is not from a terminal
Vim: Error reading input, exiting...
Vim: Finished.

Can anyone explain what could possibly goes wrong? Also, I intend to use read -p for prompt before using vim, without no success.

like image 289
Richard Avatar asked Nov 25 '12 21:11

Richard


People also ask

How to open a file with a filename in Vim?

There are multiple ways we can open files. One way, from a terminal, You can open VIM editor with a filename using the below command It opens the vim editor and the file is opened by default. It opens the main.js file in vim editor. With the command line, You can use -R option with the filename.

How to open a file in read only mode in Vim?

To open a buffer from within vim use the :e or :edit command. If you want to open a file in read only mode open it with the :v or :view command. It can be helpful to know you will not edit critical files sometimes (log or system files). It can get rather tedious typing out the entire path to a file sometimes.

How do I Pass ex commands to Vim?

-c => pass ex commands. Example: vim myfile.txt -c 'wq' to force the last line of a file to be newline terminated (unless binary is set in some way by a script) -s => play a scriptout that was recorded with -W. For example, if your file contains ZZ, then vim myfile.txt -s the_file_containing_ZZ will do the same as previously.

How do I open netrw in Vim?

Netrw can also be opened directly by listing a directory in the command line instead of a file. $ vim . Netrw is powerful and can do more than just open files but that is another article.


1 Answers

Try:

vim $( find . -name "*.txt" )

To fix your solution, you can (probably) do:

find . -name "*.txt" -print | while read aline; do
      read -p "start spellchecking fine: $aline" sth < /dev/tty
      vim $aline < /dev/tty
done

The problem is that the entire while loop is taking its input from find, and vim inherits that pipe as its stdin. This is one technique for getting vim's input to come from your terminal. (Not all systems support /dev/tty, though.)

like image 196
William Pursell Avatar answered Nov 15 '22 07:11

William Pursell