Lecture # 13 - vim Text Editor

Lecture # 13 - vim Text Editor

Working with vim Text Editor.

vim is also known as vi improved

Opening file with vim:

To open file in vim vim [file-name] command is used. If there is already a file with the name entered, this command will open that existing file otherwise a new file will be created and opened in vim editor.

By running the command vim new.txt this empty buffer will be opened.

In vim text editor, there are four modes:

  1. Normal Mode:

    This is the default mode when you start Vim. In Normal Mode, you can navigate the document, delete text, copy and paste text, search, and perform various other editing tasks using single-key commands.

  2. Insert Mode:

    In Insert Mode, you can actually insert and edit text. You can enter Insert Mode from Normal Mode by pressing "i" key. You can exit Insert Mode and return to Normal Mode by pressing the "Esc" key.

  3. Visual Mode:

    In Visual Mode, you can visually select blocks of text for copying, cutting, or other operations. You can enter Visual Mode from Normal Mode by pressing "v" key.

  4. Command-Line Mode:

    In Command-Line Mode, you can enter commands that affect the whole file, like saving changes, searching, and replacing text, and more. You can enter Command-Line Mode from Normal Mode by pressing ":" key.

Navigation in vim Text Editor:

  • h: Move cursor left.

  • j: Move cursor down.

  • k: Move cursor up.

  • l: Move cursor right.

  • w: Move cursor forward one word.

  • b: Move cursor backward one word.

  • $: Move cursor to the end of the line.

  • 0 (zero): Move cursor to the beginning of the line.

  • gg: Move cursor to the beginning of the file.

  • G: Move cursor to the last line of the file.

  • [line number]G: Move cursor to a specific line number. e.g. 2G : Move cursor to line 2.

Editing in vim Text Editor:

  • x: Delete character under the cursor

  • dd: Delete current line

  • dw: Delete from cursor to the beginning of the next word

  • yy: Copy current line

  • p: Paste copied or deleted text after the cursor

  • P: Paste copied or deleted text before the cursor

  • u: Undo last change

  • . : Redo last change

Search and Replace in vim Text Editor:

  • /[pattern]: Search forward for a pattern

  • ?[pattern]: Search backward for a pattern

  • n: Repeat last search in the same direction

  • N: Repeat last search in the opposite direction

  • :s/[pattern]/[replacement] : Replace the first occurrence of pattern with replacement in the current line

  • :%s/[pattern]/[replacement]/g : Replace all occurrences of pattern with replacement in the entire file

Saving and Quitting in vim Text Editor:

  • :w : Save changes (write)

  • :q : Quit vi

  • :q! : Quit vi without saving changes (force quit)

  • :wq or ZZ : Save changes and quit

  • :w [filename]: Save changes to a file

  • :e [filename]: Open a file for editing

Insert Mode in vim Text Editor:

  • i: Enter insert mode before the cursor

  • I: Enter insert mode at the beginning of the line

  • a: Enter insert mode after the cursor

  • A: Enter insert mode at the end of the line

  • o: Open a new line below the current line and enter insert mode

  • O: Open a new line above the current line and enter insert mode

Visual Mode in vim Text Editor:

  • v: Enter visual mode to visually select characters

  • V: Enter visual mode to visually select whole lines

  • Ctrl + v: Enter visual block mode to visually select rectangular blocks