Vi is a powerful text editor that comes pre-installed on most Unix-based systems, including Linux and macOS. While it may have a steep learning curve, mastering vi can greatly improve your productivity as a developer or system administrator. In this blog post, we will cover the basics of using vi and provide you with a cheat sheet to help you get started.
Getting Started with vi
To open a file in vi, simply type vi
followed by the file name:
vi filename
This will open the file in vi’s command mode. To start editing the file, press i
to enter insert mode. Once you’re in insert mode, you can start typing and editing the file.
When you’re done editing the file, press Esc
to exit insert mode and return to command mode. From there, you can save the file by typing :w
(write) followed by :q
(quit) to exit vi. If you want to save changes and exit at the same time, type :wq
.
Vi Cheat Sheet
Here’s a list of some of the most common vi commands to help you get started:
Navigation
h
or←
– move cursor leftj
or↓
– move cursor downk
or↑
– move cursor upl
or→
– move cursor right0
– move cursor to beginning of line$
– move cursor to end of linegg
– move cursor to beginning of fileG
– move cursor to end of file
Editing
i
– enter insert modea
– enter insert mode after cursoro
– insert new line below current lineO
– insert new line above current liner
– replace current characterx
– delete current characterdd
– delete current lineyy
– yank (copy) current linep
– paste yanked text
Search and Replace
/search_term
– search forward forsearch_term
?search_term
– search backward forsearch_term
n
– move to next matchN
– move to previous match:s/old/new
– replaceold
withnew
on current line:%s/old/new/g
– replaceold
withnew
in entire file
Saving and Quitting
:w
– write (save) file:q
– quit vi:wq
– write (save) and quit vi:q!
– force quit vi without saving
Vi may take some time to learn, but it’s a powerful tool that can greatly improve your efficiency and productivity as a developer or system administrator. With a little practice and the help of this cheat sheet, you’ll be editing and manipulating text like a pro in no time!