2. vi/vim: Text Editing#
vi/vimare both text editors for the CLIviis older but found on most systemsvimisviimproved with added functionalitiesBoth are similar to GUIs like Notepad, TextEdit, etc…
2.1. Overview#
Run by typing
viorvimfollowed by a filenameTo exit, press
escthen:qor:q!Additional commands are shown below:
# Commands to manage files
    vi <filename>     # Open <filename> in vim
    :help <topic>     # Open up built-in help docs about <topic> if any exists
    :q                # Quit vim
    :q!               # Quit vim without saving file
                      #     ! *forces* :q to execute, hence quitting vim without saving
    :w                # Save current file
    :wq               # Save file and quit vim
    :x                # Save file(only when the file is modified) and quit vim
2.2. Optional Text Editing Modes#
There are several “mode” options when using
viorvimNormal Mode - used for editor commands
This is generally the default mode and by default hitting
escreturns the editor to this mode
Insert Mode - used for typing text
Opened text in buffers can be modified with the text entered from the keyboard by hitting
ikey
Visual Mode - used to select areas of text
Commands can be run on the selected area – moving, editing, filtering
Visual linewise - selects one or more whole lines by hitting
crtl (^)+cVisual blockwise - selects a rectangular block of text across one or more lines by hitting
crtl (^)+v
Command-Line Mode
Run
vi/vimcommands by hittingescthen:
When switching between modes:
Make sure you are in Normal Mode by pressing esc. You should no longer see insert on the bottom left of the terminal!
2.3. Key Bindings Commands (Normal Mode)#
Key or combination of keys on your keyboard can be assigned (bound) with a command
Several default key bindings are:
# File Parsing
    gg                # Go to the top of the file
    G (shift + g)     # Go to the bottom of the file
    H                 # Move to the top of the screen
    M                 # Move to the middle of the screen
    L                 # Move to the bottom of the screen
    h                 # Move left one character
    j                 # Move down one line
    k                 # Move up one line
    l                 # Move right one character
    o                 # Make new line below cursor
    O                 # Make new line above cursor
# Undo/Redo
    u                 # Undo
    crtl (^) + R      # Redo
# Search for PATTERN - can be any string
    /PATTERN          # Highlights all occurrences of PATTERN after cursor
    ?PATTERN          # Highlights all occurrences of PATTERN before cursor
    n                 # Moves cursor to next occurrence of PATTERN after search
    N                 # Moves cursor to previous occurrence PATTERN 
# Scrolling
    ctrl (^) + d      # Scroll half page down
    ctrl (^) + u      # Scroll half page up
    ctrl (^) + f      # Scroll one page forward
    ctrl (^) + b      # Scroll one page backwardc
2.4. Command Line Mode#
To enter this mode, press
escand then:The cursor to move at the bottom of the window in the command box
You can then write any command you like and press enter to execute it
# File Parsing
    :NUMBER           # Go to line number, NUMBER (i.e. line 10 = :10)
# Replace PATTERN - like sed command
    :%s/foo/bar/g     # Change 'foo' to 'bar' on every line in the file
    :s/foo/bar/g      # Change 'foo' to 'bar' on the current line
    :%s/\n/\r/g       # Replace new line characters with new line characters
    :'<,'>s/foo/bar/g # Change 'foo' to 'bar on every line in the current visual
2.5. Visual Configuration#
:set number           # prefix line numbers (it is a visual guideline, won't modify text)
:set nonumber         # remove line number prefix
:set number!          # toggle number setting