How to edit text on the command line

Halvor Raknes

Member
Joined
Apr 19, 2021
Messages
139
Reaction score
15
Credits
1,081
How do I edit text on the command line? I know how to find previous commands from the bash_history file using ctrl-R, and I know how to move to the beginning and end and one word left/right using ctrl-leftarrow/rightarrow. But what more things can I do to the text on the command line? I particularly would like to know how to delete a whole word…
 
Last edited:


I use the left/right arrows to take the cursor to the last letter of the word, then use backspace to delete each letter one at a time
 
I use the left/right arrows to take the cursor to the last letter of the word, then use backspace to delete each letter one at a time
That's what I do too, but it's so tedious that I figured there must be some way of deleting word by word!
 
Probably is, but I am old and miserable with plenty of time …i have no reason to rush
 
Okay, I've looked around a little more and found the following:

ctrl-w = delete word to the left
alt-d = delete word to the right
ctrl-d = delete character under the cursor

ctrl-k = delete all text to the right of the cursor (to the end o the line)
ctrl-u = delete all text to the left of the cursor (to the start of the line)

Besides, I also found much more comprehensive list!: Bash Shortcuts For Maximum Productivity
 
 
Thanks for the cheat sheet link.
Looking at the cheat sheet you get a pretty good idea of the differences.

I've been using vi since the '90s. So, editing the command line is the same as editing a file. I don't have to think too much and my fingers spend more time on the keys.

With emacs you're using the Ctrl & Meta keys which to me aren't too as conveniently located as the alphanumeric keys. Just my 2¢

For command line editing, on the current command line,
Tap the escape key, then:
l (lower case L) will take you forward one letter at a time,
h will take you back one letter at a time,
w will jump forward to the next word
Note that it may stop at certain punctuation marks,
b will jump back a word,
k will take you to the previous command,
j the next command.
Note that the previous keys are all pretty close together.

Tapping the i key will put you into insert mode esc to exit.
x will delete the current character.
dw will delete the next word.
cw will allow you change the next word.

Once you get the hang of it. It becomes second nature.
Hope it helps.
 
Last edited:
Thanks for the cheat sheet link.
Looking at the cheat sheet you get a pretty good idea of the differences.

I've been using vi since the '90s. So, editing the command line is the same as editing a file. I don't have to think too much and my fingers spend more time on the keys.

With emacs you're using the Ctrl & Meta keys which to me aren't too as conveniently located as the alphanumeric keys. Just my 2¢

For command line editing, on the current command line,
Tap the escape key, then:
l (lower case L) will take you forward one letter at a time,
h will take you back one letter at a time,
w will jump forward to the next word
Note that it may stop at certain punctuation marks,
b will jump back a word,
k will take you to the previous command,
j the next command.
Note that the previous keys are all pretty close together.

Tapping the i key will put you into insert mode esc to exit.
x will delete the current character.
dw will delete the next word.
cw will allow you change the next word.

Once you get the hang of it. It becomes second nature.
Hope it helps.

It should be noted that in order to to use vi-style keybinds in the terminal, instead of the default emacs style ones - you need to enter the following command:
Bash:
set -o vi

That will put bash into vi mode for the current terminal session.

If you want to enable vi mode permanently, add the above line to your .bashrc. Then every time you open a terminal, you can use the vi keybinds!

Some other handy vi-mode binds for the terminal when in command mode (e.g. after pressing escape):
c$ to change everything to the right of the cursor.
c^ to change everything to the left of the cursor.
cc will change/clear the entire line and put you into insert mode.
e to jump to the end of a word.
a to append text to the right of the cursor.
A to append text at the end of the line.
I to insert text at the start of the line.
 
Last edited:

Staff online

Members online


Latest posts

Top