How to edit a text file from command line in linux ?


In Unix/*nix vi is the standard and widely used commands to edit/create a plain text file from the command line. vim (Vi IMproved ) is another text editor based on vi which can be run from a Command Line Interface as well as a Graphical User Interface. 

The syntax for vi or vim is
vi [options] filename
vim [options] filename
vi file.txt
vi can be used with a number of options. Refer the man pages for all the options. In this post we are mainly focusing on the features of vi. vi has 2 modes
  • Command Mode - in this mode everything that you type is interpreted as a command. Command mode will be the default mode when you start vi
  • Insert mode - in this mode everything that you type become part of the file.  To enter into insert mode press 'i' in the keyboard. To return to command mode press ESC key
Some important commands used in command mode are listed below

Cursor Movement Commands

Keystrokes Action
h / Left Arrow Key Move cursor Left
j / Down arrow Key Move cursor Down
k / Up Arrow Key Move cursor Up
l / Right Arrow Key Move cursor Right
spacebar Move cursor right one space
Enter Key Move cursor down to the beginning of next line
-/+ Move cursor down/up in first column
ctrl-f / Page Down Key
n ctrl-f / n Page Down Key
Scroll forward one screen
Scroll forward "n" screen
Ctrl-b / Page Up Key
n ctrl-b /n Page Up Key
Scroll back one screen
Scroll back "n" screen
G Move cursor to the end of file
gg Move cursor to the beginning of file
M Move cursor to middle of page
H Move cursor to top of page
L Move cursor to bottom of page
W
w
nw
Move cursor a word at a time (white space delimited)
Move cursor a word at a time (first non-alphanumeric)
Move cursor ahead n words
B
b
nb
Move cursor back a word at a time (white space delimited)
Move cursor back a word at a time (first non-alphanumeric)
Move cursor back n words
0 (zero) / Home Key Move cursor to beginning of line
$ Move cursor to end of line
} Move cursor to beginning of next paragraph
{ Move cursor to beginning of current paragraph
'. Move cursor to previously modified line.
m <identifier> Mark the line with an identifier. For example ma will mark the line with an identifier 'a'
'a Move cursor to line marked as 'a'
:n Move Cursor to line number n

Edit commands

Keystrokes Action
i Starts insert mode
I Starts insert mode and cursor will be at the beginning of current line
a Starts insert mode and append data after cursor
A Starts insert mode and append data at end of line.
o Starts insert mode and open a new line below the current cursor position.
O Starts insert mode and open a new line above the current line.
dd
ndd
Delete line
Delete n number of lines
D Delete contents of line after cursor
C Delete contents of line after cursor and insert new text.
dw
ndw
Delete word
Delete n words
x Delete character at cursor
X Delete character before cursor
Y / yy Yank or copy current line
p Paste after current line.
P Paste before current line.
r Replace character
R Overwrite characters from cursor onward
s Substitute one character under cursor continue to insert
S Substitute entire line and begin to insert at beginning of line
J Join current and following line into one line
~ Change case of individual character
ctrl-a
ctrl-x
Increment number under the cursor.
Decrement number under the cursor.
u Undo last change
U Undo all changes to entire line



Search Commands


Keystrokes Action
/search_string Search and highlight the search_string
?search_string Search backwards and highlights the  search_string
n Find next occurrence of search_string
N Find previous occurrence of search_string
:nohl Remove highlights

Save and Quit Commands  


Keystrokes Action
:w Write changes to the file
:q Quit
:wq Write changes and quit vi
:q! Quit without saving changes
:w! Save changes overriding file permission, provided user has privilege to do so
:qa Quit all open files
:ZZ Save changes to current file and quit

 

Comments

  1. daw - delete a word is a better option than dw,dnw,when we stand on the middle of a large word.

    ReplyDelete

Post a Comment

Popular posts from this blog

Understanding awk command with examples

Understanding sed command with example -Part 1

How to install a Software in Linux