0

There is a codeblock I copy from a personal handbook .txt file. I paste it in the terminal and execute by hitting Enter (Return). Recently I indented it there from aesthetic reasons but when I copy it indented the execution breaks (a secondary prompt is opened). To deal with this phenomenon I thought of using a FIFO.

I try make a FIFO to which I pass the following codeblock:

⇨⇨⇨⇨(
⇨⇨⇨⇨Indented commands...
⇨⇨⇨⇨)

The FIFO should remove all leading whitespaces (usually tabulations but maybe also spaces) from this original codeblock, and return a unindented version of it:

(
commands...
)

As far as I understand, the code I should put in the FIFO file is 's/^\s*//g'.

My question:

When I paste the code block to a Bash terminal, how could I make it effected by the FIFO file I created, before I execute it?

Update:

This is the full syntax I tested. It failed to be executed properly (no errors, just a secondary prompt opened), hence I consider a FIFO filter:

⇨⇨⇨⇨(
⇨⇨⇨⇨command1...
⇨⇨⇨⇨cat <<-'PMA' > /opt/script.sh
⇨⇨⇨⇨⇨⇨⇨⇨#!/bin/bash
⇨⇨⇨⇨⇨⇨⇨⇨strings...
⇨⇨⇨⇨PMA
⇨⇨⇨⇨command2...
⇨⇨⇨⇨) | sed 's/^\s*//g'
  • Angles of attack: While Zsh has preexec hook function, Bash doesn't; still, you can do something. I believe one does not paste to Bash directly but to the terminal emulator. For this reason Bash cannot know whether the particular command was typed or pasted, so your hook will affect every command. Your desktop environment may have a method to change its clipboard content on the fly (e.g. KDE has, I think) but it will alter copy-pasting desktop-wide. I don't know any terminal emulator with the ability to change pasted text, maybe there is at least one. – Kamil Maciorowski May 02 '17 at 21:15
  • 2
    BTW, why won't you convert your personal handbook .txt file into scripts and/or functions and/or aliases? It seems to me you seek an overcomplicated solution to a problem originating in your cumbersome way of work. – Kamil Maciorowski May 02 '17 at 21:28
  • Trust me, my way of work is not cumbersome - I work crossplatform, with Windows10 and WSL, and I copy from txt that includes more data than command sets (I do use separate script files). –  May 02 '17 at 23:15

1 Answers1

0

I don't believe that you can (reasonably easily) simply make a FIFO (aka a 'named pipe') that alters the data passing through it. It would probably be quite a bit simpler to filter the data as it comes out of the far end of the pipe, as with [...] | sed 's/^\s*//' (the g flag is superfluous as "the beginning of the line" can only occur once per line).

DopeGhoti
  • 76,081
  • I tried to do what you suggest, without the g argument, but it still doesn't work. The execution breaks with a secondary prompt is opened. Please see my update. –  May 02 '17 at 17:58
  • Now it's cat <<-'PMA' | sed 's/^\s*//' > /opt/pma.sh but Sadly, still the same outcome... Maybe it's because I send all of it to tmux with tmux new-session -d 'bash /opt/script.sh' but that's unlikely... –  May 02 '17 at 23:27