Introduction
- Ice Breaker
- Vim help and resources
- Opening Vim
- Modes of Operation
- Insert mode
- Normal mode
- Visual mode
- Command Line mode
- Identifying current mode in gvim
Vim is a text editor, powerful one at that. It has evolved from vi to vim (Vi Improved) and gvim (Vim with GUI)
- Vim is not a full fledged writing software like LibreOffice Writer or MS Word. It is a text editor, mainly used for writing programs
- Notepad in Windows is an example for text editor. There are no formatting options like bold, heading, page numbers etc
- If you have written C program within IDE, you might have noticed that keywords are colored (syntax highlighting), automatic code indentation, etc. Vim provides all of that and more
Vim has programmable abilities on editing tasks. One can also execute shell commands right within Vim and incorporate their outputs. Text can be modified, sorted, regular expressions be applied for complicated search and replace. Record a series of editing commands and execute on other portions of text in same file or another file. To sum it up - for most editing tasks, no need to write a program, they can be managed within Vim
Now, one might wonder, what is all this need for complicated editing features? Why does a text editor require programming capablities? Why is there even a requirement to learn using a text editor? All one needs from editor is writing text with keyboard, use Backspace or Delete key, have some GUI button for opening and saving, preferrably a search and replace feature, etc.
A simple and short answer is: to reduce repititive manual task and the time taken to type a computer code. Faster editing probably makes sense. Reduce typing time? seems absurd. But when you are in the middle of coding a large program, thinking about complicated logic, it helps to have minimal typing distraction
Ice Breaker
Open a terminal and try these steps:
gvim first_vim.txtopens the file first_vim.txt for editing (usevimifgvimis not available)- Press
ikey (yes, the lowercase alphabet i, not any special key) - Start typing, say 'Hello Vim'
- Press
Esckey - Press
:key - Type
wq - Press
Enterkey cat first_vim.txtsanity check to see what you typed is saved or not
Phew, what a complicated procedure to write a simple line of text, isn't it? This is the most challenging and confusing part for a newbie to Vim. Don't proceed any further if you haven't got a hang of this, take help of youtube videos if you must. Master this basic procedure and you are ready for awesomeness of Vim
- Material presented here is based on
gvim, which has a few subtle differences fromvim- see this stackoverflow thread for details
Vim help and resources
gvimtutorshell command. Opens a tutor file and has various lessons to get started with Vimvimtutorif gvim is not available
- For built-in help, use
Helpmenu in gvim, or type:helpin Normal mode (:his short for:help):h usr_toc.txttable of contents for Vim User Manual - 'Task oriented explanations, from simple to complex. Reads from start to end like a book.':h reference_toctable of contents for Reference Manual - 'Precise description of how everything in Vim works.'- also see
:h help-summaryand guideline to use help
- best introduction to Vi and its core editing concepts explained as a language
- this stackoverflow thread also has numerous tips and tricks to use Vim
- About, Download, Documentation, etc - https://github.com/vim/vim
- Vim 8.0 is in works
- Vim curated resources
Opening Vim
gvimnew document when filename is not specifiedgvim script.shopens script.sh, blank document if script.sh doesn't existgvim report.log power.log area.logopen specified files, but only report.log is in the current buffer. Use:n(short for:next) to go to next buffer for editinggvim -p report.log power.log area.logopen specified files in separate tabsgvim -o report.log power.log area.logopen specified files as horizontal splitsgvim -O report.log power.log area.logopen specified files as vertical splitsgvim -R script.shopens script.sh in Readonly mode, changes can still be made and saved despite warning messages shown (for example, by using:w!)gvim -M script.shopens script.sh in stricter Readonly mode, Vim won't allow any changes to be made unless:set modifiableis used and file can't be saved until:set writeis usedgvim +/while script.shopen script.sh and place the cursor under first occurrence of whilegvim +25 script.shopen script.sh with cursor on 25th linegvim -c 'normal =G' script.plopen script.pl and execute the normal mode command=Ggvim -c ':%s/search/replace/g' script.plopen script.pl and execute the given Command Line mode commandgvim -hfor complete list of options
See also stackoverflow - using vim to interactively edit lines filtered using grep
Modes of Operation
Vim is best described as a modal editor. Here, we'll mainly see four modes
- Insert mode
- Normal mode
- Visual mode
- Command Line mode
For complete list of modes, see :h vim-modes-intro and :h mode-switching
Insert mode
This is the mode where required text is typed. Usual editing options available are Delete, Backspace and Arrow keys. Some smart editing short-cuts:
Ctrl+wdelete the current wordCtrl+pauto complete word based on matching words in backward direction, if more than one word matches, they are displayed as drop-down listCtrl+nauto complete word based on matching words in forward directionCtrl+x Ctrl+lauto complete line, if more than one line matches, they are displayed as drop-down listCtrl+tindent current lineCtrl+dunindent current line
Pressing Esc key changes the mode back to Normal mode
Normal mode
This is the default mode when Vim is opened. This mode is used to run commands for editing operations like copy, delete, paste, recording, moving around file, etc. It is also referred to as Command mode
Below commands are commonly used to change modes from Normal mode:
Changing to Insert mode
iinsert, pressing i (lowercase) key changes the mode to Insert mode, places cursor to the leftaappend, pressing a (lowercase) key changes the mode to Insert mode, places cursor to the rightIto place the cursor left of first non-blank character of line (helpful for indented lines)gIto place the cursor at first column of linegito place the cursor at same position where it was left last time in Insert modeAto place the cursor at end of lineoopen new line below the current line and change to Insert modeOto open new line above the current line and change to Insert modesdelete character under the cursor and change to Insert modeSdeletes entire line and change to Insert mode
Changing to Command Line mode
:changes the mode to Command Line mode, awaiting further commands/change to Command Line mode for searching in forward direction?to start searching in backward direction
Visual mode
Visual mode is used to edit text by selecting them first
Selection can either be done using mouse or using visual commands
vstart visual selectionVvisually select current lineCtrl+vvisually select column(s)
Pressing Esc key changes the mode back to Normal mode
Command Line mode
- After
:or/or?is pressed in Normal mode, the prompt appears in last line of Vim window - This mode is used to perform file operations like save, quit, search, replace, shell commands, etc.
- Any operation is completed by pressing
Enterkey after which the mode changes back to Normal mode
Press Esc key to ignore whatever is typed and return to Normal mode
Identifying current mode in gvim
- In Insert mode, the cursor is a blinking
|cursor, also-- INSERT --can be seen on left hand side of the Command Line - In Normal mode, the cursor is a blinking rectangular block cursor, something like this █
- In Visual modes, the Command Line shows
-- VISUAL --or-- VISUAL LINE --according to visual command used - In Command Line mode, the cursor is of course on the Command Line