0

If you've typed a command, especially a lengthy one, in a Linux terminal, and you want to delete one or more words and replace them with something else, is there a way to do this quickly within the terminal?

This isn't related to Linux specifically, but in applications like Notepad, you can select a word or words and delete them in a single step. However, as far as I know, in the terminal, I have to delete them letter by letter, which can be a slow process.

I'm aware that Vim can do this, but let's assume that I want to edit it directly in the terminal without opening it in Vim.

2
  • 2
    Does this replies you using a vim behavior on command line : unix.stackexchange.com/questions/30454/…
    – admstg
    Commented Oct 6, 2023 at 7:18
  • 1
    Just like with your previous question: It would help to know what shell you are using and whether you actually executed the command or whether this is a question about navigating the command line as you are writing it. The terminal has very little to do with this nor the fact that you are using CentOS.
    – Kusalananda
    Commented Oct 8, 2023 at 9:59

4 Answers 4

1

By default you can use the following keybindings[1] to move cursors in terminal

alt + b   move one word backward
alt + f   move one word forward
ctrl + b  move one character forward
ctrl + f  move one character forward

Moving by a word is much faster than moving by a character. Once your character is in position you can use

alt + d  to delete a word in cursor position
ctrl + d  delete a character in cursor position

If you have vi keybinding setup (thanks @admstg) with bash, you can type ESC key to enter vi mode. Here is how you can move your cursor with vi keybindings[2]

h    move left by a character
l    move right by a character
b    move back by a word
w    move forward by a word
dw   delete word at cursor
x    cut a character at cursor
u    undo
r    replace a character
v    enter vi editor to edit the command

References:

1
  • Home and End are also useful, for moving quickly to the beginning or end of the command, respectively. Ctrl-W to delete backwards one word, Ctrl-Y to "yank" back the cumulatively deleted content, Ctrl-U to delete backwards to BoL, etc. Many more.
    – Jim L.
    Commented Oct 6, 2023 at 17:24
1

I personally prefer to use the following keys frequently. I'm using bash mostly with its default settings, with the following few extras defined in .inputrc:

"\e[1;5A": previous-screen-line
"\e[1;5B": next-screen-line

CtrlUp and CtrlDown move the cursor to the previous/next visual line of the command. Useful for very long, multiline commands.

"\e[1;5C": forward-word
"\e[1;5D": backward-word

CtrlLeft and CtrlRight to move the cursor horizontally by words.

"\e[3;5~": kill-word
"\b": backward-kill-word

CtrlDelete and CtrlBackspace to delete an entire word on the right/left. (AltBackspace should do it by default, but I prefer Ctrl for consistency with other apps as well as with the other shortcut keys.)

And of course Home, End, Delete, Backspace as usual, these work out of the box.

Consult the readline manual, section "EDITING COMMANDS" to look for other possibilities that you may find convenient.

Switching to another popular shell, e.g. zsh or fish might offer more (or just different) options, I'm not familiar with them, you might also want to take a look.

A few terminal emulators (I'm not sure which ones) allow to position the cursor with the mouse. This is a hack, a somewhat unreliable guesswork: The terminal synthesizes several Left or Right keypresses (according to the relative location of the click point vs the cursor), just as if you pressed these arrow keys this many times, and assumes that the application (the shell) will react to this by moving the cursor there (which is often, but not always a correct guess).

1

If you are using the usual default emacs key bindings (IE not vi bindings) you can use CtrlW to delete backwards from the cursor to the next space boundary.

That can be a bit aggressive when, say, trying to remove part of a path or URL, so there is also AltBackspace which respects various other word boundary characters.

There are many bindings available, too many to list here.

1

You can use vi commands directly in the terminal by using set -o vi.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .