Tuesday, December 2, 2014

VIM HACKS

VIM'S BACKGROUND

Vim is free, open source powerful, and highly configurable text editor. It was written by Bram Moolenaar and first released publicly on 2 Nov 1991(23 years). Written in C and Vimscript.  It is released for various operating systems: Unix, Linux, Microsoft, OS X, iOS, Android. Vim is designed for use, both from a Command-line interface and as a standalone application in a graphical user interface. The name "Vim" is an acronym for "Vi improved"
because Vim is an extended version of "Vi" editor initially release at 1976 for Unix machine.

During late 90s, Vim took over where vi was lacking behind in the so-called editor war that existed between the vi editor and the Emacs editor. Bram implemented a lot of the missing features that the Emacs community used as arguments for why Emacs was better than vi / Vim.

In this post, we will introduce a list of recipes that will help you personalize and efficiently use Vim. You will find recipes for the following personalization tasks:


1.NAVIGATION

  • <Esc> h(left), j(down), k(up), l(right)
  • <Esc> gg   (go to the top of the file), G (go to the bottom of the file)
  • <Esc> 0     (go to the starting of the line), $ (go to the end of the line)
  • <Esc> H, M, L (go to the top, middle and last line of the current screen )
  • <Esc> N% (go to the N% length of the file)
  • <Esc> NG (go to the Nth line of the file)
  • <Esc> :N  (go to the Nth line of the file)
  • <Esc> e    (go to the end of the next word)
  • <Esc> w   (go to the beginning of the next word)
  • <Esc> b   (go to the beginning of the previous word)
  • <Esc> {   (faster navigation upward the file)
  • <Esc> }   (faster navigation downward the file)
  • <Esc> ctrl+f (move cursor to the bottom of the screen)
  • <Esc> ctrl+b (move cursor to the top of the screen)


2.OPENING, CLOSING, AND EDITING
  • <Esc> a           (append text after the cursor)
  • <Esc> A           (append text at the end of the line)
  • <Esc> i            (insert text before the cursor)
  • <Esc> I            (insert text before the first non-blank in the line)
  • <Esc> o           (insert text in the beginning of the next line of the cursor)
  • <Esc> :w         (write the changes)
  • <Esc> :q          (quit)
  • <Esc> :q!         (quit neglecting the changes)
  • <Esc> :wq       (write the changes and quit)
  • <Esc> :ggVG   (select all the text)
  • <Esc> :y          (copy)
  • <Esc> :d          (cut)
  • <Esc> :p          (paste)
  • <Esc>  u          (undo)
  • <Esc>  .           (repeat last change)
  • <Esc> ctrl + r (redo)
  • <Esc> :sav <filename>     (save as)
  • <Esc> :e   <filename>      (open file)
  • <Esc> :read <filename>   (read file)
  • <Esc> :read ! <any command> eg. :read ! cat example.txt | grep <pattern>
  • vim +N <filename> place the cursor at nth position of the file
  • vim +<pattern> <filename> (place cursor on the line where pattern lies)

3.MISCALLENOUS
  • <Esc> :colorscheme <tab> (editor themeing)
  • <Esc> :set number or nu  (shows the line numbering)
  • <Esc> :set nonu                (removes the line numbering)
  • <Esc> :set rnu                  (shows the relative line numbering)
  • <Esc> :set nornu              (removes the relative line numbering)
  • <Esc> :set readonly          (sets the file in read only mode)
  • <Esc> :set spell                 (activates spelling check)
  • <Esc> :set spelling=<language code>; set spelling=en where en=English
  • <Esc> :syntax on              (activates the syntax highlighting)
  • <Esc> :syntax off:             (deactivates the syntax highlighting)
  • <Esc> :set syn=<name>   eg. set syn=java or set syntax=java
  • <Esc> ctrl shift +            (zoom in)
  • <Esc> ctrl -                     (zoom out)
  • <Esc> :sh                        (open temporary shell)
  • <Esc> :set ls=1               (show the status line)
  • <Esc> :set hlsearch        (highlight the searched item)
  • <Esc> :set autoindent    (sets the auto indention while editing the file)
  • <Esc> :set smartindent  (set the smart mode in the auto indention of the file)
  • <Esc> :vsp                      (split the screen vertically)
  • <Esc> :sp                        (split the screen horizontally)
  • <Esc> :qa                        (quit all the screen)
  • <Esc> :iabbr teh the       ("teh" will be replaced by "the")
  • <Esc> :iabbr forx for(int i=0;i<n;i++){}  ( for(int i=0;i<n;i++){} will replace forx)
  • <Esc> :%s/<old string>/<new string>/g  (replace old string with new string   without confirmation)
  • <Esc> :%s/<old string>/<new string>/gc (replace old string with new string with confirmation)




NOTE: <Esc> is not needed to be pressed if you are already in normal mode.

NOTE: All these hacks are temporary. If you want them to be permanent follow following steps:

#  create .vimrc file in your home directory

  •   cd
  •   vim .vimrc

#  sample .vimrc












#  this is all you have to do. Vim will automatically load the changes.







To get offline information go to vim and type
  • <Esc> :help
  • <Esc> :help syntax.txt
  • <Esc> :help map.txt
  • <Esc> :help options.txt


other links

No comments:

Post a Comment