Vim should be installed by default with your Fedora Core distribution, if not, type
yum install vim-inhanced
Start Vim typing vim (and not vi, this will launch vi with minimal features that you won't be able to use here)
Please note, this post does not aim to introduce you to Vim world; if you are new, type
vimtutor
and follow the instructions, they are very instructive and teach you basic use of the editor. In another word, it's very hard to step forward without reading the tutorial and practicing it.
Here is a reminder of the basic operations that you should already have learned:
"To enter insert mode, just type "i"; to save:
:w
"Where is carriage return (just hit Enter)
"To quit and save:
:wq
"To quit without saving
:q!
"To delete a line and move it to the buffer, put the cursor on the line and type
dd
"To paste the line, press "p" it will be added under the cursor; "yy" is for pasting
Advanced deletion
"To delete 2 lines
2dd
"To delete 4 next words
4dw
"To delete from the cursor to the end of line
d$
"To delete from the cursor to the next specified string
d/somestring
"To delete a paragraph (A paragraph is separated by more than one return)
d}
"3 paragraphs
3d}
To just copy, repeat the same operations above replacing 'd' by 'y'Search'n'Replace
Hit slash (/) to start searching; Vim uses regular expressions very similar to Perl, Awk or PythonFor example, the following text
Leonardo da Vinci (1452-1519) of Florentine was known as one of the great masters of the High Renaissance, due to his innovations in both art and science.If you type
/^.*H.\{3\}
Will highlight from the beginning of the text till the word "High". It uses the following logic:
^ : Beginning of line
.* : Any thing
.\{3\} : 3 characters (which comes after the capital "H")
"To toggle highlighting:
: set hls!
"To set insensitive case
: set ignorecase
"To activate "smart" search mode (Will respect uppercase if specified)
: set smartcase
As for replacing, you use the following syntax
:%s/\(^.*)\)\(.*\)\(F.\{9\}\)\(.*\)\(art\)\(.*\)\(\ s.*$\)/Richard\ M.\ Stallman\2Boston\4Software\6\ Freedom./g
As you may notice, % represents the whole document, if you want to specify lines, type instead
:5,20s
A group is limited by \( and \) and the group content is called by antislash followed by the group number, and /g to specify a global substitution.
Playing with cases
To convert a line to uppercase, place the cursor on the beginning of the line, and typegU$
The symbol $ means the end of line, if you type "w" instead, it will replace the word you are placing the cursor on; if you type "G", it will replace till the end of document. To convert a specified number of words, type the number before the "w", example: gU4w
gu$
Converts, with the same way, the line to lowercase.
Visual Editing
Place the cursor on a line and type "V", you will switch to Visual mode, when you move the arrow, the text will be selected, without typing "Word Completion
Without having to exit the insert mode, type Ctrl+N at the word you are typing; if the word already exists, it will be completed. If there are many possibilities, you will be proposed the words according to frequency of use, type Ctrl+N again if it's not the right word.Make everything predefined
For an optimal mode of programming (Syntax highliting, auto-indent, etc.), copy the default vimrc to your personal vimrccp -v /usr/share/vim/vim/vimrc_example.vim ~/.vimrc
Run Vim again to see the difference.
vimrc is the file that contains your preferences and instructions. You may for example append the following lines to it (Yes, with Vim too! Changes will be considered after you restart Vim)
set ignorecase
set smartcase
Typing macro's is funny; you don't have to learn Vim symtax, just suppose you are typing the line.
For example:
When you hit F7 key, Perl headers will appear, F9 will generate a C template and with F8, Python headers.
The macro has the following structure:
map keys command
Let's try
map i#!/usr/bin/perl -wuse strict;use warnings;use POSIX;
""""""""""""""""""
map i#include int main(int argc, char *argv[]){return 0;}
""""""""""""""""""
map i#!/usr/bin/pythono## -*- coding: utf-8 -*-
Getting help
Simply type:h commandname
for example
:h number
You also may try
:h 42
Don't wonder, Vim developers are also humans who want to get fun sometimes!