VIM EDITOR

vim is not a simple text editor..
Vim itself is a very powerful tool and has lot of options like a programming language..

  • Press  A  to append text.
  • Type  dw  to delete a word.
  • Type  d$   to delete to the end of the line.
  • CTRL-R (keeping CTRL key pressed while hitting R) a few times  to redo the commands (undo the undo's).
  • Type CTRL-g to show your location in the file and the file status.. Type  G  to move to a line in the file.
  • Press  G  to move you to the bottom of the file.
  • Type  gg  to move you to the start of the file.
  • Type  %  to find a matching ),], or } .
  • Type  :s/old/new/g  to substitute 'new' for 'old' in the line
  • Type   :%s/old/new/g      to change every occurrence in the whole file.
  • Type  :!        followed by an external command to execute that command.
  • Type  a  to insert text AFTER the cursor.
  • Type a capital  R  to replace more than one character.
  • yw  yanks one word.
  • use the  y  operator to copy text and  p  to paste it  ..use it along with visual mode (v)
  • Type  o  to open a line BELOW the cursor and start Insert mode.    Type  O  to open a line ABOVE the cursor.
  • Type  a  to insert text AFTER the cursor.  Type  A  to insert text after the end of the line.
  • The  e  command moves to the end of a word.
  • The  y  operator yanks (copies) text,  p  puts (pastes) it.
  • Typing a capital  R  enters Replace mode until  <ESC>  is pressed.
  • Typing ":set xxx" sets the option "xxx".  Some options are:
  • 'ic' 'ignorecase'       ignore upper/lower case when searching
  • 'is' 'incsearch'        show partial matches for a search phrase
  • 'hls' 'hlsearch'        highlight all matching phrases
  •  You can either use the long or the short option name.
  • Prep-end "no" to switch an option off:   :set noic
  •  Vim has a comprehensive on-line help system.  To get started, try one of  these three:
    •                        - press the <HELP> key (if you have one)
    •                        - press the <F1> key (if you have one)
    •                        - type   :help <ENTER>
  •  You can find help on just about any subject, by giving an argument to the  ":help" command.
  •  Try these (don't forget pressing <ENTER>):
    •                        :help w
    •                        :help c_CTRL-D
    •                        :help insert-index
    •                        :help user-manual
    •                        :help vimrc-intro
  • There is a variable in VIM runtime..
  • $VIMRUNTIME/  ==> we can refer to this if want to know any vim  versions etc..
  • Command line completion with CTRL-D and <TAB>
  • Type vimtutor in UNIX to bring  up the full vim documentation:
  •                            unix> vimtutor
##=====================================================##
How do I switch between panes in split mode in Vim/GVIM ??
##=====================================================##
In command mode, hit Ctrl-W and then a direction, or just Ctrl-W again to switch between panes.
  • :b <number>  will open the specified buffer in the current pane.
Example  : UNIX > vim test_1 test_2 ; #  This command opens two test_1 and test_2 files
But in vim we can see only one file(by default the first file test_1) with this approach.
If want to go to test_2 file then use :b 2

:ls will show your open buffers
If we opened multiple vim files like above and want to know which are in buffer use this command
and with the help of :b <number> we can switch bw files

  • If we open many windows in vim/gvim  and you want to have equal window size for all split pans
Esc mode  Ctrl+W =
For more info type:
:help window-resize

  • you can use <ctrl> + w + w To switch the panes in order.
A suggest way is to put this codes in your vimrc file
===========================
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l
===========================
That means you could use <ctrl>+j/k/h/l to switch the right direction just like you use the j/k/h/l to move the cursor
map is a vim command that can be used for the key mapping or aliases etc..
http://vim-adventures.com/   -->    vim while playing
:help command                     -->    man page help
http://github.com/                -->    you can find vim stuff
##=====================================================##
How to enable the back space in vi editor Esc(command) mode ??
##=====================================================##
  • To go to a specific column in gvim from the current cursor position use   coloumn_number |
  • Example      3 |  will take you to the 3rd coulmn
  • Let 's say we coded a program using control statements in a file ..
but the file indent (the for loop start and end ) etc.. are not in a proper order, then it is difficult to read the content
  • Use  gg=G
  • This will arrange the file contents in a nice readable format
  • Ctrl - C can be used to stop any running commands(if something got stuck while doing any operation)
  • To use backspace in vim
  • set backspace =start,eol,indent
  • set term=ansi
  • Space to new line in vim :
                               :%s/ /\r/g
  • New line to space
                               :%s/\n/ /g
##=====================================================##
Removing Blank Lines
Here's a handy one-liner to remove blank lines from a file.
           :g/^$/d
Breaking this down. The "g" will execute a command on lines that match a regular expression.
The regular expression matches blank lines, and the command is "d" (delete).
You can also use
           :v/./d
(v means complementary, so any line that don’t have a single character at least will be deleted)
##=====================================================##
Some examples on :s option:
:%s/a/b/gi       To replace all a s by b s globally and car insensitive
:s/                      To do in the current line or first occurrence
:%s/                   Same as above but for all occurrences in while file
/\<the               To search all strings that are starting with the
/the\>               To search all the strings that are ending with the
/\<the\>           To search a string the
:3,$s/hi/bye/g    To replace hi with bye from 3rd LINE to end of file(EOF)
:%s/\r//g           To delete dos carriage returns,(^M)
:%s/\r/\r/g        To replace dos carriage returns with returns
:%s/ *$//g          To delete white spaces
:1,10 w >> output_filename  This appends the lines from 1 to to to the file called output_filename
:r infile                                   This it's to insert the text of the file called infile in current file where the cursor is on
:23r infile                               Same as above but we'll insert@23 rd LINE in the currently opened file
##=====================================================##
Vu                        if you want to convert the whole line of text under the  cursor to Lowercase line
VU                       if you want to convert the whole line of text under the  cursor to Uppercase line
g~~                     whatever is the case of the text under the cursor line and you want to do Invert case
##=====================================================##
How can I use tabs??
:tabnew file_name            Opens file_name in a new tab  of current vim editor
 gt                                    Go to next tab
:tabfirst                           Go to first tab
:tablast                            Go to last tab
:tabm n                           Arrange tabs . Tab movement from current position to N (0 is first ,1 is second …. Position from the starting)
:tab ball                         Puts all open files in tabs
:tabdo %s/a/b/g              Execute a command in all tabs
##=====================================================##
AUTO COMPLTETION ? (auto completion of words in insert mode)
Ctrl+p   to insert the previous matching word
Ctrl+n  to insert the next matching word
Ctrl +x  followed by Ctrl+l          to complete a whole line form the buffer
(basically ctrl + x pressing will open a sub mode from there on we can do lot of stuff..
for example ctrl + l in submode gives you to complete the whole line )
##=====================================================##
Want to make use dictionary of words while typing in VIM ?
So first we need to let the  vim to know where is locally installed dictionary file.
Use below command (make it part of .vimrc for better usage)
:set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words
Then while typing a word , type first few letters, then press CTRL + X (to go to sub command mode) followed by CTRL + K (to look up in dictionary)
"It can be cumbersome to type CTRL-X CTRL-K for many different completions. So, Vim gives us a shortcut.
While in insert mode CTRL-N and CTRL-P will pull completion results from multiple sources.
This set is defined by the 'complete' option and by default dictionary completion is not enabled.
Add 'k' to the list to enable dictionary completion source:
:set complete+=k
Now, while in insert mode we can type the following to complete use : CRTL + N
One of the YouTube link with useful vim info:
##=====================================================##
Ever wondered how to see the line count from the current line ? [like relative to the line under cursor ?
:se relativenumber
Undo, Redo :
u            ==> UNDO
Ctrl + r   ==> REDO
##=====================================================##
Verbs in VIM :-)
d  ==> Delete
c  ==> Change (delete and enter insert mode)
> ==> Indent
v  ==> Visually Select
y  ==> Yank (Copy)

Nouns in Vim :-)
w ==> Word (forwared by a word)
b  ==> Back (Back by a word)
2j   ==> down 2 lines
iw   ==>  "Inner wor" (works from anywhere in a word)
it      ==>    Inner Tag (the contents of an HTML tag)
i"      ==>   Inner Quotes
ip     ==>   Inner Paragraph
as     ==>   a sentence
##=====================================================##
Playing with tabs in gvim/vim:
 

##=====================================================##
Quick info:
Shift + g  ==> go to the end of the file [eof)
:=            ==> Displays number of lines in the file
CTRL+ G   ==> Shows "file name", "number of lines in the file" and % file at which we are currently
/\u           ==> To find any upper case
/\l            ==> To find any lower case letters
/\d           ==> To match any digit search
/\D           ==> To match any non-digit search (letters (either case) + special characters)
:%s/"TEST\(.*\)","//gc ==> To replace any strings like "TEST/adafasdfas " to null
:edit foo.txt    --Vim will close the current file and open the new one.
CTRL+pg up and CTRL+pg dn to switch between tabs(different files) in gvim.
:E - displays all vim files in CWD. Select, double click will open the file
GVIM:tabnew <filename..path can also be included> --opens up a new tab in GVIM
:sp x 
//this will open the file -- horizontal split
:vs x / :vsp x
//this will open the file --vertical spilt
Type some letter and press ctrl + P to see the words start with that in that file
Press SHIFT 9 to go from middle of module to start of module in a Verilog file
Press SHIFT 0 to go from middle of module to end  of module in a Verilog file
:tabnew newfile_name
//this it open a new file in a new tab of gvim !!
:%s/LINT-32)\n/LINT-32) /gc
//this will replace LINT-32) and a new line with LINT-32) and space !!!
##=====================================================##
  • TO COUNT  the number of occurrences of the pattern in the file
    • :%s/pattern//gn
  • If we want to know that : in how many lines the pattern has occurred(but not the total number of occurrences) then
    • :%s/pattern//n
  • Do “gf” on the file link in gvim to go forward to the corresponding file.
  • Do “:bd” on the new file to come back to the previous where we were.
  • Instead of 2 nd step,we can also do “:e# ”
  • Do “:se nowrap” will display the contents clearly without wrapping
  •  :set nu ---> set line numbers
  •  :set hls --> highlights the selected
  •  shift * to highlight the word under cursor
  •  :set ic --> ignores the case (used for searching a string in the vi editor)
  •  :set noic --> to unset the :set ic  condition
  • Visually block and the operations
    • Ctrl +v  and shift + g to select all lines
    • Shift + I to go to start line and insert mode
    • Enter the required pattern
    • Press Esc
      • This will paste the required pattern ion all lines beginning
  • Delete all lines containing a pattern
    • Say if we want to delete all lines  which contain a particular string
      • :g/pattern/d
    • If we remove /d form the previous command , we can see the lines which are targeted to be deleted
      • :g/pattern
    • To delete the white space characters or any empty lines
    • :g/^\s*$/d
    • o delete all lines that are not commented in the vim editor( opposite to above command behavior)
    • :g!/^\s*$/d
    • g! is equal to v
    • :v/^\s*$/d        and     :g!/^\s*$/d are  same
    • To delete all the lines except which contain error or warning or fail in those lines
    • :v/error\|warning\|fail/d
##=====================================================##

No comments:

Post a Comment