More useful vim tips

Go to place in file

:G    End of file

1G  Start of file

<n>G    (or) :n Go the nth line of file

Find and Replace

This is of the form %s/<text to find>/<text to replace>/<options>

%s/foo/bar/g : Replaces all occurrences of  foo with bar

%s/\<foo\>/bar/g : Replaces all EXACT matches of the word foo  with bar

%s/foo/bar/gc : The c option, asks for a confirmation before replacing

Editing multiple files in Vim

Splits and multi-file editing

Perhaps one of the most limiting features of the original vi is its inability to edit more than one file at once. vim removes this limitation; it can edit many files at the same time. In typical vim fashion, it provides many ways to do this. I’ll focus on the easiest way to visualize, which uses “splits,” which are like independent windows that can each contain a text file.

Opening a file in a split

Assuming you are already editing a file in a vim session and want to split the screen and edit a second file, you can use either of these commands:

:sp filename
Splits the screen horizontally and opens filename in the new area.
:vsp filename
Splits the screen vertically and opens filename in the new area.

For most purposes, horizontal splits are easier to manage. Any split can be further split — you can edit as many files in a single instance of vim as you like.

Managing splits

Once you are editing multiple files, you will probably need to move back and forth through your files and manage the size of the splits so that you can see the relevant portions of the files you’re interested in.

The prefix for all the below commands is <C-W> — that is, hold down Ctrl and type w. Press <C-W> before typing any of the below commands.

<C-W>
Moves to the next split.
k
Moves to the split above the current split.
Up
Moves to the split above the current split.
j
Moves to the split below the current split.
Down
Moves to the split below the current split.
+
Increases the size of the current split by one line. (Try combining this with counts — e.g. 5<C-W>+ will make the split five lines bigger.)
(Minus sign) Decreases the size of the current split by one line.
_
(Underscore) Maximize the current split (that is, make it take up as much room as possible.)

Saving and quitting

Each split acts something like an individual instance of vim — so :w and :q work on the current split. If you want those commands to apply to all the splits instead of just the current one, add a to them — for instance, :wa saves all the open files, :qa quits all open files (and exits vim), and :wqa saves and then closes all open files.

Recommended mappings

I recommend adding the below files to your .vimrc file to make working with splits easier:

map <C-J> <C-W>j<C-W>_ map <C-K> <C-W>k<C-W>_ set wmh = 0

The first two lines allow you to switch between splits much more smoothly — just press <C-J> to open and maximize the split below the current one and <C-K> to open and maximize the split above the current one. I chose these mappings because they correspond to vi’s default up and down keys, you might want to use different key combinations if you’ve ever used an editor that had hotkeys for moving from one open file to another.

The last line allows splits to reduce their size to a single line (which includes the filename and position); this saves a lot of space when you have many splits open. By default, vim forces splits to include an additional line that contains the line of text the cursor was on in that file.

Vim : Things you thought you could never do Part I

When i first started out programming using Vim, my first reaction was “What a boring editor, ancient piece of crap” . So i started using gedit regularly. It was only recently that i was enlightened about the Awesome features of Vim!! So i thought i need to enlighten some of the vim critics out there.
*

Vim Modes

There are 2 main modes in Vim
a. Normal mode: The one when you open it initially in Vim
b. Editing Mode: When you press one of the following keys when in normal mode
i   –  insert at current position
a  –  insert AFTER current position
o  – open (create) a new line below current line
I  – insert AT START of current line
A – insert AFTER end of current line
O – open (create) a new line ABOVE current line
Here, as you can see they key ‘o‘ would turn out to be pretty useful.
Note: The following tips are useful in normal mode. The quotes are given only for understanding that they are strings. You dont need them while entering them in normal mode.

*

Navigation

To make navigation easier i.e to make use of the main keys on the keypad h , j , k , l are used. To remember better
a. h is on far left -> so stands for left
b. l is on far right -> so stands for right
c. Since j looks like it is somewhat pointing down -> stands for down
d. Obv -> k stands for up

*

Use of combos to achieve magic !

d- delete
c- change
w- word
b- word before
a. So type “wwwwww” (and) “bbbbbbbbbb” and see what happens. Cool right!
b. Similarly place the cursor before a word and type “dw” -> a word is deleted!
c. So now, you can make combo’s of the above letters to make super things happen
i. d6w – delete 6 words after cursor posn
ii. d6j – delete 6 lines down
iii. d4b – delete 4 words before cursor posn
iv.  cw – change current word
etc. etc…. All this just in a few keystrokes !!

*

Auto-complete Feature

Lets say i am typing a document/program and i repeat the use of many words/keywords. The auto-complete feature shows me a list of complete options allowing me to fill in the word of my choice
Ctrl – P – Search previous
Ctrl – N – Search Next
Once the a drop-down of possible options(for multiple fills) is shown, go down to the appropriate one and press <enter >
Try it out!

*

Visual Mode

Now, we are always faced with the stigma as to
“How do i select multiple lines without a mouse? How do i copy/paste/delete multiple lines”.
Vim has the solution in the form of the “Visual Mode”
*
1. Type “v” – allows you to select multiple characters
2. Type “V”(Shift-v) : allows you to select multiple lines
*
Once youve selected the lines, you can  use
*
a. “d” – to delete
b. “y” – copy (short for yank)
c. “p” – paste (once you have copied)
d. Use “<“ and “>” to indent left and right
e.  “o” moves to start and of select
*
Other useful shortcuts
“dd”-delete current line
“d2d” – delete 2 lines
“yy” – yank current line
LOOK OUT FOR PART 2 !! Coming up soon!

Hack to share files in Vbox : OpenSolaris

Usually files can be easily shared between the host system and the guest system in VirtualBox by using the easy and simple to configure ” Guest Additions” feature. The problem lies wherein the “Guest Additions” feature is not compatible in Solaris making it a headache to share files.I currently host Ubuntu with OpenSolaris as the guest OS.

So a simple but roundabout hack is to

1. Create an iso image in the host OS with the files you want to share

Screenshot-2

2. Mount the iso image in VBOX through the Devices -> Mount CD/DVD ROM-> Stuff2Share.iso

Screenshot-1

3.  Run/Open the Guest OS [OpenSolaris] and go to Places -> Stuff2Share.iso {auto mounted }

Screenshot

4. Happy Sharing !!

Downloading entire websites using Wget

Wget is a good tool for downloading resources from the internet. The basic usage is wget url:

wget http://linuxreviews.org/

The power of wget is that you may download sites recursive, meaning you also get all pages (and images and other data) linked on the front page:

wget -r http://linuxreviews.org/

But many sites do not want you to download their entire site. To prevent this, they check how browsers identify. Many sites refuses you to connect or sends a blank page if they detect you are not using a web-browser. You might get a message like:

Sorry, but the download manager you are using to view this site is not supported.
We do not support use of such download managers as flashget, go!zilla, or getright

Wget has a very useful -U option for such websites. Use -U [your-browser] to tell the site you are using some commonly accepted browser:

wget -r -p -U Mozilla http://www.thesiteyouwannadnld.com/restricedplace.html

The most important command line options are --limit-rate= and --wait=. You should add –wait=20 to pause 20 seconds between retrievals, this makes sure you are not manually added to a blacklist. –limit-rate defaults to bytes, add K to set KB/s. Example:

wget --wait=20 --limit-rate=20K -r -p -U Mozilla http://www.stupidsite.com/restricedplace.html

A web-site owner will probably get upset if you attempt to download his entire site using a simple wget http://foo.bar command. However, the web-site owner will not even notice you if you limit the download transfer rate and pause between fetching files.

Use –no-parent

--no-parent is a very handy option that guarantees wget will not download anything from the folders beneath the folder you want to acquire. Use this to make sure wget does not fetch more than it needs to if just just want to download the files in a folder.

Playing rmvb files in Linux

If you want to play rmvb files in GNU/Linux then follow the steps given below

If you do not have mplayer install it by

sudo apt-get install mplayer

1.Download the binary codecs from the mplayer site based on your platform

http://www.mplayerhq.hu/design7/dload.html

2.Extract the tar file (essential-20071007.tar.bz2)

3.Copy the contents of the folder essential-20071007.tar.bz2 to /usr/lib/codecs

4.Open Mplayer -> Preferences -> Codecs -> and set

Video Codec : Real Video Decoder

Audio Codec: FFmpeg/libavcodec audio decoders

NOTE: One requirement for this is to install libstdc++5

Installing Linux Distros without boot DVD/CD

Tired of waiting to burn the latest distros released onto DVD’s/CD’s to try them out?

Wanna reduce the stack of CD’s?

Here is a solution to directly install a distro by directly booting the image files from the harddisk of a GNU/Linux operating system. The steps to be followed are prescribed below(Eg. Fedora 10 from Ubuntu 8.10)

1. Mount the iso image of the distro to a mount point

$ mkdir /media/fedora10

$sudo mount -o loop /home/varrun/Fedora10.iso   /media/fedora10

2. Copy the contents of the iso responsible for booting the distro to a folder in the root directory

  • The kernel -> is present in the isolinux/vmlinuz folder
  • initrd image -> responsible for loading the initial root file system in the RAM

3. Make a directory “Fedora” in root directory ” / “

$ sudo mkdir /fedora

4. Copy the isolinux and images folder in the iso to /fedora directory

$ cd /media/fedora10

$ sudo cp -rf isolinux    /fedora

$ sudo cp -rf images      /fedora

5.Copy the Fedora10.iso to /fedora

$ sudo cp /home/varrun/Fedora10.iso /fedora

6.The final step is to configure the bootloader to load the files

$ sudo gedit /boot/grub/menu.lst

ADD THE TEXT

title Fedora 10 DVD(from hard disk)
root (hd0,3)
kernel /fedora/isolinux/vmlinuz ro liveimg root=/dev/sda4 rhgb quiet
initrd /fedora/isolinux/initrd.img

Now reboot the computer and you will see a new option “ Fedora 10 DVD(from hard disk)

Enter -> Install -> Enjoy!!