vi editor

  • Deletes lines 6 through 12: :6,12d
  • Exit vi editor session temporarily (type exit or Ctrl+d to come back): :sh
  • Run the specified command without quitting vi: :!ls
  • Show all options and their values: :set all
  • Show invisible characters (:set nolist): :set list
  • Show line numbers (:set nonumber): :set number
  • Find out the encoding: :set encoding
  • Set syntax highlighting: :setf [filetype]
  • Open a new tab: :tabnew [filename]
  • Move the current file to Gvim: :gui
  • Insert another file into the current file below the current line: :r [filename]
  • Insert command output into the document: :r !cmd
  • Edit another file: :e [filename]
  • Save the file with your new name and continue editing it under the old filename: :w [new filename]
  • Save the file with your new name and continue editing it under the new filename: :saveas [filename]
  • Save the text between line numbers that you specify: :1,6 w [filename]
  • Copy lines 1-2 after line 3: :1,2 co 3
  • Move line 4-5 after line 6: :4,5 m 6
  • Save a file with sudo command: :w !sudo tee %
  • Returns line number of current line at bottom of screen: :.=
  • Execute vi commands in a file directly from the command line: vi -c "%s/true/false/g|:wq" filename

Substitution commands

  • Change first occurrence on current line: :s/old/new
  • Change globally (all) on current line: :s/old/new/g
  • Change first occurrence of each line of file: :%s/old/new/
  • Change every occurrence in file: :%s/old/new/g
  • Change all occurrences between line 5 and 10 :5,10s/old/new/g
  • Change all occurrences from curent line to end of file: :.,$s/old/new/g
  • Change all occurrences starting at the current line for the next 10 lines: :.,+10s/old/new/g
  • Insert something at the start for the lines 1 to 3: 1,3s!^!new_word !
  • Insert something at the start for all lines: :%s!^!new_word !
  • Remove all blank lines: :%g/^$/d
  • Add something to end of line for all lines: :%s/$/new_word/g
  • Comment all lines between line 5 and 10: :5,10s/^/#/g
  • Transform everything in uppercase (or gggUG): :%s/.*/\U&/
  • Transform everything in lowercase (or ggguG): :%s/.*/\L&/
  • Remove CTRL-M characters in a file (to enter ^M, type CTRL-V, then CTRL-M): :%s/^M//g

vim commands

  • Comparing two files: vim -d <firstfile> <secondfile>

Plugins

vim-airline

mkdir ~/.vim
cd .vim
git clone https://github.com/vim-airline/vim-airline.git
shopt -s dotglob && mv vim-airline/* .

vi .vimrc

set t_Co=256

Links