Linux - Vi Editor
Vi is a text editor originally created for Unix-based systems and is widely used in Linux environments. It is a powerful, lightweight, and versatile editor that operates directly within a terminal window. Vi is known for its efficiency and keyboard-based editing commands, making it popular among system administrators, developers, and power users.
Here are some key commands of VI
Basic Navigation
h
: Move leftj
: Move downk
: Move upl
: Move right0
: Move to the beginning of the line$
: Move to the end of the linew
: Move to the next wordb
: Move to the previous wordG
: Go to the last line of the filegg
: Go to the first line of the file:n
: Go to line n
Modes
i
: Insert mode (enter text)Esc
: Exit insert mode (return to normal mode)v
: Visual mode (select text)V
: Visual line mode (select whole lines)Ctrl + v
: Visual block mode (select a block of text)
Editing
x
: Delete character under cursordd
: Delete the current linedw
: Delete the wordd$
: Delete from cursor to the end of the lined0
: Delete from cursor to the beginning of the lineyy
: Copy (yank) the current linep
: Paste after cursorP
: Paste before cursoru
: Undo last changeCtrl + r
: Redo last undone changecw
: Change (replace) wordr
: Replace a single character
Search
/text
: Search for textn
: Jump to the next occurrence of the search termN
: Jump to the previous occurrence of the search term?text
: Search backward for text
Saving and Exiting
:w
: Save the file:q
: Quit:wq
: Save and quit:q!
: Quit without savingZZ
: Save and quit (shortcut)
Working with Multiple Files
:e filename
: Open a new file in the same window:bnext or :bn
: Go to the next buffer:bprev or :bp
: Go to the previous buffer:sp filename
: Open a file in a new horizontal split:vs filename
: Open a file in a new vertical splitCtrl + w + w
: Switch between split windows:close
: Close the current split window
Other Useful Commands
:set number
: Show line numbers:set nonumber
: Hide line numbers:%s/old/new/g
: Replace all occurrences of old with new in the file:s/old/new/g
: Replace all occurrences of old with new on the current line:noh
: Turn off highlighting for search resultsCtrl + g
: Show the file name and cursor position in the file