
Creating / Opening a file
vi filename
Edit modes: These keys enter editing modes and type in the text area of your document.
(Before executing following command press ESC Key. Example ESC + i = Insert before current cursor position)
i |
Insert before current cursor position |
I |
Insert at beginning of current line |
a |
Insert (append) after current cursor position |
A |
Append to end of line |
r |
Replace 1 character |
R |
Replace mode. <ESC> Terminate insertion or overwrite mode |
To search any word within vi mode: (n - Next word, N= Previous word)
/any word
Deletion of text
(Before executing following command press ESC Key)
x |
Delete single character |
dd |
Delete current line and put in buffer |
ndd |
Delete n lines (n is a number) and put them in buffer |
J |
Attaches the next line to the end of the current line (deletes carriage return). |
Change, Delete & Yank
(Before executing following command press ESC Key)
Change |
Delete(Cut) |
Yank (copy) | |
Line |
cc |
Dd |
yy |
Letter |
c1 |
d1 |
y1 |
Word |
cw |
Dw |
yw |
Sentence ahead |
c) |
d) |
y) |
Sentence behind |
c( |
d( |
y( |
Paragraph above |
c{ |
d{ |
y{ |
Paragraph below |
c} |
d} |
y} |
Oops
(Before executing following command press ESC Key)
u |
Undo Last action |
U |
Undo all action in the current line |
Cut and paste
(Before executing following command press ESC Key)
yy |
Yank current line into buffer |
nyy |
Yank n lines into buffer |
p |
Put the contents of the buffer after the current line |
P |
Put the contents of the buffer before the current line |
cursor positioning
(Before executing following command press ESC Key)
^d |
Page Down |
^u |
Page Up |
:n |
Position cursor at line n |
:$ |
Position cursor at the end of file |
^g |
Display current line number |
h,j,k,l |
Left, Down, Up, and Right respectivly. Your arrow keys should also work if your keyboard mappings are anywhere near sane. |
string substitution
:n1,n2:s/string1/string2/[g]
Substitute string2 for string1 on
lines n1 to n2. If g is included (meaning global), all instances of
string1 on each line are substituted. If g is not included, only the
first instance per matching line is substituted.
^ matches start of line
. matches any single character
$ matches end of line
These and other "special characters" (like the forward slash) can be "escaped" with \ i.e to match the string "/usr/STRIM100/SOFT" say "\/usr\/STRIM100\/SOFT"
Examples:
:1,$:s/dog/cat/g
(Substitute 'cat' for 'dog', every instance for the entire file - lines 1 to $ (end of file)
:23,25:/frog/bird/
Substitute 'bird' for 'frog' on
lines 23 through 25. Only the first instance on each line is
substituted.
Saving and quitting and other "ex" commands
These commands are all prefixed by pressing colon (:) and then entered in the lower left corner of the window. They are called "ex" commands because they are commands of the ex text editor - the precursor line editor to the screen editor vi. You cannot enter an "ex" command when you are in an edit mode (typing text onto the screen)
Press <ESC> to exit from an editing mode.
(Before executing following command press ESC Key)
:w |
Write the current file |
:w new.file |
Write the file to the name 'new.file' |
:w! existing.file |
Overwrite an existing file with the file currently being edited |
:wq |
Write the file and quit |
:q |
Quit |
:q! |
Quit with no changes |
:e filename |
Open the file 'filename' for editing |
:set number |
Turns on line numbering |
:set nonumber |
Turns off line numbering |
vim +5 filename
(cursor goes to the 5th line of that file in vim editor)
vim +/wali filename
(cursor will be placed on the first occurrence of wali)
vim + filename
(start from the bottom of page)
Note:
vim editor is normally by default installed but if in case vim editor is not installed, and if you are running vim to create/edit files, it shows following message (showing here in bold letter).
[root@localhost ~]# vim anyfile
-bash: /usr/bin/vim: No such file or directory
Solution: To install vim editor, simply run following command.
yum -y install vim-enhanced
Command Output:
[root@localhost ~]# yum -y install vim-enhanced
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.i686 2:7.2.411-1.6.el6 will be installed
--> Processing Dependency: vim-common = 2:7.2.411-1.6.el6 for package: 2:vim-enhanced-7.2.411-1.6.el6.i686
--> Running transaction check
---> Package vim-common.i686 2:7.2.411-1.6.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced i686 2:7.2.411-1.6.el6 media 839 k
Installing for dependencies:
vim-common i686 2:7.2.411-1.6.el6 media 6.0 M
Transaction Summary
================================================================================
Install 2 Package(s)
Total download size: 6.8 M
Installed size: 19 M
Downloading Packages:
--------------------------------------------------------------------------------
Total 57 MB/s | 6.8 MB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : 2:vim-common-7.2.411-1.6.el6.i686 1/2
Installing : 2:vim-enhanced-7.2.411-1.6.el6.i686 2/2
Installed:
vim-enhanced.i686 2:7.2.411-1.6.el6
Dependency Installed:
vim-common.i686 2:7.2.411-1.6.el6
Complete!
[root@localhost ~]#
Note: Apart from vi editor, there are also few other editors (emacs, gedit, gvim, evim, nano, ed) available in Linux. Use following command to create/modify files:
For Example: To edit a file using emacs editor, run following command. (Not all systems will have emacs)
emacs filename
In the same way you can use following editors:gedit file-name
gvim file-name
evim file-name
nano file-name
ed file-name (then type visual to see the matter)





