Nothing Special   »   [go: up one dir, main page]

Vi/vim/gvim Editor Commands & Configuration (20150815) : Open File: VI Save/Quit

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Vi/vim/gvim Editor commands & configuration (20150815)

Open File: vi <filename/s>  {vi *.c means open all .c files in the current directory}
Save/Quit: :w
Quit without saving file: :q
Save and quit: :wq
Forcefully Save: :w! {used to save a Read Only File like /etc/shadow}
Forcefully Quit: :q! {it will ignore all changes after last save}
Forcefully Save & Quit: :wq!
Editing Commands:
Insert char/s i
Insert at start of line I
Insert line after o
Insert line before O
Append after cursor a
Append at the EOL A
Delete char x {delete n char nx}
Delete word dw {delete n words ndw}
Delete line dd {delete n lines ndd}
Delete lines n1 to n2 :n1,n2 d
Delete rest of the line D
Change word cw {change n words ncw}
Change entire line cc
Change rest of the line C
Replace char r {replace n chars nr}
Replace till <ESC> R
Swap char postion xp
Undo last edit u
Cursor movement:
Go to line no. n : n
Go top of file :1
Go bottom of file :$
Go end of line $
Go start of line 0 or ^
Go up k or  {Go n lines up nk}
Go down j or  {Go n lines down nj}
Go left h or  {Go to left by n col nh}
Go right l or  {Go to right by n col nl}
Go one screen back ^b or Pg Up
Go one page down ^f or Pg Dn
Go to next file :n  {if you opened more than one file}
Go to Previous file :N  {if you opened more than one file}
Search & Substitution:
Search /<string> (use n for going to next occurrence of <string>)
Substitution :n1,n2 s/X/Y/options  Substitute 'Y' for first occurrence of ‘X'
between lines n1 & n2
Options: g – Global Change c - Confirm each change
Copy, Cut& Paste:
Yank (Copy) yy {yank n lines nyy}
Cut can be of chars nx , words ndw or lines ndd or :n1,n2 d
{After deletion or yank, position the cursor properly and use p to paste the deleted chars, words or lines}
Put p  { Equivalent to Paste}
Search Patterns (Regular Expression)
Beginning of line ^ {Hat}
End of line $
Any character . {Dot/Point}
Zero or more of previous character * {Star}
Matches any character from A to Z [A-Z]
Matches any character from a to z [a-z]
Matches any character from 0 to 9 [0-9]
Matches a, b, or c [abc]
Matches any character BUT a, b or c [^abc]
escape character for literal: \ / $ . ^ [ ' & * | ~ \
General Commands:
Transfer lines :n1,n2 t n3  {transfer lines between n1 & n2 after line n3}
Write to other file :n1,n2 w <filename> {write lines n1 to n2 to file filename}
Append to a file :n1,n2 w>> <file name> {Append from lines n1 to n2 to file filename}
Read from file :r <filename>
Know about file detail ^g
Refresh Screen ^l
Set Options:
Prints all option settings :set all
Enables option 'option' :set option
Disables option 'option' :set nooption
Prints current value of 'option' :set option?
Prints modified options :set
Show Line Numbers :set nu
Hide Line Numbers :set nonu
Set Auto Indent :set ai
Set Ignore Case :set ic {for case insensitive search}
Complex Examples:
Remove first 5 char from all the lines :1,$s/^…..// {There are 5 dots}
Remove last 5 char from all the lines :1,$s/…..$// {There are 5 dots}
Open Multiple Files for comparison or simultaneous reference :vsplit <filename>

User can use .exrc or_vimrc file as a default vi/vim configuration.In Unix/Linux it is called
vi/vim. In Windows it is called vi/gvim. .exrc is always kept in user’s <HOME> area in Linux.
Sample common_vimrc
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set nu
set nobackup
set ts=5
set ai

Sample common .exrc


set nu redraw showmatch beautify autoindent tabstop=5

Extra Info
:scriptnames list all files that Vim loads during start, including .vimrc file.
:echo($MYVIMRC) will give you the location of your .vimrc file.
:e $MYVIMRC will open .vimrc file.

Multiple string search


/^fail.*warn.*down/ find fail AND warn AND down (fail at start of line)
/fail\|warn Search for fail OR warn

You might also like