Table of Contents
1. vi Editor
In the 90s, when visual editors were introduced, the popularity of command-line editing tools like the vi editor started declining. Then in the first decade of this century came the IDEs (Integrated Development Environment). Eclipse made IDE attractive and popular among developers. Developers of the current generation prefer an IDE to plain visual text editors and most of them do not even know about the existence of the vi editor. However, people working in system engineering in Linux and Mac still prefer vi. In this article, I’ll highlight 20 commands when practiced, will enable you to use the vi editor like a pro.
2. Modes
The improved version of the vi editor is vim (vi improved). All the vi commands are supported by vim as well. There are two modes in the vi
editor:
- Command mode – This mode allows to perform tasks like cut, copy, paste, delete, undo, redo, etc.
- Insert mode – In this mode, it directly adds texts to the file.
3. Commands
To open a file in vi, use the command vi file_name
.
SL# | Purpose | Command |
---|---|---|
1 | Switch Mode | Esc to switch to command modei to switch to insert mode and add characters before the current positionI to switch to insert mode but move the cursor to the beginning of the linea to switch to insert mode and add characters after the current positionA to switch to insert mode but move the cursor to the end of the line |
2 | Go to Line | G to go to the last linegg to go to the first linenG to go to the nth line. Example: 120G :line_number to go to a specific line. Example: :120 |
3 | Undo and Redo | u or :u or :undo for undoing the last change. to repeat the last undo operation:Nu for undoing last N changes. Example: :2u Ctrl + r for redoing the last change |
4 | Move | hjkl to move left, down, up, and rightw to move one word forward b to move one word backward0 to move to the beginning of the line $ to move to the end of the lineH to move to the top of the screenM to move to the middle of the screenL to move to the bottom of the screen Ctrl + f for page downCtrl + b for page up |
5 | Copy and Paste | yy or Y to copy the current line to the clipboardNyy to copy N lines to the clipboard. Example: 10yy yw to copy the current word. Example: 2yw or y2w yl to copy the current letterp to paste clipboard contents after the current lineP to paste clipboard contents before the current line |
6 | Delete | x to delete the character at the current cursor positionX to delete the character before the current cursor positiondw to delete one worddd to delete the current lined$ or D to delete from the current position to the end of the lined^ to delete from the current position to the beginning of the line |
7 | Add a line | o to add a line after the current line O to add a line before the current line |
8 | Find and Replace | /search_text for search forward ?search_text for search backwardn repeats the last search in the forward direction N repeats the last search in the backward direction:s/search_text/replace_text to replace the first occurrence of search_text by replace_text :s/search_text/replace_text/g to replace all occurrences of search_text by replace_text in the current line:%s/search_text/replace_text/g to replace all occurrence of search_text by replace_text in the entire file |
9 | Save and Quit | :w to save:q to quit! to force either saving or exiting:wq to save and quit:q! to force quit without saving |
10 | Match Brackets | % to match brackets ( with ) and { with } |
11 | Shift | << Shifts line left>> Shifts line right |
12 | Options | :set option to set an option:set number or :set nu to show line numbers:set autoindent for automatic line indentation:set showmatch to show bracket matches ( with ) and { with } :set autowrite to write before quit:set cursorline to highlight the current line |
13 | Configuration | To permanently configure options open vi ~/.vimrc and add one or more lines to it:syntax on for syntax highlighting:set number to show line numbersSet other options mentioned in #12 as needed. |
Join our list to get instant access to new articles and weekly newsletter.
[…] To work with vi editor like a pro, refer this article: Work With vi Editor Like a Pro. […]
[…] In this article, you have learned how to show line numbers in the vi editor temporarily for a single session or permanently. To work with vi editor like a pro, refer to this article: Work With vi Editor Like a Pro. […]
[…] highlighting in the vim ad vi editor. To work with vi editor like a pro, refer to this article: Work With vi Editor Like a Pro. To know how to go to a specific line in vi editor refer to this article: vi Show Line […]