2

When I want to quickly write something to a file that either get's pasted or manually input from the terminal, a quick cat > something.txt and Ctrl+D to close, is a nice shortcut. However of course this does not allow you to move the cursor around in case there is a glaring error, so your stuck opening a text editor such as vim or nano. However these take up your screen unless you use a terminal muxer and it's slightly inconvenient/overkill for my use case. Is there an alternative to cat that allows you to move the cursor? Some kind of micro editor that doesn't grab your screen and is literally just a text buffer until you Ctrl+D it?

My greater use case is I made a little helper script so I can type hel SomeScript in a terminal and it will automatically make a script with that name in my ~/bin which is in my path, add the shebang to the top, and chmod+x when I'm done. I used cat > ~/bin/$1 to start and decided I would make too many mistakes and switched to nano, but since it grabs the screen I can't see what I've been doing in the terminal prior to that without scrolling back and also I keep trying to use vim keys since I'm in an editor lol.

5
  • 1
    man ed (or man sed). Dec 5, 2021 at 0:42
  • @ThomasDickey well that's closer to what I want, but you still can't move the cursor in ed, and you need extra editing keys to do anything.
    – UserZer0
    Dec 5, 2021 at 3:25
  • Are you looking for something that would allow you to move back up to the previous lines too? While still keeping it so that only part of the visible area is used by the editor? A problem there would be that the editor would need to dynamically change the size of the editing area as new lines are entered. I don't think that's impossible, but for such a narrow use-case... You could consider splitting the terminal view with GNU screen or tmux, or just open another terminal emulator in a GUI.
    – ilkkachu
    Dec 5, 2021 at 16:47
  • @ilkkachu "dynamically change the size of the editing area as new lines are entered. I don't think that's impossible, but for such a narrow use-case" it's perfectly possible, and zsh does it. Dec 6, 2021 at 4:10
  • @UncleBilly, well, post that as an answer?
    – ilkkachu
    Dec 6, 2021 at 9:20

2 Answers 2

3

With rlwrap

rlwrap cat

If you configured readline properly (via ~/.inputrc), rlwrap would use the same key bindings as bash and other programs using readline.

With vi

If you're used to modal vi keybindings, there's also the "open mode" of the vi editor (The real vi, not some inadequate clone like vim or nvi).

With zsh

zsh command-line editing interface is also able to do multi-line without taking over the whole screen.

Here is an "implementation" of a quick editor, using zsh's vared builtin (whose purpose is to edit variables):

# in .zshrc
bindkey -N for-qe emacs
bindkey -M for-qe \
   "^M" self-insert-unmeta \
   "^J" self-insert-unmeta \
   "^X" accept-line
qe(){
    local e=$(cat $1; echo x); e=${e%x}
    vared -M for-qe e && printf %s $e > $1
}

# usage
% qe some_file
  # Enter, ^M, ^J = insert a newline
  # ^X = save & exit
  # ^C = abort editing 
1
  • Interesting solutions but rlwrap wasn't installed by default on any of the systems I tried so far. I've tried zsh a couple of times and didn't like it, but this could change my mind, I heard it doesn't play well with scripts so I'll have to do some more investigating.
    – UserZer0
    Dec 8, 2021 at 22:24
2

If you want to stick to your "quick" method and do not want to learn ed you could use:

cat > test <<'EOF'
...
EOF

This is no complete editor, of course, but could edit the line you are typing (before pressing enter).

2
  • That's not too different from just cat > test, though you do get the shell's line editing and not just the dumber line editing the terminal provides. Though too bad even zsh doesn't seem to provide for editing the full here-doc, even though it does support multi-line editing in some contexts as another comment in the Q says. It seems to allow for that if you dig up the command and here-doc from the history...
    – ilkkachu
    Dec 6, 2021 at 11:20
  • Unfortunately that does not work in a script, complains that you can't delimanet a herdoc by end of file.
    – UserZer0
    Dec 10, 2021 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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