Vim text editor

From Redbrick Wiki
Revision as of 00:01, 23 March 2026 by Digi (talk | contribs) (Add navigation section)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

What is Vim

Vim is a text editor. Vim was created as an extended version of the vi editor, with many additional features designed to be helpful in editing program source code, its full name is Vi IMproved. While Vim is cross-platform, it is most popular on Unix-like operating systems.

Interface

Vim's interface is based not on menus or icons but on commands given in a text user interface. For many users, Vim may present a steep learning curve, meaning that learning is slow initially but once the user gets a grasp of the basics they progress quickly and their editing becomes more efficient.

The Basics

to start vim and edit a file (new or existing)

   vim file_name

to start vim in NetRW (file tree)

   vim .

to write your changes

   :w

to quit vim

   :q

you can also combine these

   :wq

to write to a file

   :w <file>

to quit without saving

   :q!

to enter NetRW

   :Ex

Normal Mode

Normal mode is the default mode you are in when you enter Vim. This is the mode you will spend most of your time in, as it's used to navigate around your file. To go back to normal mode hit 'ESC' on your keyboard.

Navigation

To navigate in Vim you primarily use the

   I J K L

keys, to navigate to the

   left down up right 

directions respectively.

For example,

   J

will bring your cursor 1 line down! You can also add a number to vim commands to do them that number of times.

For example,

   5J

will bring your cursor 5 lines down!

There are also other keys to move your cursor such as:

   W

which moves your cursor forward one word,

   B

which moves your cursor back one word,

   G

which moves your cursor to the final line of the file,

   gg

which moves your cursor to the first line of the file, and

   f<character>

which moves your cursor to the next instance of <character> on the current line!


Searching for Words

Suppose you want to find all the words apple in your file 1. Make sure you are in command mode

   by hitting ESC

2. type

   /apple

followed by ENTER to find an occurrence of apple. When you type the slash, it and the following characters will be shown on the bottom of the screen. After you press ENTER, the cursor will go to the first occurrence of apple if found, and the target will be highlighted.

3. after you got the first apple, you can keep typing

   n

to find other apples

Substituting words

First make sure you're in command mode by pressing ESC.

  • replace first occurrence of old in current line with new
   :s/old/new/
  • replace all occurrence of old in current line with new
   :s/old/new/g
  • replace the first occurrence of old in each line between line n1 and n2 with new
   :n1,n2s/old/new/
  • replace all occurrence of oldbetween line n1 and n2 with new
   :n1,n2s/old/new/g
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
   :1,$s/old/new/gc
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
   :%s/old/new/gc