Installing and Using Vim text editor
Introduction
The vi editor is a very powerful tool and has a very extensive built-in manual, which you can activate using the
What makes vi confusing to the beginner is that it can operate in two modes: "command mode" and "insert mode". The editor always starts in command mode. Commands move you through the text, search, replace, mark blocks and perform other editing tasks, and some of them switch the editor to insert mode. This means that each key has not one, but likely two meanings: it can either represent a command for the editor when in command mode, or a character that you want in a text when in insert mode.
Installing on Linux
First, check if you have Vim.
$ vim
, and you should see something like this:
~
~ VIM - Vi IMproved
~
~ version 7.4.52
~ by Bram Moolenaar et al.
~ Modified by pkg-vim-maintainers@lists.alioth.debian.org
~ Vim is open source and freely distributable
~
If not, you should install Vim:
$ sudo apt-ge update
$ sudo apt-get install vim
You can check the default Vim tutorial with:
$ vimtutor
Starting
Similar to another editors, you can create or open a new file in the current directory with:
$ vim file.txt
As I mentioned in the introduction, Vim has two modes "command mode" and "insert mode". The editor always starts in command mode. So, commands move you through the text, search, replace, mark blocks and perform other editing tasks, and some of them switch the editor to insert mode. This means that each key has not one, but likely two meanings: it can either represent a command for the editor when in command mode, or a character that you want in a text when in insert mode.
After the file.txt is open:
-Insert
-Insert ESC buttom to switch the editor from insert mode (able to edit the file) to command mode (not able to edit the file).
NOTE: Instead of
-
-
-
-
-
-
Saving and Quitting
In command mode:
Insert
Insert
Insert
Default VIM setting in .vimrc
In command mode:
Insert
Insert
Insert
Insert
Insert
Insert
But if the file is closed with
$ cd
$ vim .vimrc
Then edit with Vim using
set ruler laststatus=2 number title hlsearch
syntax on
Then save and exit with
Setting Indent in .vimrc
In command mode:
Add the following lines in your .vimrc:
filetype plugin indent on
" show existing tab with 2 spaces width
set tabstop=2
" when indenting with '>', use 2 spaces width
set shiftwidth=2
Indent several lines
Press
Editing files
In command mode:
Insert
Insert Ctrl +
Insert
Copy/cut and paste into Vim
Position the cursor where you want to begin cutting.
Insert
Move the cursor to the end of what you want to cut.
Insert
Move to where you would like to paste.
Insert
Copy/cut and paste to external
-Copy from Vim to external:
Position the cursor where you want to begin cutting.
Insert
Move the cursor to the end of what you want to cut.
Insert
Move to where you would like and paste.
-Copy from external to Vim:
Select manually the text and copy.
Insert
Search and replace
In command mode:
Insert
Insert
Insert
Insert
Insert
Insert
Opening different files
When starting Vim, the -p option opens each specified file in a separate tab.
$ vim -p first.txt second.txt
Insert
In the opened file, insert
Cursor movement
List of commands to insert in command mode:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Ctrl +
- Ctrl +
- Ctrl +
- Ctrl +
Resoures
- Home site of Vim: https://vim.sourceforge.io/.
- Vim Cheat Sheet: https://vim.rtorr.com/.