Thursday, 22 November 2012

VImroved 101

Vim is an advanced text editor that provides the power of the de-facto Unix editor 'Vi' with a more complete feature set.

The console version of vim comes preinstalled with ubuntu, a cut-down version called "vim-tiny".

STEP 1 :  install the package vim-gnome, for a GUI based vim

prayag@prayag:~$ sudo apt-get install vim-gnome ## on linux ubuntu
[...]
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place


STEP 2 : install vim, with advanced functions

prayag@prayag:~$ sudo apt-get install vim ## on ubuntu
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libqtwebkit4 skype-bin
[...]
Unpacking vim (from .../vim_2%3a7.3.429-2ubuntu2.1_i386.deb) ...
Setting up vim (2:7.3.429-2ubuntu2.1) ...

STEP 3 : edit file with line numbers
$ vi grails-app/domain/eccount/Staff.groovy
add [ESC] :set nu at the end to enable line numbers or add set number to ~/.vimrc to enable line numbers every time.

u                   -  undo last change
Ctrl + r -  redo last change
Shift + w      -  Jump forward word
b                   -  Jump backward word


:w     -  write to file OR
:w !sudo tee % > /dev/null


STEP 4 : goto exact line number
add [ESC] :29 at the end.



STEP 5 : Open file in a new tab from a working tab
:tabe <filename>




Switch to previous tab
:tabp


Switch to nth tab
:tabn TAB_INDEX

STEP 6 : Scroll up/down
Scroll up : SHIFT + Page Up
Scroll Down : SHIFT + Page Down


STEP 7 : text selecting and copying

v - to enter visual mode
x  - to cut
y  - to yank
[ + p - before cursor
p  - to paste
] + p - after cursor

(Reference  : copy lines using visual mode in vim)

copy to clipboard, (first enable +clipboard, make sure it is enabled using vim --version)
In MacOS I had to enable following settings in vimrc
    set clipboard=unnamed


$ vim --version | grep clip
-clipboard       +job             +path_extra      -toolbar
+ex_extra        -mouse_gpm       -sun_workshop    -xterm_clipboard
:%y+ ##+ is a register


STEP 8 : running shell command from inside VI editor

:!ls

STEP 9 : Indentation

Go to the start of the text,
 - press [v] for visual mode.
 - use [up]/[down] arrow to highlight text.
 - press [=] to indent all the lines highlighted.


STEP 10 : folding
Syntax folding

##add to ~/.vimrc

set foldmethod=syntax
set foldlevelstart=1

let javaScript_fold=1         " JavaScript
let perl_fold=1               " Perl
let php_folding=1             " PHP
let r_syntax_folding=1        " R
let ruby_fold=1               " Ruby
let sh_fold_enabled=1         " sh
let vimsyn_folding='af'       " Vim script
let xml_syntax_folding=1      " XML

STEP 10 replace chars/words
:s/node2/yarn-node  # only one occurrence
:%s/node2/yarn-node/g  # all occurrences, % = all followed by pattern



use \ to escape special characters.

## replace \" with " in json data

:%s/\\"/"/g





References

No comments:

Post a Comment