Summary: Vim

A brief introduction to Vim

  • Vim is a text editor, which can be used from a command-line interface
  • Vim can be used completely without the mouse
  • Vim is available on a lot of UNIX-like operating systems per default
  • Vim keybindings and functionality can likely be emulated in your IDE with a plugin

Installation

  • Install Vim with your system's package manager if you are on an UNIX-like OS
  • Install Vim with gitforwindows if you use windows

Modes

  • To switch from normal mode to insert mode, press the i key
  • To switch from insert mode (or any other mode) to normal mode, press the ESC key

Open, save and quit

  • Type :w followed by the ENTER key to write to the file opened
  • Type :q followed by the ENTER key to close/quit the file opened (you will be notified if there are unsaved changes)
  • Type :q! followed by the ENTER key to close/quit the file opened without saving
  • Type :wq or :x followed by the ENTER key to both write and quit
  • Alternatively you can also press ZZ to write and quit

Navigation

  • To navigate in Vim, use the normal mode
  • Don't use the arrow keys to navigate, because that way your fingers have to leave the home row of the keyboard

Vim cheatsheet

  • h to move the cursor one character to the left (equivalent of )
  • l to move the cursor one character to the right (equivalent of )
  • k to move the cursor one line up (equivalent of )
  • j to move the cursor one line down (equivalent of )

  • Press w (lowercase) to place the cursor at the beginning of the next word (Note that e.g. - counts as a separator)

  • Press W (uppercase) to place the cursor at the beginning of the next WORD (Note that here a word counts as anything as long as it is not separated by a space character)

  • Press b (lowercase) to place the cursor at the beginning of the previous word (Note that e.g. - counts as a separator)

  • Press B (uppercase) to place the cursor at the beginning of the previous WORD (Note that here a word counts as anything as long as it is not separated by a space character)

  • Press e (lowercase) to place the cursor at the end of the current word (Note that e.g. - counts as a separator)

  • Press E (uppercase) to place the cursor at the end of the current WORD (Note that here a word counts as anything as long as it is not separated by a space character)

  • Press 0 to place the cursor at the beginning of the line

  • Press ^ to place the cursor at the first non-space-character of the line

  • Press $ to place the cursor at the end of the line

  • Press :10 followed by the ENTER key to jump to the 10th line of the file

  • Press gg to place the cursor at the first line of the file

  • Press G to place the cursor at the last line of the file (the number after the colon determines to which line you jump)

  • Press H to place the cursor at the first line visible on the screen (H for High)

  • Press M to place the cursor at the center line of the visible screen (M for Middle)

  • Press L to place the cursor at the last line visible on the screen (L for Low)

Scrolling

  • Press CTRL+E to scroll down by one line
  • Press CTRL+Y to scroll up by one line
  • Press CTRL+F to scroll one whole screen down
  • Press CTRL+B to scroll one whole screen up
  • Press CTRL+D to scroll a half screen down
  • Press CTRL+U to scroll a half screen up

Searching

  • Type / followed by a phrase followed by pressing the ENTER key to search for that phrase
  • Special characters like * have to be escaped with a bashslash (\)
    • E.g. to search for /*, type in /\/\* followed by pressing the ENTER key

  • Press n to jump to the next occurrence of the phrase

  • Press N to jump to the previous occurrence of the phrase

Commands

  • Commands can be executed in the runtime of Vim by pressing : followed by the command
  • Commands can also be persisted by writing them into the ~/.vimrc file

Runtime-only commands

  • Some commands can only be executed in runtime, because it makes no sense to write them in the config file
    • We already covered :w, :q, :q!, :wq and :x
    • edit [filename] and e [filename] opens filename relative to the directory, you opened Vim in
    • w [filename] you can save the currently opened file to a new or an already existing but different file by specifying a filename

More commands

  • syntax on enables syntax highlighting
  • set number enables line numbers
  • set relativenumber enables relative line numbers to where the cursor is currently at (can be used together with set number)
  • set ignorecase disables case sensitive searching
  • set smartcase enables case sensitive searching if the phrase you are searching for has uppercase letters in it
  • tabs can be configured with commands too
  • hlsearch highlights the phrase your searched for
  • nohlsearch removes the highlighting of the searched phrase

Indenting and unindenting

  • The cursor does not have to be at the beginning of the line
  • Press >> to indent the line, the cursor sits on
  • Press << to unindent the line, the cursor sits on

Manipulation

  • Manipulations are performed in normal mode
  • Think about speaking a language or telling Vim in a sentence, what to do (with a verb, modifier and noun)

Verbs

  • Determines what action will be performed
  • Change (key c): Deletes the desired part of the file (specified by modifier and noun) and then switches to insert mode
  • Delete (key d): Deletes the desired part of the file (specified by modifier and noun) and stays in normal mode
  • Yank (key y): Yanks/Copies the desired part of the file (specified by modifier and noun) and stays in normal mode

Modifier

  • Determines in what scope the action will be performed
  • Inside (key i): Performs the action inside the given noun
  • Around (key a): Performs the action around the given noun
  • Till (key t): Performs the action until (not including) the given character
  • /: Performs the action until (not including) the given phrase

Nouns

  • Determines on what element the action will be performed on
  • All navigation keys can be used as noun
  • Word (key w): Performs the action on the word, the cursor currently sits on
  • Tag (key t): Performs the action on the tag, the cursor currently sits on
  • ', ", <: Performs the action on the previous (<) and the next (>) symbol to the cursor

Examples

  • Change Inside Word (ciw): Deletes the word, the cursor currently sits on and then switches to insert mode (can also be used with a uppercase W - the behaviour is the same as in the navigation section)
  • Delete Around ' (da'): Deletes everything between (including because around) the previous and the next occurrence of '
  • Delete Till l (dtl): Deletes everything between (including) the cursor and the beginning of the next occurrence of the character l
  • Yank / foo (y/foo): Yanks everything between (including) the cursor and the next occurrence of the phrase foo
  • Delete Inside Tag (dit): Deletes everything inside of the Tag, the cursor is currently inside of
  • Delete $ (d$): Deletes everything between (including) the cursor and the end of the line the cursor sits on

More Manipulations

  • Press dd to delete the whole line
  • Press cc to change the whole line
  • Press yy or Y to yank the whole line
  • Press D to delete from the cursor until the end of the line
  • Press C to change from the cursor until the end of the line
  • Press x to delete the character under the cursor

Copy & Paste

  • Press p, to Paste after the cursor
  • Press P, to Paste before the cursor

Use numbers

  • Use numbers as modifiers
  • Press 5l to move the cursor 5 times to the right

The dot command

  • By pressing . the previous action will be performed again

Search and replace

  • :%s/foo/bar/g searches for all occurrences of foo and replaces them with bar in the file

Visual mode

  • Visual modes let you visually select something and then perform an action on the selection
  • Enter visual mode by pressing v
  • Enter linewise visual mode by pressing V
  • Enter visual block mode by pressing CTRL+v
  • Exit any visual mode by pressing ESC (press twice to skip the delay)
  • You can indent multiple lines at once by selecting them with a visual mode

Special manipulations

  • J to join the line, the cursor is currently on with the following into one line (separated with a space character)
  • r followed by any character to replace the character, the cursor is sitting on
  • R to switch into replace / overwrite mode which enables you to replace multiple successive characters at and after your cursor (press ESC to get back to normal mode)

More possibilities to join insert mode

  • a switch into insert mode and place the cursor after the current character (Append)
  • A switch into insert mode and place the cursor at the end of the line
  • o create an empty line after the one, the cursor currently sits on, switch into insert mode and place the cursor at the beginning of the new line
  • O create an empty line before the one, the cursor currently sits on, switch into insert mode and place the cursor at the beginning of the new line
  • I switch into insert mode and place the cursor at the first character of the line

Undo and redo

  • Press u to undo the most recent action
  • Press CTRL+r to redo the changes

Macros

  • Record a macro by pressing q[register][actions_to_record]q e.g. qadwq
  • Play that macro by pressing @[register] e.g. @a
  • Play a macro multiple times by adding a number infront of it e.g. 3@a

Key mapping

  • map creates a key map which then is available in most modes
  • nmap creates a key map which then is available in normal mode
  • imap creates a key map which then is available in insert mode
  • xmap creates a key map which then is available in visual mode
  • noremap creates a (non-recursive) key map which then is available in most modes
  • nnoremap creates a (non-recursive) key map which then is available in normal mode
  • inoremap creates a (non-recursive) key map which then is available in insert mode
  • xnoremap creates a (non-recursive) key map which then is available in visual mode
  • Non-printable characters can be used aswell but have a special notation:
Command Explaination
<BS> Backspace
<Tab> Tab
<CR> Enter
<Enter> Enter
<Return> Enter
<Esc> Escape
<Space> Space
<Up> Up arrow
<Down> Down arrow
<Left> Left arrow
<Right> Right arrow
<F1> - <F12> Function keys 1 to 12
<Insert> Insert
<Del> Delete
<Home> Home
<End> End
<PageUp> Page-Up
<PageDown> Page-Down
<bar> The Pipe character
<C-n> CTRL+n
  • nmap <Space>w :w<CR> creates a key map to write the file on pressing SPACE+w

Mapleader

  • Define a mapleader by adding let mapleader=" " to your config
  • Use that mapleader like this: :nnoremap <leader>w :w<CR>

Color schemes

  • Type in :colorscheme, then press CTRL+d to see available color schemes
  • Type in : colorscheme darkblue, to activate the darkblue color scheme (or add colorscheme darkblue to your config)
  • Download a color scheme (e.g. https://vimcolors.com/) and place the .vim file in ~/.vim/colors/
  • This color scheme will now be available to use in Vim

Plugins

  • To install vim-pathogen (plugin manager):
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  • Then add the line execute pathogen#infect() to your ~/.vimrc

ctrlp.vim

  • To install ctrlp.vim
cd ~/.vim
git clone https://github.com/kien/ctrlp.vim.git bundle/ctrlp.vim
  • Now run Vim and execute the command: :Helptags
  • By pressing CTRL+p, ctrlp.vim will open up

NERDTree

  • To install NERDTree
git clone https://github.com/scrooloose/nerdtree.git ~/.vim/bundle/nerdtree
  • Now run Vim and execute the command: :Helptags
  • Enter the command :NERDTreeToggle to toggle *NERDTree
  • Navigate with Vim navigations in NERDTree, open a file by pressing ENTER
  • map <C-n> :NERDTreeToggle<CR> will set a key mapping to open NERDTree on pressing CTRL+n

Use the systems clipboard

  • Execute vim --version | grep clipboard
    • If you are on macOS, make sure the output includes +clipboard
    • If you are on linux, make sure the output includes +xterm_clipboard

  • Install another Vim version with your systems package manager if it doesn't

  • Add set clipboard=unnamed to your ~/.vimrc to make Vim use the systems clipboard

Comments in the config

" This is a comment

My Vim config

" pathogen initialization
execute pathogen#infect()

" syntax highlighting
syntax on

" use numbers
set number
set relativenumber

" searching
set ignorecase
set smartcase
set hlsearch

" use system clipboard
set clipboard=unnamed

" tabs
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab

" key maps
let mapleader=" "
map <C-n> :NERDTreeToggle<CR>
nnoremap <leader>w <esc>:w<CR>
nnoremap <leader>q <esc>:q<CR>
nnoremap <leader>Q <esc>:q!<CR>
nnoremap <leader>n <esc>:nohlsearch<CR>

" colorscheme
colorscheme atom