Vim crash course from someone used to GUI

By xngo on March 2, 2019

Overview

I always want a super fast and small editor to write code in Bash, Python, CSS, Javascript, HTML, dot, etc. Something that has syntax highlighting capability and it is cross-platform. I have found that editor. It is called SciTE. I have used it for years. Now, I write more and more complex code in Python and I miss the autocomplete features of Java & C/C++ editors. Without an autocomplete in Python, typo errors are commons.

I decided to take the plunge into Vim world. It fits my requirements: small, fast, syntax highlighting and autocomplete.

Common pitalls

  • Press Ctrl+s will freeze your terminal. To get out press Ctrl+q
  • To quit Vim. Press ESC, then press :q!

Vim concept

Coming from GUI editor, it is expected to simply open a file and then start editing. However, for Vim, you have to ditch that notion. Vim has a different concept. It has modes: visual(v), insert(i), command(:), replace. Before you do anything, you have to switch to the correct mode. Press ESC allows you to change mode and then select a mode:

  • v to view, read-only mode.
  • i to edit(insert)
  • : to run your command

Basic commands

It assumed that before typing the commands below, you have already pressed ESC.

  • :q: Quit
  • :q!: Quit without saving.
  • :w: Save
  • :w filename.txt:: Save into filename.txt
  • :set number: Display line number.
  • gg: Go to the beginning of the file.

Copy and paste

  • v: Select by moving your cursor.
  • y: Copy(yank in Vim term).
  • p: Paste.
  • dd: Cut the line. Can be used to delete line.
  • V: Select the whole line.

Delete

  • dw: Delete next word.
  • d$: Delete the remainding of line.

Undo & redo

  • u: Undo.
  • Ctrl+r: Redo.

Select all

Select all needs multiple commands: gg"*yG.

  • gg: Place the cursor at the beginning of the file.
  • "*y: Start copy(yank) command to the register * from the first line, until
  • G: end of file.

This action requires a long commands. It is good to add this action in ${HOME}/.vimrc as follows:

map <C-a> <esc>ggVG<CR>

Copy from and to clipboard

It is good that you can copy and paste from Vim to other applications and vice versa. The medium to hold the data for exchange is the clipboard.

To paste from the clipboard to Vim:

  1. Enter the INSERT mode by pressing ESC and then i.
  2. To paste, press Ctrl+Shift+v.

To copy to clipboard so you can paste to other programs:

  1. Your Vim needs to compile with +clipboard to work. Check vim --help | grep clipboard.
  2. *yy

Highlight search

To highlight all occurences of a word, turn on hlsearch as follows:

  1. Press ESC.
  2. Type in :set hlsearch
  3. Position your cursor to the word and then press *.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.