BASH COMMAND LINE WITH VIM AND EMACS MODE SHORTCUTS

BASH Command line Editing with Vim and EMACS Mode shortcuts options

This post is not directly related to programming, but I think that many developers can benefit from a few tips for the command line, since linux in it’s many flavours is widely used by developers out there.

So anyways…, as you may or may not know, I’m a big fan of what I like to call efficiency focused text editors, you may know them by the names of VIM and Emacs (there might be others, but those two are my favorites).
Even though they have different approaches to editing text on an efficient way, the main idea is the same: you don’t need to move your hands out of the “writing position” to achieve things like moving around the document, copy&pasting, selecting text, etc, etc.

If you’re like me, a fan of either one of them, you’ll be happy to know that you can do similar things on your linux command line. Yes, you read correctly: Bash has an emacs and a Vi mode (VI not VIM, almost the same)!

Emacs mode cheat sheet

This is the best one, simply because it’s the default mode for Bash. That’s right, out of the box, Bash allows you to move around your command line using Emacs commands.

For the complete cheat sheet, click here (credit goes to Peteris Krumins from http://www.catonmat.net).

Here are some examples of what you might do on your command line with it’s default mode:

Navigation

Moving around the line is as simple as it is on Emacs. Lets say you have the following command already typed in:

  %user>ls etc/apache2 _

You probably forgot the initial “/” on your path, so going back is as simple as hitting M-b twice!
Say that you now changed your mind, and decided that you want to see the content of your .shhfolder:

  %user>ls _etc/apache2 

Just hit C-k and write your new path!

  %user>ls ~/.ssh

Completition

Yes, on Bash you already have tab completition, but the one provided by the emacs mode is a bit more powerful:

Want to autocomplete an environment variable’s name? Hit C-x $
Want to autocomplete a hostname? Hit C-x @

You also have the usual completition commands that are available using the tab key:

Complete filename: Hit C-x /
Complete command name: Hit C-x !

Command repetition

One final note about the emacs mode: You can repeat the same command as many times as you wish the same way you would on your Emacs: M-[NUMBER] (being [NUMBER] the number of times you want to repeat your command).
For example, say your accidentally wrote everything in caps:

  %user>CAT /ETC/APACHE2/HTTP.CONF _

Just hit M-5 then M-b and you’re at the begining of the line, then hit: M-5 then M-l and it’s all lowecased!

Vi mode cheat sheet and shortcuts

Bash edit command line in vim / Vi editor,

The Vi mode for our bash is a very powerful mode too, and setting it up is not difficult at all, all you have to do is execute the following bash vi commands:

  %> set -o vi

You can add it to your .bashrc file so, everytime you open a new terminal window, it’ll auto-set the Vi mode.

For the complete cheat sheet, click here (credit goes, again, to Peteris Krumins from http://www.catonmat.net).

 

Here are some examples of what you might do on your command line with the Vi mode:

Navigation

Just like on the previous mode, moving around your command line using your Vi mode is as simple as it is to move around vi.
Lets use the same example:

  %user>ls etc/apache2 _

You probably forgot the initial “/” on your path, so going back is as simple as hitting ESC then b 3 times, that’s it, you’re at the start of etc.
Say that you now changed your mind, and decided that you want to see the content of your **.shh* folder:

  %user>ls _etc/apache2 

Just hit ESC again to enter command mode, and hit D, it’ll erase the line from there on, and you can now type in the new path. Easy!

That takes care of going back and forward, but what about up and down, how does that translate?
Hitting k and j on your command line, while on command mode, lets you navigate the command history.

bash vi mode indicator.

Command editing

Here is an example of something I found particularily interesting: Say you wrote a long and complex command, something like finding large files and creating a list with it’s names:

%>  find / -type f -size +20000k -exec ls -lh {} \; | awk ‘{ print $9 “:  $5 }’ > filelist.txt _

Once you’re done, you realize that you want to change some things around… , this mode allows you to change it easily, just hit ESC to enter command mode and then press v. TA-DA! You’re inside Vim (or Vi), able to edit your entire command as you wish.
When you’re done just save and quit, the new command will be executed automatically for you. Cool, uh?

To sum things up

This was a basic introduction to what you can do on your command line, as you’ll see on the cheat sheets available for download there are many more things to do. I encorage you to check them out, since the examples provided here are just the tip of the iceberg (trust me!).

I hope this helps you be a bit more efficient when working with it, I know it did for me!

bash command line editing cheat sheet
bash command line editing shortcuts
bash command line substitution
bash command line arguments
set show-mode-in-prompt on
bash vi mode key bindings

Leave a Reply

Your email address will not be published. Required fields are marked *