0

In the Linux terminal, I initially entered the following command:

tail -n +80 numbers.txt

After realizing that I made a mistake and actually wanted to use +70 instead of +80, I wanted to correct it. To do this, I moved the cursor to the digit '8' and then entered '7', which inserted '7' before '8', resulting in '780'. To achieve '70', I needed to delete the '8'. However, this process seemed slow to me. Is there a way to directly replace the '8' with '7' in a single step, like simply replacing the '8' with '7'?

5
  • I don't want to be rude or anything, but... if you find this slow then I'm afraid you're going to have a pretty bad time using your computer, because this is how text editing works in almost any piece of software. A tiny hint that might be useful, and not just in terminals: You can press 7 and Delete at the same time, it doesn't matter in which order the computer registers and executes these two steps. Commented Oct 3, 2023 at 18:37
  • @egmont In this case, it's just one letter, so you can quickly delete it. However, what if you want to delete a word or several words? Deleting one letter at a time is very slow. "... because this is how text editing works in almost any piece of software." Is it? I mostly use Windows, and I believe in every text editor I can select what I want to delete as a whole and then just delete it. However, I don't see that feature in the Linux terminal. Commented Oct 6, 2023 at 0:47
  • 1
    You might want to rephrase your question (including the title) or post a new one, because your question was very specifically about the very step of replacing one single letter by another, and getting surprised that pressing a letter inserts it rather than overwriting. And this step is the same as in almost every software. Other steps are different in terminals than typical graphical software, maybe you wish to ask about them too :) Commented Oct 6, 2023 at 5:10
  • Please update your question to include the shell you are using (e.g. bash orzsh or other), and also indicate whether you first executed the wrong command or not (which means it would end up in the shell's command history, which some shells can recall and make substitutions in before executing the command again). The terminal has very little to do with this nor the fact that you are using CentOS. Commented Oct 8, 2023 at 9:54
  • With bash and Emacs-style editing, Ctrl-W deletes one word backwards. Ctrl-U deletes from the current position to begin-of-line. Commented Oct 9, 2023 at 16:39

2 Answers 2

6

In Bash, if you have history expansion enabled (which is on by default though some disable it), then entering ^old^new will re-run the last command with a substitution applied (I think it's short for !:s/old/new/ or something). So in your case, ^8^7 without even having to arrow-key through the entire command.

Typing a new tail -n +70 command and pressing Alt. to insert the last argument of the previous command may be faster than editing the previous one. Similarly, tail -n +70 $_ should do the same.

(For more complex changes: Pressing Ctrlx, Ctrle will open your default editor ($VISUAL) where you can edit the command and immediately submit it.)

5
  • 1
    I think the question is asking about changing 8 to 7 before executing the line — so there’s no history to manipulate. Commented Oct 3, 2023 at 7:42
  • @StephenKitt I'd say it's ambiguous - the OP says they entered the command rather than typed it Commented Oct 3, 2023 at 9:19
  • @roaima they also say they entered '7', so it seems they're using it synonymously with typing. Commented Oct 4, 2023 at 3:25
  • +1, that's good to know, as I haven't learned that either. Sorry for being ambiguous; Stephen Kitt was right in that case. I haven't entered that into the terminal, so there would be no history. Commented Oct 6, 2023 at 0:41
  • Interesting. I didn't know about Alt+. or $_; for the latter I've always used !$ Commented Oct 8, 2023 at 1:49
4

Assuming you're using bash, the overwrite mode is disabled and not bound to any shortcut by default:

Toggle overwrite mode. With an explicit positive numeric argument, switches to overwrite mode. With an explicit non-positive numeric argument, switches to insert mode. This command affects only emacs mode; vi mode does overwrite differently. Each call to readline() starts in insert mode.

In overwrite mode, characters bound to self-insert replace the text at point rather than pushing the text to the right. Characters bound to backward-delete-char replace the character before point with a space.

By default, this command is unbound.

You can add a keybinding and enable it, then disable it again once you're done, but I suppose the best thing to would be to use delete-char instead, bound to Ctrld by default:

Delete the character at point. [...]

Once your cursor is on the 8, press Ctrld to delete it, and then type in the 7.

In vi mode, when already on the 8: <Esc>r7. Or the whole thing can be done using <Esc>F8r7 (starting at the end of the line) to move the cursor to the first 8 and then replacing it with 7.

1
  • Thanks. That sounds complex. I was thinking the feature was there; I just didn't know about it. Commented Oct 6, 2023 at 0:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.