Getting Started with Vim

Table of Contents

     🔰   Introduction
     🔰   Modes
     🔰   Movements
     🔰   Insert
     🔰   Find
     🔰   Go to
     🔰   Search
     🔰   Remove and Delete
     🔰   Replace and Repetition
     🔰   Visual Mode
     🔰   Practice

🔰 Introduction

Vim is one of the most powerful text editors I have ever used in CLI(Command Line Interface). There are so many files which need to be opened or edited. Vim can be the best choice because Vim is more than an editor. 😎

Installation commands on Ubuntu:

$ sudo apt update
$ sudo apt install vim

🔰 Modes

There are two basic modes in Vim.

  • insert mode (In this mode you can edit texts in your own way).
  • normal mode (In this mode you can manipulate and play with texts according to some instructions).

To switch between these modes you need to use Esc for normal mode and i for insert mode.

There is another mode in vim called visual mode. It is described here.

🔰 Movements

🚶 Basic Movements

You can move the cursor using following keys instead of uding arrow keys:

  • h for moving towards left.
  • l for moving towards right.
  • k for moving towards up.
  • j for moving towards down.

🚶 Word Movements

Movement between words is possible via these keys:

  • w or W for moving to the beginning of the next word from current cursor and will continue.
  • b or B for moving to the beginning of the previous word from current cursor and will continue.
  • e or E for moving to the end of the next word from current cursor and will continue.

Use number before these keys for moving fast e.g. 3w or 3W means cursor will move to the beginning of the 3rd next word.

🔰 Insert

📝 Insert Text Repeatedly

Suppose you want to insert yes 30 times consecutively. Steps will be as follows:

  • Write 30 in normal mode.
  • Then press i, vim will be switched from normal mode to insert mode.
  • Write yes then press Esc.

You will see that yes has been written 30 times consecutively.

📝 Insert New Line

By pressing o or O, a new line will be inserted and vim will be switched to insert mode as well.

🔰 Find

👓 Find a Character.

  • Press fq to find next q.
  • Press Fq to find previous q.
  • Press 3fq to find 3rd next occurrence of q.
  • Press 3Fq to find 3rd previous occurrence of q.

👓 Find word under cursor

  • Press * to find the next occurrence of the word under current cursor.
  • Press # to find the previous occurrence of the word under current cursor.

🔰 Go to

  • Go to matching parentheses ((), {}, []) by simply pressing %.
  • Press 0 to go to the start of the line.
  • Press $ to go to the end of the line.
  • Press gg to go to the beginning of the file.
  • Press G to go to the end of the file.
  • Press G with number before to go to the specific line e.g. press 3G to go to the 3rd line.

You can search a specific text in a file i.e. for searching text in your file, steps will be as follows:

  • Write /text and then hit Enter button.
  • After that press n, it will find the next text from the file and so on.
  • Press N, it will find the previous text from the file and so on.

🔰 Remove and Delete

✂️ Removing a Character

  • By pressing x, the character under the cursor will be removed.
  • By pressing X, the character to the left of the cursor will be removed.

✂️ Deleting

The delete key is d. You can use it with movements' keys.

E.g. dw will delete the first word on the right side of the cursor. It also copies the deleted content so that you can paste it to another location by pressing p.

🔰 Replace and Repetition

🚜 Replace

For replacing a letter under cursor:

  • Press r and then write replaceable letter e.g. a to replace cursor’s letter by a.

🚜 Repetition

Repeat the previous command by pressing simply .

E.g.

  • d2w will delete next two words of current cursor.
  • After that if you press ., the previous command i.e. next two words will also be deleted and so on.

🔰 Visual Mode

To switch to visual mode, just press v. In this mode you can select a text via movement keys.

E.g.

  • Press v and then e to select a word.
  • After that press d to delete that word.

🔰 Practice

When you change the file in vim you have to know these commands as well:

  • :w (save)
  • :wq (save and quit)
  • :q (quit)
  • :q! (quit without saving)
  • u (For undo)
  • ctrl+R (For Redo)
  • :help (For Help)

This article is written according to this website. You can practice above commands in realtime. 🎉🎉🎉

Comments

Comments are not configured yet. Create the giscus identifiers for shahnawaz-pabon/shahnawaz-pabon.github.io and add them as repoId and categoryId in src/data/config.js.