hluqma
Last Updated: May 20, 2016
·
2.165K
· dmichaelavila
Photo

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.

Say Thanks
Respond

20 Responses
Add your response

13169
Fd95a5b2b90662b969de0b4128564fbe

Funny. These are the defaults in Emacs.

over 1 year ago ·
13171
Photo

Where do you think I learned them from? :)

over 1 year ago ·
13195
4a54ccb0eaef0699ddec7e477af6b72f

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 ·
13196
Photo

@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 ·
13202
Shoes av

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

over 1 year ago ·
13205
Photo

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

over 1 year ago ·
13234
C6f1397bde3843a22d0b2ba9c3aee773

This is only a system-wide thing on OSX, sadly. Gnome users can switch the keyboard 'theme' to Emacs, but that remaps all the common UI keyboards.

over 1 year ago ·
13240
Photo

@alanpearce thanks.

over 1 year ago ·
13249
8a39dd600981979b8a7069136246fae5

"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 ·
13252
Photo

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

over 1 year ago ·
13265
8a39dd600981979b8a7069136246fae5

@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 ·
13266
Photo

@zhimingwang I still lovingly use vi :)

over 1 year ago ·
13267
8a39dd600981979b8a7069136246fae5

@dmichaelavila It's a religious war!

over 1 year ago ·
13268
Photo

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

over 1 year ago ·
13269
8a39dd600981979b8a7069136246fae5

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

over 1 year ago ·
13270
Photo

@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 ·
13286
1642b4962ff22912a5cd7a1fd75fcd9d

Don't forget that :

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

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 ·
13316
Photo

@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 ·
13366
Chino chris.whitebg

It's very useful ,Thanks!

over 1 year ago ·