Skip to content

Latest commit

 

History

History
180 lines (126 loc) · 5.87 KB

1. Vim Editor Basics.md

File metadata and controls

180 lines (126 loc) · 5.87 KB

Vim Editor Basics

Vim is a highly configurable and powerful text editor that is an enhanced version of the older Vi editor. It is commonly used in Unix-like operating systems, and mastering Vim can significantly boost your productivity when editing text or code.

Understanding Vim's Modes

Vim operates in several modes, each designed for different tasks:

  • Normal Mode: This is the default mode for navigating and manipulating text. Most of Vim's powerful commands are executed in this mode.
  • Insert Mode: In this mode, you can insert or modify text. Enter this mode by pressing i, I, a, A, o, or O.
  • Visual Mode: Used for selecting text. You can enter visual mode by pressing v for character-wise selection or V for line-wise selection.
  • Command-Line Mode: Accessed by pressing : in Normal Mode, this mode allows you to execute more complex commands like saving, quitting, and searching.

Switching Between Modes

  • Normal Mode: Press Esc to switch to Normal Mode from any other mode.
  • Insert Mode: Press i to enter Insert Mode.
  • Visual Mode: Press v to enter Visual Mode.
  • Command-Line Mode: Press : to enter Command-Line Mode.

Basic Vim Commands

Opening Vim

To start editing a file with Vim:

vim filename

If the file does not exist, Vim will create it upon saving.

Navigating in Normal Mode

  • h, j, k, l: Move the cursor left, down, up, and right, respectively.
  • w: Move the cursor to the beginning of the next word.
  • b: Move the cursor to the beginning of the previous word.
  • 0: Move the cursor to the beginning of the line.
  • $: Move the cursor to the end of the line.
  • gg: Move to the beginning of the file.
  • G: Move to the end of the file.
  • Ctrl+f: Scroll down one screen.
  • Ctrl+b: Scroll up one screen.

Inserting Text

  • i: Insert before the cursor.
  • I: Insert at the beginning of the line.
  • a: Insert after the cursor.
  • A: Insert 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.

Editing Text

  • x: Delete the character under the cursor.
  • dw: Delete from the cursor to the start of the next word.
  • dd: Delete the entire line.
  • u: Undo the last change.
  • Ctrl+r: Redo the undone change.
  • p: Paste text after the cursor.
  • y: Yank (copy) text.
  • yy: Yank the current line.
  • P: Paste text before the cursor.

Saving and Quitting

  • :w: Save the file (write changes).
  • :q: Quit Vim (close the file).
  • :wq: Save and quit.
  • :q!: Quit without saving changes.
  • :x: Save and quit (same as :wq).

Searching and Replacing

  • /pattern: Search forward for "pattern".
  • ?pattern: Search backward for "pattern".
  • n: Move to the next occurrence of the search pattern.
  • N: Move to the previous occurrence of the search pattern.
  • :%s/old/new/g: Replace all occurrences of "old" with "new" in the entire file.
  • :s/old/new/g: Replace all occurrences of "old" with "new" in the current line.

Advanced Vim Tips

Buffers, Windows, and Tabs

  • :e filename: Open a new file in the current buffer.
  • :bnext or :bn: Switch to the next buffer.
  • :bprev or :bp: Switch to the previous buffer.
  • :split filename or :sp filename: Open a file in a new split window.
  • :vsplit filename or :vsp filename: Open a file in a new vertical split window.
  • Ctrl+w: Switch between split windows.
  • :tabnew filename: Open a file in a new tab.
  • gt: Move to the next tab.
  • gT: Move to the previous tab.

Working with Registers

Vim allows you to store text in registers for more complex copy-paste operations.

  • "ayy: Yank a line into register "a".
  • "ap: Paste the contents of register "a".
  • :reg: Display the contents of all registers.

Macros

You can record a sequence of commands and play them back.

  • q followed by a letter: Start recording a macro into that register.
  • q: Stop recording.
  • @ followed by a letter: Play back the macro stored in that register.
  • @@: Replay the last macro.

Customizing Vim

Vim is highly customizable through the ~/.vimrc file. You can add settings, key mappings, and plugins to tailor Vim to your workflow.

Example .vimrc

" Enable line numbers
set number

" Enable syntax highlighting
syntax on

" Set tabs to 4 spaces
set tabstop=4
set shiftwidth=4
set expandtab

" Enable search highlighting
set hlsearch

" Set file encoding to UTF-8
set encoding=utf-8

Installing Plugins

Vim supports plugins, which can be managed manually or with plugin managers like Vundle or Pathogen.

Example using Vundle:

  1. Add this to your .vimrc:

    set nocompatible              " Be iMproved, required
    filetype off                  " Required
    
    " Set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()
    
    " Let Vundle manage Vundle, required
    Plugin 'VundleVim/Vundle.vim'
    
    " Add more plugins here
    Plugin 'tpope/vim-sensible'
    
    " All of your Plugins must be added before the following line
    call vundle#end()            " Required
    filetype plugin indent on    " Required
  2. Install the plugins:

    vim +PluginInstall +qall

Conclusion

Vim is a powerful and flexible text editor that, once mastered, can significantly enhance your text editing efficiency. While the learning curve can be steep, the investment in learning Vim pays off in speed and productivity.


Next: Advanced Vim Usage

Previous: Pacman (Arch)