Linux - VIM Editor Commands


Linux - Vim Editor Commands




What is VIM?

Vim is a highly configurable text editor for efficiently creating and changing any kind of text. It is included as "vi" with most UNIX systems and with Apple OS X.

MODES

There are two different modes in VIM editor.

  • Command Mode
    • Move around the file
    • Copy, Paste and Delete the text
  • Insert Mode
    • User can insert the text

Change between the Modes

  • Press i to get into Insert Mode.
  • Press Esc (escape key) to exit form the Insert mode to Command Mode.

Text Entry Commands

  • a Append text following current cursor position
  • - A Append text to the end of current line
  • - i Insert text before the current cursor position
  • - I Insert text at the beginning of the current line
  • o Open up a new line following the current line and add text there
  • - O Open up a new line in front of the current line and add text there

Cursor Movement Commands

  • h Moves the cursor one character to the left
  • l Moves the cursor one character to the right
  • k Moves the cursor up one line
  • Moves the cursor down one line
  • nG or :n cursor goes to the specified nth line (ex. 10G goes to line 10)
  • Moves the cursor to the end of current line
  • 0 Moves the cursor to the beginning of the current line
  • Moves the cursor to the start of the next word
  • Moves the cursor to the end of the word
  • Moves the cursor to beginning of the word

Text Deletion Commands

  • Delete the character under the cursor
  • dw Delete word on right side of cursor
  • db Delete word on left side of cursor
  • dd Delete current line
  • d$ or D Delete current line till end
  • d^ or d0 Delete to beginning of current line
  • ndd Delete n lines from the current line

Yank - VI's copy commands

  • yy Yank current line
  • y$ Yank to the end of line from cursor
  • yw Yank from the cursor to the end of the word
  • yb Yank from the cursor to the beginning of the word
  • 5yy Yank 5 line from the current line

Paste (used after delete or yank to recover lines.)

  • p Paste after the cursor
  • P Paste before the cursor
  • np Paste n time from buffer
  • u Undo last change
  • U Restore line
  • J Join next line to the end of current line

Other Useful Commands

  • - Most commands can be repeated n times by typing a number, n, before the command. For example 10dd means delete 10 lines.
  • . (period) Repeat last command
  • cw Change the current word to a new word
  • Replace one character at the cursor
  • R Begin over-strike or replace mode, use ESC key to exit
  • :/pattern Search forward for the pattern
  • :?pattern Search backward for the pattern
  • (Press after the above search command to find the next occurrence and press to find the previous occurrence of the pattern)
  • :g/pattern1/s//pattern2/g Replace every occurrence of pattern1 with pattern2

Exit Commands

  • Press Esc key: This is very important because you must exit the edit mode first before typing the exit command(s). Next, you can type one of the following commands:
  • :q (yes, the colon is included in the command) – This will quite the editor
  • :q! – Quit Vim without saving the data file
  • :wq – Save the file and exit Vim

Example

  • 1. Type vim filename (open file in command mode)
  • 2. Press i (switch to insert mode)
  • 3. Enter text “Hello VIM”
  • 4. Hit Esc key (switch back to command mode)
  • 5. Type :wq (write file and exit)

Thanks

Comments

Popular posts from this blog

Top 5 Programming Languages to Learn in 2020

10 NodeJS Tips and Tricks for developers

What is Deno and will it Replace NodeJS?