Get started with the Vi editor

The Vi application is the default text editor on most Linux systems, so it’s the primary interface you will use when you need to edit a configuration file. If you’re used to a graphical text editor, such as Notepad++ or VS Code, Vi can be confusing at first.

In its default form, Vi launches and runs in a terminal and is entirely keyboard-driven. But once you learn the basics of Vi, you’re likely to find that it’s a fast, efficient, and flexible editor.

Start Vi

To start Vi, open a terminal and type vi followed by a file path. If the file you provide doesn’t exist, it’s created when Vi opens:

$ vi example.txt

The vi command is usually a symlink to the vim (Vi improved) command, but many users refer to Vi as Vi, not Vim.

Command mode and insert mode

Vi has two different modes. There’s command mode and insert mode. Command mode is the default, and you can think of it as Vi’s menu. Because Vi doesn’t have a menu bar like most applications have, you use command mode to tell Vi what you want to do next (move up or down a line, delete a character or a whole word, copy and paste, and so on.)

You can always get back to command mode by pressing Esc on your keyboard. Get used to that; you’ll be pressing Esc a lot in Vi.

To enter insert mode, press i. In insert mode, you can type and edit text and use the arrow keys to navigate around your document. There’s not much to say about insert mode. It’s very much as its name advertises: In insert mode, you can insert text, so there’s nothing to learn except, maybe, touch typing.

Command mode, however, has a wealth of keyboard shortcuts and commands for you to discover. Remember, to enter command mode, press Esc. If you’re ever in doubt about which mode you’re in, just press Esc, and you can be sure that you’re now in command mode.

Keyboard shortcuts for command mode

In Vi, you don’t use your mouse or any of the keyboard shortcuts you’re used to in other editors. For instance, you don’t copy with Ctrl+C and paste with Ctrl+V because Vi has its own set of quick shortcuts that optimize key presses. It’s unquestionably a learning curve, but one that pays dividends. Once you’ve got the basic keyboard shortcuts committed to muscle memory, you’ll work faster in Vi than you’ve worked in any editor.

Modes

  • i: for insert mode
  • (that’s an upper-case i): insert text at beginning of current line
  • a: append text after cursor
  • A: append text at end of current line
  • Esc: command mode

[ Boost your Linux skills by downloading the Bash shell scripting cheat sheet. ]

Navigation

Navigation happens on the home row of your QWERTY keyboard:

  • h: cursor left
  • j: cursor down (j looks trivially like a down arrow)
  • k: cursor up
  • l (that’s a lower-case L): cursor right
  • b: cursor to beginning of current word
  • e: cursor to end of current word
  • $: go to end of line
  • 0 (that’s a zero): go to beginning of line
  • gg: go to first line of file
  • G: go to last line of file
  • 9G: go to line 9 of file

Select

  • v: select text
  • V: select line

Copy and paste

  • y: copy (“yank”) selection
  • yy: copy (“yank”) the entire line
  • p: paste

Delete

  • x: delete character
  • dw: delete word
  • d$: delete from current position to end of line
  • d0: delete from current position to beginning of line
  • dG: delete to last line of file

Undo

  • u: undo

Save

  • :w: save
  • :wq or :ZZ: save and quit
  • :q!: force quit without saving

Get to know Vi

Knowing Vi is a valuable skill on Linux because it’s on nearly every Linux system you’re likely to encounter. Once you get comfortable with its key bindings and how they enable you to keep your hands on the keyboard instead of bouncing back and forth to your mouse or even to Function keys, you’ll see why Vi has endured for so long.

Source

About Author