Home > Linux, MAC, Technical > VI/VIM tips

VI/VIM tips


Following are some random tips which I found to be useful. All of them has been collected from different sites and I am just putting them together for my reference.

Most of the examples mentioned here uses the below methodology

if there is a column prefix “:”  then that indicate its the vi command mode. To do this, open the file, press “ESC” key and type “:”

If you want these changes to be permanent, then you can put these commands in a special file called “.vimrc” which is present in your home directory(/users/<username> in Linux & Mac). Create this file, if it doesnt exist.

Indentation Tips

  1.  Enabling/Disabling auto indent
    :set ai ==> To enable
    :set noai ==> to disable
  2. Disabling auto indentation while pasting a code
    :set paste        ==>To disable autoindenting while pasting
    :set nopaste   ==>To enable it back
  3. How to indent a line which you are newly pasting to the existing code block’s indentation
    use ]p instead of just p. This aligns the pasted block with the surrounding text.
  4. Code block indentation tips
    These commands will fix the indents:

    • =i{ reindents “inner block” (inside the braces).
    • =a{ reindents “a block” (including the braces).
    • =2a{ reindents 2 blocks (this block and containing block).

    Instead of “{“, you can use “}” or “B”, for example, =aB indents a block.

    These commands will decrease or increase indents:

    • >i{ increase indent for inner block.
    • . repeat last change (increase indent of block again).
    • <i{ decrease indent for inner block.

    With the cursor on { or }:

    • =% indents the block (including matching brace).
    • >% or <% indents or unindents the block.

Commenting/uncommenting multiple lines in a file
Open the file in VI and in command mode, press “ctrl+v”, now it will go in to visual mode, now move the cursor using the movement keys (arrow keys up/down), move the up/down arrow till you reach the last line you want to comment. Now press “Shift+i” and now enter the character used for commenting “#”

In Nutshell
Commenting: CTRL+v ==> SHIFT+i ==> # ==> Will insert # on the selected column
Uncommenting: CTRL+v ==> x ==> Will delete the #’s in the selected column

Advertisements
Categories: Linux, MAC, Technical
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: