Skip to main content
Tweeted twitter.com/StackUnix/status/730291875463233536
added 253 characters in body
Source Link
theideasmith
  • 630
  • 1
  • 5
  • 10

Sometimes I need to write text and then pipe that text into another command. My usual workflow goes something like this:

vim # I edit and save my file as file.txt cat file.txt | pandoc -o file.pdf # pandoc is an example rm file.txt 

I find this cumbersome and seeking to learn bash scripting I'd like to make the process much simpler by writing a command which fires open an editor and when the editor closes pipe the output of the editor to stdout. Then I'd be able to run the command as quickedit | pandoc -o file.pdf.

I'm not sure how this would work. I already wrote a function to automate this by following the exact workflow above plus some additions. It generates a random string to act as a filename and passes that to vim when the function is invoked. When the user exits vim by saving the file, the function prints the file to the console and then deletes the file.

function quickedit { filename="$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32)" vim $filename cat $filename rm $filename } # The problem: # => Vim: Warning: Output is not to a terminal 

The problem I soon encountered is that when I do something like quickedit | command vim itself can't be used as an editor because all output is constrained to the pipe.

I'm wondering if there are any workarounds to this, so that I could pipe the output of my quickedit function. The suboptimal alternative is to fire up a separate editor, say sublime text, but I really want to stay in the terminal.

Sometimes I need to write text and then pipe that text into another command. My usual workflow goes something like this:

vim # I edit and save my file as file.txt cat file.txt | pandoc -o file.pdf # pandoc is an example rm file.txt 

I find this cumbersome and seeking to learn bash scripting I'd like to make the process much simpler by writing a command which fires open an editor and when the editor closes pipe the output of the editor to stdout. Then I'd be able to run the command as quickedit | pandoc -o file.pdf.

I'm not sure how this would work. I already wrote a function to automate this by following the exact workflow above plus some additions. It generates a random string to act as a filename and passes that to vim when the function is invoked. When the user exits vim by saving the file, the function prints the file to the console and then deletes the file.

The problem I soon encountered is that when I do something like quickedit | command vim itself can't be used as an editor because all output is constrained to the pipe.

I'm wondering if there are any workarounds to this, so that I could pipe the output of my quickedit function. The suboptimal alternative is to fire up a separate editor, say sublime text, but I really want to stay in the terminal.

Sometimes I need to write text and then pipe that text into another command. My usual workflow goes something like this:

vim # I edit and save my file as file.txt cat file.txt | pandoc -o file.pdf # pandoc is an example rm file.txt 

I find this cumbersome and seeking to learn bash scripting I'd like to make the process much simpler by writing a command which fires open an editor and when the editor closes pipe the output of the editor to stdout. Then I'd be able to run the command as quickedit | pandoc -o file.pdf.

I'm not sure how this would work. I already wrote a function to automate this by following the exact workflow above plus some additions. It generates a random string to act as a filename and passes that to vim when the function is invoked. When the user exits vim by saving the file, the function prints the file to the console and then deletes the file.

function quickedit { filename="$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-f0-9' | head -c 32)" vim $filename cat $filename rm $filename } # The problem: # => Vim: Warning: Output is not to a terminal 

The problem I soon encountered is that when I do something like quickedit | command vim itself can't be used as an editor because all output is constrained to the pipe.

I'm wondering if there are any workarounds to this, so that I could pipe the output of my quickedit function. The suboptimal alternative is to fire up a separate editor, say sublime text, but I really want to stay in the terminal.

Source Link
theideasmith
  • 630
  • 1
  • 5
  • 10

How to invoke vim editor and pipe output to bash

Sometimes I need to write text and then pipe that text into another command. My usual workflow goes something like this:

vim # I edit and save my file as file.txt cat file.txt | pandoc -o file.pdf # pandoc is an example rm file.txt 

I find this cumbersome and seeking to learn bash scripting I'd like to make the process much simpler by writing a command which fires open an editor and when the editor closes pipe the output of the editor to stdout. Then I'd be able to run the command as quickedit | pandoc -o file.pdf.

I'm not sure how this would work. I already wrote a function to automate this by following the exact workflow above plus some additions. It generates a random string to act as a filename and passes that to vim when the function is invoked. When the user exits vim by saving the file, the function prints the file to the console and then deletes the file.

The problem I soon encountered is that when I do something like quickedit | command vim itself can't be used as an editor because all output is constrained to the pipe.

I'm wondering if there are any workarounds to this, so that I could pipe the output of my quickedit function. The suboptimal alternative is to fire up a separate editor, say sublime text, but I really want to stay in the terminal.