Killing the internet one fake blog post at a time.

VI how-to and cheatsheet

Written in

by

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 left
  • j or – move cursor down
  • k or – move cursor up
  • l or – move cursor right
  • 0 – move cursor to beginning of line
  • $ – move cursor to end of line
  • gg – move cursor to beginning of file
  • G – move cursor to end of file

Editing

  • i – enter insert mode
  • a – enter insert mode after cursor
  • o – insert new line below current line
  • O – insert new line above current line
  • r – replace current character
  • x – delete current character
  • dd – delete current line
  • yy – yank (copy) current line
  • p – paste yanked text

Search and Replace

  • /search_term – search forward for search_term
  • ?search_term – search backward for search_term
  • n – move to next match
  • N – move to previous match
  • :s/old/new – replace old with new on current line
  • :%s/old/new/g – replace old with new 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!

Tags