Linux & vim shortcuts – let’s have some fun

I feel stupid for using VI text editor, why didn’t I think of trying VIM in the first place ?

Yesterday I went to GMIC SV which pretty much sucked but this is a totally different story and I’ll write about it tomorrow.
The only good thing that came out of this conference was an HTML5 Code Lab (speaker was Song Zheng) which demonstrated a few neat tricks about using video for chat/conference using HTML5.

Now, the “not so cool thing” was that half of the session was using webkit flags that are currently supported only by chrome and only if you enable a specific flag by browsing flags.

So after I got my share of joy using VIM I started looking what else I have missed.
That wasn’t difficult to find, a simple google search brought me to this excellent tutorial which I highly recommend!

This reminded me that a few years ago, when I was working for another company, a cool (and unknown) IT dude wrote a bash script that allows you to browse your history commands and choose the one you like and re-run it. All of it was done by one simple shortcut and the arrow keys (for “forward” and “backward”).

The first day I started working for Bluesnap, I banged my head against the wall for not copying this script, I was so used to it…
Since I had no choice I started using:

history | grep "part of the command I was looking for"

but it seemed too much hassle and I kept looking for a better option. Back then we had no IT and it was before the days of stackoverflow, so I searched a bit and found that, in shell mode, when you type

Ctrl+r

it lets you type a string and it’ll find matching results from your commands History.
Since only one command can be displayed at a time, it’ll display only the most recent command that matches the string you typed. If you want to “browse” previous commands just keep hitting

Ctrl+r

– nice and simple!

But there are other tricks to using history as well, say the last time you ran mysql you entered

mysql -u root -p

and now you want to run the same command again. All you have to do is type:

!mysql

and it’ll run the last command that you ran which started with “mysql”. Cute.

But I feel that I kind of ran forward and missed “small things” which I treat as obvious, sorry for that, let’s fix it:

ctrl+a

will jump the cursor to the beginning of the line while

ctrl+e

will “jump” to the end.

Both VI and VIM support Esc+/ which enters “search” mode – this is very convenient when you’re in a long document and you want to “jump” to a specific location without scrolling (ctrl+f and ctrl+b will move the cursor one “page” forward and backwards respectively). If you want to go all the way to the end you can Esc and then shift+G – it’ll bring you to the last line in the file, but even better, if you know the line number you want:

Esc + :[line-number]

will get you there promptly!

Since I got used to type vi – I added the following line to .bashrc:

alias vi='vim'

and suddenly the world became a better place to live in…

Another really useful shortcut is substituting the first occurrence of “param1″ with “param2″ – in the last command:

^param1^param2

it’s extremely useful when you use long commands, for example, say you want to copy file1 to a specific destination and then file2 to the same destination:

scp /usr/home/alfasin/file1 /var/www/html/code

Now, instead of using the “up arrow” and edit the command by moving the cursor like this:

scp /usr/home/alfasin/file2 /var/www/html/

You can simply do:

^file1^file2

Can you guess what the following alias does?

alias fuck='sudo $(history -p \!\!)'

I can’t take credit for it and it’s too bad cause that’s one of my favorites: when you type a command in shell and you get the “insufficient permission” screw-you response – all you have to do is simply type “fuck” and Enter. Yes. That’ll run the previous command preceeding by “sudo”…

If you’re a shortcut-junkie like me, better RTFM.
And if you’re just looking for a few more tools for ‘ya belt, this is a cool read. In case you want to improve your shell skills, a beautiful website called memrise offers a nice course called: Shell-Fu. Enjoy!

Advertisements
Linux & vim shortcuts – let’s have some fun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s