DEV Community

Cover image for Vim Basics
csm
csm

Posted on

Vim Basics

Just the basics you need to get started.
Also for those who are trapped in classic vim situation, where you don't know how to quit!

Vim is a powerful, keyboard-driven command-line text editor (an improved version of the classic Unix “vi”) used for efficiently editing code and plain-text files.

Things you need

  • Linux Mint or Ubuntu
  • Vim 8.2 or above
  • Alacritty terminal (Why? Just for feel!)

Install

sudo apt update && sudo apt install vim
Enter fullscreen mode Exit fullscreen mode

Open a file in Vim

Suppose you are inside a folder called hello and it has a file hello.txt.
You open the file in Vim:

vim hello.txt
Enter fullscreen mode Exit fullscreen mode

Modes of Vim

In vim we have modes.
When you open a file the default mode is Normal mode.

Normal mode  -> Esc key
Insert mode  -> i
Command mode -> Esc key then :
Enter fullscreen mode Exit fullscreen mode

What the heck are modes?

  • When you press 'i', you can type in the editor, thats Insert mode.
  • When you press 'Esc' key, you go back into Normal mode where you can view the file without typing.
  • When you press ':' inside normal mode, that is Command mode.

For beginners, insert mode is for you to type.
Later you will learn the powers of normal mode where you can use Vim's shortcuts to edit the file super hyper fastly!
Command mode, for now, is for saving the changes you made and to quit and get out of Vim.

Quit, Save, and others

First, press 'Esc' to ensure you are in Normal mode.
Then:

  • To just quit: Type :q then press Enter
  • To just save: Type :w then press Enter
  • To save and quit at once: Type :wq then press Enter
  • To quit without saving: Type :q! then press Enter

Example

1) Open the hello.txt file:

vim hello.txt
Enter fullscreen mode Exit fullscreen mode

2) Type some text:

  • Press i
  • Then type this text:
hello world!
Enter fullscreen mode Exit fullscreen mode

3) Save the changes:

  • Press Esc to come out of insert mode
  • Type :w and press Enter to save the changes

4) Quit:

  • Type :q and press Enter to quit

Classic 'Unable To Quit Vim' Situation

If you are someone who just want to quit, then:

  • Press Esc then type :q! then press Enter

Thats it! 🥶

Top comments (0)