Vim overview
Vim is a text editor known for editing files without using a mouse. It's also useful when you SSH into a remote server, and you have to edit some files there. This post covers the main notes from installation to its usage (shortcuts, commands, configuration).
Installation
Vim is usually already installed on mostly *nix operating systems. You can install it via the package manager and open it with the following commands.
sudo apt-get updatesudo apt-get install -y vimvim
Modes
Vim has four modes
- Normal - this is the default mode. It enables scrolling through the file content
- Visual - type
v, and you can select text content for deleting and copying with scrolling shortcuts - Insert - type
i, and you can start editing the file content - Command-line - type
:and some command plusEnterto run the command
Usage
Shortcuts
Esc- go back to Normal modehto scroll leftjto scroll downkto scroll uplto scroll rightShift+g- scroll to the end of the fileg+g- scroll to the beginning of the fileline number+Shift+g, (e.g.,5+Shift+g) - jump to the specific line number, 5th in this case^- jump to the start of the current line$- jump to the end of the current linew- move to the next wordb- move back to the previous word
Commands
:edit script.js- create a new file or open the existing one:w- save the changes:q- exit the file:wq- save the changes and exit the file:q!- exit without the changes:%s/<text>/<new text>/g- find and replace the occurrences within the whole file (e.g.,:%s/Vim/Emacs/g):+ ↑ - to find the previous command
Miscellaneous
- Copy pasting
- enter the visual mode, scroll through the text you want to copy, type
y, then scroll to the place you want to paste it and typep - type
y+ number of lines +yto copy specified lines and paste it withp
- enter the visual mode, scroll through the text you want to copy, type
- Deleting
- enter the visual mode, scroll through the text you want to delete, and type
d - type
d+ number of lines +dto delete specified lines - type
xto remove the letter - type
dwto remove the word (and the space after it)
- enter the visual mode, scroll through the text you want to delete, and type
- Type
uto undo the previous change - Type
CTRL+rto redo the previous undo - Find specific text with
/<text>like/vimand pressEnter. Typento go to the next occurrence andNto the previous one
Configuration
Vim configuration is stored in ~/.vimrc file. You can specify plugins and other configurations, like theme, tabs spaces, etc.
To use plugins, install the vim-plug plugin manager with the following command
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
and run the :PlugInstall command.
Check the status of the plugins with the :PlugStatus command.
Below is the configuration I use.
"------PLUGINS SETTINGS---------set nocompatible " be iMproved, requiredfiletype off " requiredcall plug#begin('~/.vim/plugged')Plug 'vim-airline/vim-airline'Plug 'vim-airline/vim-airline-themes'Plug 'Raimondi/delimitMate'Plug 'flazz/vim-colorschemes'Plug 'prettier/vim-prettier', { 'do': 'npm install' }Plug 'tpope/vim-commentary'" All of your Plugins must be added before the following linecall plug#end() " requiredfiletype plugin indent on " required"---------AIRLINE SETTINGS------let g:airline_powerline_fonts = 1let g:airline_theme='solarized'"-----COMMENTARY SETTINGS-------noremap <leader>/ :Commentary<cr>"-----PRETTIER SETTINGS---------let g:prettier#autoformat = 1let g:prettier#autoformat_require_pragma = 0"------------TABS---------------set expandtabset tabstop=2set shiftwidth=2set softtabstop=2" makefile tabsautocmd FileType make setlocal noexpandtab" tab completionset wildmenu" line numbersset numberset relativenumbersyntax on" themecolorscheme molokailet g:solarized_termcolors=256set background=dark" indentionset autoindent" highlight found wordsset hlsearch" press left/right and move to the previous/next line after reaching the first/last character in the lineset whichwrap+=<,>,h,l,[,]" long linesnnoremap k gknnoremap j gj" disable arrow keys in normal modemap <Left> <Nop>map <Right> <Nop>map <Up> <Nop>map <Down> <Nop>" toggling paste modeset pastetoggle=<F2>" last commandset showcmd" disable swap files and backupsset noswapfileset nobackupset nowritebackup" mouse click navigationset mouse=a
Further learning
Try vimtutor with the following command to dive deep into Vim features.
vimtutor