I have the following bash script (play.bash):
#!/bin/bash
pico2wave -l=en-US -w=/tmp/test.wav "$1"
aplay /tmp/test.wav
rm /tmp/test.wav
I would like to use it for reading selected text in the vim editor.
The following does not work
:'<,'>w !bash play.bash
The command executes, but I'm not hearing it.
I'd like to only hear the text, without leaving the vim window.
Is this possible?
When using the ! command in vim, your selection will be piped to your script on standard input, not given as a command line argument.
A simple solution would be to modify your script so it reads from standard input:
xargs pico2wave -l=en-US -w=/tmp/test.wav
xargs reads from standard input, then calls pico2wave with the data as command line arguments, removing newlines.
Related
I have two simple scripts:
./cpthat
BlueM/cliclick types on the keyboard: Shift+Cmd+A, then Cmd+C, to the active iTerm terminal:
#!/bin/zsh
cliclick kd:shift,cmd t:a ku:shift t:c ku:cmd
pbpaste>$THATF
Shift+Cmd+A selects the output from the previous command, and
Cmd+C copies "that" to the clipboard.
pbpaste then writes that to the file $THATF defined system-wide.
./that
#!/bin/zsh
cat $THATF
This prints out the output of the last command as stored by cpthat.
(I know I can run $ command > $THATF directly but for other reasons I need to act retroactively on the command output. Also, not thread safe.)
The challenge:
I'm trying to get to where I can start a zsh or bash command with a pipe:
$ |grep -i sometext
Where, in effect, this happens:
$ that|grep -i sometext
Would this be possible somehow?
Overriding the pipe operator?
zsh config magic?
I'm using zsh heavily but am open for any solution.
You don't need to start with a |. Thegrep utility naturally reads STDIN.
Here's a contrived example:
# /bin/sh
# count_matches
grep $1 | wc -l
$ cat file | count_matches thing
You can see the | you're looking for is on the command line itself, not within the script
Similarly this works:
$ count_matches thing < file
In the first example, the STDIN is connected (via the pipe) to the output of the first command (trivially cat). In the second, it's from the actual file via redirection.
So, just get rid of the | and you should be good to go.
Animation showing that | alone at the beginning of command can be replaced automatically by the output of previous command:
Edit ~/.zshrc to override zsh's zle accept-line widget:
readonly THATF="path/to/your/temporary/file"
my-accept-line () {
if [[ "${BUFFER:0:1}" == '|' ]]; then
/usr/local/bin/cliclick kd:shift,cmd t:a ku:shift w:100 t:c ku:cmd
pbpaste>"${THATF}"
BUFFER='cat ${THATF} '${BUFFER}
fi
zle .accept-line
}
zle -N accept-line my-accept-line
Explanation
When you hit enter after entering a command, zsh runs the accept-line widget.
We override that widget, but before exiting we remember to call the original widget with zle .accept-line. With the dot prefix the factory widget is ran.
In iTerm2, shift+cmd+a selects all the output from the previous command, and cmd+c copies that to the system pasteboard.
We paste the contents of the pasteboard and redirects that to the temporary file declared earlier, pointed to by ${THATF}.
We prepend $BUFFER, the zsh special variable available within zle widget code, with the output of the previous command.
Dependencies, caveats:
This particular solution depends on:
cliclick dispatching macOS keyboard events. Perhaps a native solutions exist e.g. ANSI/escape sequence.
iTerm to handle the keybind for copying last commands output.
zsh for the zle widget.
Xode snippet above is proof-of-concept only and is wildly insecure.
Each line in a given file 'a.txt' contains the directory/path to another unique file. Suppose we want to parse 'a.txt' line-by-line, extract the path in string format, and then use a tool such as vim to process the file at this path, and so on.
After going through this thread - Read a file line by line assigning the value to a variable, I wrote the following script, say 'open-file.sh' on bash (I'm new to it)
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
vim -c ":q" -cq $line # Just open the file and close it using :q
done < "$1"
We would then run the above script as -
./open-file.sh a.txt
The problem is that although the path to a new file is correctly specified by $line, when vim opens the file, vim continues to receive the text contained in 'a.txt' as a command. How can I write a script where I can correctly obtain the path from 'a.txt', open it using vim, and then continue parsing the remaining lines in 'a.txt' ?
Replace:
vim -c ":q" -cq $line
With:
vim -c ":q" -cq "$line" </dev/tty
The redirection </dev/tty tells vim to take its standard input from the terminal. Without that, the standard input for vim is "$1".
Also, it is good practice to put $line in double-quotes to protect it from word splitting, etc.
Lastly, while vim is excellent for interactive work, if your end-goal is fully automated processing of each file, you might want to consider tools such as sed or awk.
Although I'm not sure of your ultimate goal, this shell command will execute vim once per line in a.txt:
xargs -o -n1 vim -c ':q' < a.txt
As explained in the comments to Read a file line by line assigning the value to a variable, the issue you're encountering is due to the fact that vim is an interactive program and thus continues to read input from $line.
The problem was already mentioned in a comment under the answer you based your script on.
vim is consuming stdin which is given to the loop by done < $1. We can observe the same behavior in the following example:
$ while read i; do cat; done < <(seq 3)
2
3
<(seq 3) simulates a file with the three lines 1, 2, and 3. Instead of three silent iterations we get only one iteration and the output 2 and 3.
stdin is not only passed to read in the head of the loop, but also to cat in the body of the loop. Therefore read reads one line, the loop is entered, cat reads all remaining lines, stdin is empty, read has nothing to read anymore, the loop exits.
You could circumvent the problem by redirecting something to vim, however there is an even better way. You don't need the loop at all:
< "$1" xargs -d\\n -n1 vim -c :q -cq
xargs will execute vim once for every line in the file given by $1.
I have a simple Bash script:
#!/usr/bin/env bash
read X
echo "X=$X"
When I execute it with ./myscript.sh it works. But when I execute it with cat myscript.sh | bash it actually puts echo "X=$X" into $X.
So this script prints Hello World executed with cat myscript.sh | bash:
#!/usr/bin/env bash
read X
hello world
echo "$X"
What's the benefit of executing a script with cat myscript.sh | bash? Why doesn't do it the same things as if I execute it with ./myscript.sh?
How can I avoid Bash to execute line by line but execute all lines after the STDIN reached the end?
Instead of just running
read X
...instead replace it with...
read X </dev/tty || {
X="some default because we can't read from the TTY here"
}
...if you want to read from the console. Of course, this only works if you have a /dev/tty, but if you wanted to do something robust, you wouldn't be piping from curl into a shell. :)
Another alternative, of course, is to pass in your value of X on the command line.
curl https://some.place/with-untrusted-code-only-idiots-will-run-without-reading \
| bash -s "value of X here"
...and refer to "$1" in your script when you want X.
(By the way, I sure hope you're at least using SSL for this, rather than advising people to run code they download over plain HTTP with no out-of-band validation step. Lots of people do it, sure, but that's making sites they download from -- like rvm.io -- big targets. Big, easy-to-man-in-the-middle-or-DNS-hijack targets).
When you cat a script to bash the code to execute is coming from standard input.
Where does read read from? That's right also standard input. This is why you can cat input to programs that take standard input (like sed, awk, etc.).
So you are not running "a script" per-se when you do this. You are running a series of input lines.
Where would you like read to read data from in this setup?
You can manually do that (if you can define such a place). Alternatively you can stop running your script like this.
I have multiple files to edit. I have to make certain changes, like copying some lines based on patterns and then I have to insert some text at different places. I am planning to use vim to automate my task. I have written this script
gg
i
some text to add
some more text to add
<esc>
:%s/text/TEXT/g
:wq
But it just opens up the vim editor and inserts even the commands in the file and then I have to manually remove the following text
<esc>
:%s/text/TEXT/g
:wq
and save the file.
I am invoking the following command:
vi -s vimscript mytextfile
I have used vim scripting earlier to do other things than inserting text like searching and replacing or copy-pasting patterns etc.
Alternatives
Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.
That said, you can use Vim non-interactively:
Silent Batch Mode
For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.
REM Windows
call vim -N -u NONE -n -es -S "commands.ex" "filespec"
Note: silent batch mode (:help -s-ex) messes up the Windows console, so you may have to do a cls to clean up after the Vim run.
# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"
Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.
Full Automation
For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:
vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"
Here's a summary of the used arguments:
-T dumb Avoids errors in case the terminal detection goes wrong.
-N -u NONE Do not load vimrc and plugins, alternatively:
--noplugin Do not load plugins.
-n No swapfile.
-es Ex mode + silent batch mode -s-ex
Attention: Must be given in that order!
-S ... Source script.
-c 'set nomore' Suppress the more-prompt when the screen is filled
with messages or output to avoid blocking.
From :help -s:
-s {scriptin} The script file "scriptin" is read. The characters in the
file are interpreted as if you had typed them. The same can
be done with the command ":source! {scriptin}". If the end
of the file is reached before the editor exits, further
characters are read from the keyboard. Only works when not
started in Ex mode, see |-s-ex|. See also |complex-repeat|.
{not in Vi}
Think of it as a macro. This is how your vimscript file should look:
Osome text to add^Msome more text to add^[:%s/text/TEXT^M:wq^M
The ^M special character is a literal <CR> and is obtained with <C-v><CR>.
The ^[ special character is a literal <Esc> and is obtained with <C-v><Esc>.
Why using vim to automate that kind of task whereas there are many commands or shell languages which are excellent and more efficient to do it ? You could use other tools instead of vim to automate your task.
You could try to use sed for exemple :
[ ~]$ cat file
Here is a file
[ ~]$ echo "some text to add" >> file
[ ~]$ echo "some more text to add" >> file
[ ~]$ cat file
Here is a file
some text to add
some more text to add
[ ~]$ sed -i "s/text/TEXT/g" file
[ ~]$ cat file
Here is a file
some TEXT to add
some more TEXT to add
EDIT : There are different ways to insert some text at the top of a file.
Using a temporary file :
echo "some text to add" > tmp.txt
cat file.text >> tmp.txt
mv tmp.text file.txt
Using sed :
sed -i "1isome text to add" file.text # or
sed -i "1s/^/some text to add\n/" file.txt
Using subshell :
echo -e "some text to add\n$(cat file.txt)" > file.txt
I find it more natural to use a standard Vim script, not a normal mode script. You can invoke it using:
vim -c "source vimscript" mytextfile
Your vim script will need a little update to work using this approach. It will look something like:
1
i
some text to add
some more text to add
.
%s/text/TEXT/g
wq
I am a bit new to bash scripting and have not been able to find an answer for what I am about to ask, that is if it is possible.
I have a text file which is created by search a directory using grep for files containing "Name" and outputs the below, say the file is called PathOutput.txt
/mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/4c3483af-b41a-4979-98b7-6f6a4f147670/4c3483af-b41a-4979-98b7-6f6a4f147670.ovf
/mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/5b5538a5-423f-4eaf-9678-d377a6706c58/5b5538a5-423f-4eaf-9678-d377a6706c58.ovf
/mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/0e2d1451-45cc-456e-846d-d174515a60dd/0e2d1451-45cc-456e-846d-d174515a60dd.ovf
/mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/daaf622e-e035-4c1b-a6d7-8ee209c4ded6/daaf622e-e035-4c1b-a6d7-8ee209c4ded6.ovf
/mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/48f52ab9-64df-4b1e-9c35-c024ae2a64c4/48f52ab9-64df-4b1e-9c35-c024ae2a64c4.ovf
Now what I would like to do if possible is loop through the file with a command, using a variable to bring in each line in the text file. But I cannot work out a way to run the command againist each line. With all my playing around I did get a results where it would run once against the first line, but this was when the output of grep was piped into another command.
At the moment in a bash script I am just extracting the paths to PathOutput.txt, cat to display the paths, then copy the path I want to a read -p command to create a variable to run against a command. It works fine now, just have to run the script each time for each path. If I could get the command to loop through each line I could output the results to a txt file.
Is it possible?
You could use xargs:
$ xargs -n1 echo "arg:" < file
arg: /mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/4c3483af-b41a-4979-98b7-6f6a4f147670/4c3483af-b41a-4979-98b7-6f6a4f147670.ovf
arg: /mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/5b5538a5-423f-4eaf-9678-d377a6706c58/5b5538a5-423f-4eaf-9678-d377a6706c58.ovf
arg: /mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/0e2d1451-45cc-456e-846d-d174515a60dd/0e2d1451-45cc-456e-846d-d174515a60dd.ovf
arg: /mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/daaf622e-e035-4c1b-a6d7-8ee209c4ded6/daaf622e-e035-4c1b-a6d7-8ee209c4ded6.ovf
arg: /mnt/volnfs3rvdata/4007dc45-477a-45b2-9c28-43bc5bbb4f9f/master/vms/48f52ab9-64df-4b1e-9c35-c024ae2a64c4/48f52ab9-64df-4b1e-9c35-c024ae2a64c4.ovf
Just replace echo "arg:" with the command you actually want to use. If you want to passed all the files at once drop the -n1 option.
If I understand correctly, you may want something like this:
for L in `cat PathOutput.txt`; do
echo "I read line $L from PathOutput.txt"
# do something useful with $L
done