Saturday, August 10, 2019

Getting Started with Vim - Quick Tutorial

Why would you want to use Vim?


When you ssh into a server, you will need a text editor that works in the terminal to change for example, server configurations. Nano is a text editor that works on terminal. But it is hard to navigate fast with Nano. Improve your Vim skill for the speed!

How to use Vim?


To open a file:
  • In the terminal, type in:  vi /tmp/1.txt
To navigate:
  • Your cursor keys just work
  • Press 'control' + 'D' for pageDown
  • Press 'control' + 'U' for pageUp

To get to the bottom of the doc:
  • Press 'shift' + 'G'
To get to the top of the doc:
  • Type in ':0' then 'enter'
To get to the start of the line:
  • Press 'shift' + '^'
To get to the end of the line:
  • Press 'shift' + '$'
To get to the 50th line of the doc:
  • Type in ':50' then 'enter'
To delete the current line:
  • Type in 'dd'
To delete the next 20 lines:
  • Type in '20dd'
To delete the all lines:
  • Type in '100000dd'
  • Repeat until there's no line left
To save file and exit:
  • Type in ':wq!' then 'enter'
To exit without saving:
  • Type in ':q!' then 'enter'
To edit:
  • Press the 'i' key to enter editing mode
  • Your 'delete' key and 'backspace' key just work
To exit editing mode:
  • Press the 'esc' key
To undo a change:
  • Press the 'esc' key to exit the edit mode
  • Press the 'u' key to undo
To get to a word:
  • Press the '/' key to search forward
  • Example: '/hello' will search forward the next hello that appears in the text
  • Press the 'n' key to get to the next 'hello'
To get to a word from the bottom of the doc:
  • Press 'shift' + 'G' to get to the bottom
  • Press 'shift' + '?' key to search backward
  • Example: '?hello' will search last hello that appears in the text
  • Press the 'n' key to get to the next 'hello'
To delete a word:
  • Use the above method to get to the word
  • Press 'dw' to delete the word
To delete one character:
  • Use the above method to get to the word
  • Press 'x' to delete a character
To copy text: 
  • Your cursor just works in selecting text
  • Your Windows command just works: 'ctrl' + 'c'
  • 'cmd' + 'c' for OSX
To paste text:
  • Press the 'i' key to enter editing mode
  • Your Windows command just works: 'ctrl' + 'v'
  • 'cmd' + 'v' for OSX
 If your terminal does not support text selection with copy & paste, try:

To copy 1 line:
  • Press 'yy' to copy the current line
To paste 1 line:
  • Press 'p' to paste a copied a line


Great. You know how to use vim.

No comments: