Saturday 18 May 2024

Useful vi editor commands

vi editor:
  • text editor for Linux and Unix
  • used to create, edit and manage text files

vim editor is the advanced version of vi editor.


Vi has several modes:
  • Command
  • Insert
  • Visual
  • Last Line (Escape)

Command Mode

This is the default mode, the one vi is in upon the start of the editor. 
Characters are interpreted as commands and are not displayed.

In this mode we can:
  • move through a file
  • delete
  • copy
  • paste

To delete a line, move the cursor with Up and Down keys to desired line and then press dd.

Insert Mode

To get into this mode, press i key while in Command or in Visual mode.

Visual Mode

In this mode we can:
  • select and delete consecutive lines of text
  • copy and paste selected text

To enter this mode from the command mode, go first to the desired line e.g. the first or last line of the section that we want to delete or copy and then press v key.

To make a selection, go up and down with Up and Down keys.

To delete the selection press x or Delete keys.

To delete the current line, press d twice (dd).

To copy selection, press y ("yank" - saves selection into vi's register; copied text is not in OS's clipboard and can't be pasted to another application).

To paste selection, move the cursor to target location and press p ("put").

Press i to get to Insert mode where you can e.g. paste the content of the OS clipboard.

Last Line Mode (Escape Mode)

To get into this mode from Command mode, press colon (:).

In this mode we can:
  • save the file
  • execute commands
q - to quit
q! - force quit - without saving changes
wq - write (save) changes and quit
w - write (save) to file ("Save As...")
w! - force write - overwrite file 


No comments: