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.

Modes


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

Command (Normal) 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.

Press ^ to move the cursor to the start of the current line.
Press $ to move the cursor to the end of the current line.
Press H (“high”) to move the cursor to the top of the screen.
Press M (“middle”) to move the cursor to the middle of the screen.
Press L (“low”) to move the cursor to the bottom of the screen.

Last Line Mode (Escape Mode)


To get into this mode from Command mode, press colon (:) after which a colon character appears at the bottom of the window and after it you can type a command.

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 

To show previously executed commands, type :  and press UP/DOWN keys.

To remove all leading whitespace from every line in the file, effectively "left-aligning" text at the beginning of each line, type command: %s/^\s*//g.
  • In vi or vim is a substitution command used to remove all leading whitespace (spaces, tabs, etc.) from every line in the file. Here's a breakdown of the components:
    • %: This specifies the range of lines to apply the substitution. % means "all lines in the file."
    • s: The substitute command, which is used for search and replace.
    • /^\s*//g:
      • ^: Matches the beginning of a line.
      • \s*: Matches zero or more whitespace characters (spaces, tabs, etc.).
      • //: Replaces the matched pattern with nothing (effectively deleting it).
      • g: Global flag, ensuring all matches in each line are processed.
To remove all double quotation characters: :%s/\"//g
To remove all comma characters: %s/,//g


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.


References:


No comments: