Intro to Vim
January 31, 2019
This blog is about introduction to one of the coolest, highly configurable text editor for efficiently creating and changing any kind of text i.e Vim.
Why to use Vim
Learning to drive a car takes effort. Is that a reason to keep driving your bicycle? No, you realize you need to invest time to learn a skill. Text editing isn’t different. You need to learn new commands and turn them into a habit.
- Bram Moolenaar
- It’s fast
- I don’t need to remember
- Light on fingers
- persistent, multi-level undo tree
- extensive plugin system
- support for hundreds of programming languages and file formats
- powerful search and replace
- integrates with many tools
History
Bram Moolenaar began working on Vim in 1988 for a company called Amiga. He published first public release of Vim in 1991. The name Vim is an acronym for VI improved because Vim is an extended version of vi editor. It was built with the aim of adding some additional features designed to help in source code editing.
Interface of Vim
Vim is based on text user interface and like other text editors, it also has a GUI mode, gVim. Vim has a built-in tutorial for beginners(vimtutor command). Vim also has a built-in facility (:help command) which allow the user to search, explore and query commands.
A little Walk-through of Vim
Starting Vim
vim <file name>
vim +NUM <file-name>
vim +/{pattern} <file-name>
vim +cmd or -c cmd <file-name>Exiting Vim
-
In normal mode
:q!Quit Vim without saving the changes to the file.ZQSame as :q! Quits Vim without writing changes
-
Or
Ctrl + zTo send vim back in background$fgTo get back in vim
Save
:wSave the file:w new_nameSave the file with the new_name filename:wqSave the file and quit Vim.ZZWrite file, if modified, and quit Vim
Special inserts
:r [filename]Insert the file [filename] below the cursor:r ![command]Execute [command] and insert its output below the cursor
Delete text
xdelete the character at cursordwdelete a word.d0delete to the beginning of a line.d$delete to the end of a line.d)delete to the end of the sentence.dggdelete to the beginning of the file.dGdelete to the end of the file.dddelete line3dddelete three lines
Simple replace text
r{text}Replace the character under the cursor with {text}RReplace characters instead of inserting them
Copy/Paste text
yycopy current line into storage bufferppaste storage buffer after current linePpaste storage buffer before current line
Undo/Redo operation
uundo the last operation.Ctrl+rredo the last undo.
Search and Replace keys
/search_textsearch document for search_text going forward?search_textsearch document for search_text going backwardsnmove to the next instance of the result from the searchNmove to the previous instance of the result:%s/original/replacementSearch for the first occurrence of the string “original” and replace it with “replacement”:%s/original/replacement/gSearch and replace all occurrences of the string “original” with “replacement”:%s/original/replacement/gcSearch for all occurrences of the string “original” but ask for confirmation before replacing them with “replacement”
Navigation keys
hmoves the cursor one character to the left.j or Ctrl + Jmoves the cursor down one line.k or Ctrl + Pmoves the cursor up one line.lmoves the cursor one character to the right.0moves the cursor to the beginning of the line.$moves the cursor to the end of the line.^moves the cursor to the first non-empty character of the linewmove forward one word (next alphanumeric word)Wmove forward one word (delimited by a white space)5wmove forward five wordsbmove backward one word (previous alphanumeric word)Bmove backward one word (delimited by a white space)5bmove backward five wordsGmove to the end of the fileggmove to the beginning of the file.
Modes
- Normal Mode
- Insert Mode
- Command Mode
- Visual Mode
Insert Mode -> Normal Mode
EscNormal ModeCtrl [Same as above
Auto Completion
You can start from a built-in omnifunc setting. Just put:
filetype plugin on
au FileType php setl ofu=phpcomplete#CompletePHP
au FileType ruby,eruby setl ofu=rubycomplete#Complete
au FileType html,xhtml setl ofu=htmlcomplete#CompleteTags
au FileType c setl ofu=ccomplete#CompleteCpp
au FileType css setl ofu=csscomplete#CompleteCSSon the bottom of your .vimrc, then type
NeoVim
NeoVim also quoted as Literally the future of Vim is known for the cool and awesome features.
- Powerful The Nvim msgpack API enables structured communication to and from any programming language. Remote plugins run as co-processes that communicate with Neovim safely and asynchronously.
-
Usable
- Strong defaults
- Works the same everywhere: one build-type, one command
- Modern terminal features such as cursor styling, focus events, bracketed paste
- Built-in terminal emulator
- Embeddable GUIs and other applications can nvim —embed to discover the msgpack API dynamically.
- Drop-in Vim Neovim is an extension of Vim: feature-parity and backwards-compatibility are high priorities. If you already use Vim, see :help nvim-from-vim.
Other features
And below are some of the simple and elegant commands used to indent lines quickly in Vim or gVim.
gg=GThe master of all commands is. This indents the entire file!==To indent the current line=GTo indent all the lines below the current linen==To indent n lines below the current line=%To indent a block of code, go to one of the braces and use the command
These are the simplest, yet powerful commands to indent multiple lines.