Chapter3 Creating and Editing Text Files With Vim
Chapter3 Creating and Editing Text Files With Vim
================================================
Saving files (in Ex mode):
:wq Save and quit the current file.
:x Save the current file if there are unsaved changes, then quit.
:w Save the current file and remain in editor.
:w <filename> Save the current file under a different file name.
:q Quit the current file (only if there are no un saved changes).
:q! Quit the current file, ignoring any un saved changes.
ZZ In command mode, saves the current file if there are unsaved changes, then
quit.
================================================
Getting help:
[root@master ~]# vimtutor
Or)
:help
:help save
:q
================================================
Replacing or deleting text:
cw... Replaces one word.
3cw... Replaces three words.
c$ OR C OR cc... Replaces to the end of the line.
3cc... Replaces the next 3 lines.
r... Replaces a character.
x... Delete one character.
J... join two lines together.
~... Changes the case of the character under the cursor.
D... delete from the cursor to the end of the line.
:1,5d... Deletes 5 lines.
:.,+5d.. Deletes 5 lines begining from the cursor.
:.,-5d.. Deletes 5 lines up from the cursor.
:.,$d... Deletes from the cursor to the end of the file.
================================================
Copy and paste:in command mode
yw.... Copy a word.
yl.... Copy a letter.
5yaw.... Copy the current word and the next four words.
yy.... Copy a line.
3yy.... Copy three lines.
p.... Paste after the current cursor.
P.... Paste before the current cursor.
================================================
Visual mode:
character-based (started with v).
line-based (started with V).
Block-based (started with Ctrl+V).
================================================
Searching:
/... to search forward
?...to search backward
n... find the next match
N... find the previous match
================================================
Search and Replace (in Ex mode):
:1,6s/old/new (substitutes from line 1 to 6)
:%s/old/new (substitutes the entire file)
:%s/old/new/i (case insensitive)
:r file_name (copy the content of that file to the file being edited with
vim)
================================================
Undo and redo (in command mode):
u.... undo
(.).... redo
ctrl+r.... redo
================================================
vi Options:
:set number Enables line numbers.
:set nu Enables line numbers.
:set nonu Turn line numbers off.
:set nonumber Turn line numbers off.
:set showmode Show the current mode of operation (default on).
:set noshowmode Hide the mode of operation.
:set tabstop=4 Set tab to 4 character jumps.
:set ts=4 Set tab to 4 character jumps.
:set ignorecase Ignore case-sensitive.
:set ic Ignore case-sensitive.
:set noic Case-sensitive.
:set hlsearch Set highlighting of search results on.
:set nohlsearch Set highlighting of search results off.
- To make this options available to all vi sessions, put it into a .exrc or .vimrc
file in your HOME-directory.
================================================
Execute a command in vim:
:.!date (the current cursor position)
:3!date (in the third line)
================================================
Best wishes:
Abeer :)