Last Updated: November 09, 2021
·
2.722K
· dmichaelavila

Edit Text Like A Pro(grammer)

When programmers aren't in vi they edit text with the following:

  • ^a - beginning of line
  • ^e - end of line
  • ^f - forward one character
  • ^b - backward one character
  • ^n - down one line
  • ^p - up one line
  • ^d - delete one character forward
  • ^h - delete one character backward
  • ^w - delete one word backward
  • ^k - kill all characters forward on this line
  • ^y - put the last thing you killed

^ means the ctrl key, e.g. ^a means "press and hold ctrl, then press a, then release both"

Try them out. These combinations work in nearly every single place you deal with text. If you know of a place that these combinations do not work please let us know below! Or if you know of important keys I've missed here please list those as well.

I thought I'd share a scenario in which I often use several of these combos:

I'm at a command prompt in the middle of typing a command when I realize that I actually need to run a different command first. Now, I can ^c to bring up a fresh prompt, but that loses the command I've already typed. Instead, I would ^a to go to the beginning of the line, ^k to kill the line, I'd then run the command I needed to run first, once that was done I would ^y to put the line I killed before back. At which point I can finish typing the command and run it.

Don't leave home row.

Update 1: This does not work in Gnome.

19 Responses
Add your response

Funny. These are the defaults in Emacs.

over 1 year ago ·

Where do you think I learned them from? :)

over 1 year ago ·

If you love vi so much, why don't you just marry it?

set -o vi  # :-)

https://www.gnu.org/software/bash/manual/html_node/Command-Line-Editing.html

over 1 year ago ·

@jayallen that is just at the command prompt. I wasn't saying programmers only use vi. I was just saying that these commands work everywhere outside of vi (e.g. emacs, sublime, command prompt, browser text field, browser url field, even renaming a filename in your OS GUI.) These commands, familiar to any emacs user, should be known by all programmers, because they are used just about everywhere we deal with text. That's all.

over 1 year ago ·

Don't forget ^u to kill everything previous on the line.
Which can then be recalled with ^y as well.

over 1 year ago ·

@djtb ^u think this only works in emacs + zsh/bash. Not system wide.

over 1 year ago ·

@alanpearce thanks.

over 1 year ago ·

"These commands, familiar to any emacs user, should be known by all programmers, because they are used just about everywhere we deal with text." That's why you should use Emacs instead of vi ;)

over 1 year ago ·

@zhimingwang there are good reasons to be familiar with both!

over 1 year ago ·

@dmichaelavila Yeah, I'm not saying you should never touch vi; I'm saying it's a good idea to export EDITOR=emacs, git config --global core.editor emacs, etc. Of course vi still has the advantage of being installed everywhere. In the rare cases when even vi isn't installed, some familiarity with ed might help, too ;)

over 1 year ago ·

@zhimingwang I still lovingly use vi :)

over 1 year ago ·

@dmichaelavila It's a religious war!

over 1 year ago ·

@zhimingwang ha, not a war at all. Just two people who use two different editors.

over 1 year ago ·

There's indeed an editor war, you know. But good humor is always more important :)

over 1 year ago ·

@zhimingwang I'm aware of the war, just making the point that I'm not participating in it. And I agree, I'm not taking any of this too seriously ...

over 1 year ago ·

Don't forget that :

  • ^(Left Arrow) Moves left one word
  • ^(Right Arrow) Moves right one word.
over 1 year ago ·

These look like shortcuts in a terminal. But then I don't know why say it doesn't work in Gnome. That's desktop environment. And these certainly work in gnome-terminal, xterm, in fact it's up to the shell you're using, not so much the terminal.

And why would you use ^f and ^b instead of the arrow keys? Moving word by word is much more useful. In gnome-terminal I can use Alt+Left/Right, but that doesn't work on a Mac without hacking ~/.inputrc. An alternative that works almost everywhere is ESC Left/Right. Don't hold down ESC, but press and release, and then press and release Left or Right.

You can read more about these and many more in man bash, in the Commands for Moving section. You might also like my article on a very related topic: http://www.linuxjournal.com/content/time-saving-tricks-command-line

over 1 year ago ·

@janosgyerik we're talking about system wide shortcuts. ^f and ^b are right on home row so no reaching, that's why I prefer it (very much so in fact.) ESC-f and ESC-b work per word in most places (and you can do ^[ instead of ESC as usual.)

Remember though, this is about system-wide commands for editing text. We have a lot of options at the command prompt, but programmers spend their time all over the place.

over 1 year ago ·

It's very useful ,Thanks!

over 1 year ago ·