Vim text editor: Difference between revisions

From Redbrick Wiki
Jump to navigation Jump to search
No edit summary
 
(Add navigation section)
 
Line 8: Line 8:
to start vim and edit a file (new or existing)
to start vim and edit a file (new or existing)
     vim file_name
     vim file_name
to start vim and open a new file (
to start vim in NetRW (file tree)
You can give the stuff you typed in a name when later you save it as a file.)
     vim .
     vim


to exit vim and save your document first press '''ESC''' then
to write your changes
    :w
to quit vim
    :q
you can also combine these
     :wq
     :wq
to exit, saving as a new filename again press '''ESC'' then
to write to a file
     :wq <filename>
     :w <file>
to exit without saving (force quit) press '''ESC''' then
to quit without saving
     :q!
     :q!
to enter NetRW
    :Ex


==='''Editing'''=== When in vim you will notice that you cannot enter any text, this is because you are in the command mode of the editor, to enter the insert mode press 'i' or 'insert'. To return to the command mode hit 'ESC'
==='''Normal Mode'''===
==='''Deleting'''===
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.
OK, the 'delete' key always works, as does the 'backspace'. However, vim has another deletion method through the command mode that doesn't require entering the insert mode.


1. move the cursor to the character you want to delete
===='''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!


2. press x , this character disappears


==='''Searching for Words'''===
==='''Searching for Words'''===

Latest revision as of 00:01, 23 March 2026

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