DEV Community

Željko Šević
Željko Šević

Posted on • Originally published at sevic.dev on

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 update
sudo apt-get install -y vim
vim
Enter fullscreen mode Exit fullscreen mode

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 plus Enter to run the command

Usage

Shortcuts

  • Esc - go back to Normal mode
  • h to scroll left
  • j to scroll down
  • k to scroll up
  • l to scroll right
  • Shift + g - scroll to the end of the file
  • g + g - scroll to the beginning of the file
  • line 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 line
  • w - move to the next word
  • b - 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 type p
    • type y + number of lines + y to copy specified lines and paste it with p
  • Deleting
    • enter the visual mode, scroll through the text you want to delete, and type d
    • type d + number of lines + d to delete specified lines
    • type x to remove the letter
    • type dw to remove the word (and the space after it)
  • Type u to undo the previous change
  • Type CTRL + r to redo the previous undo
  • Find specific text with /<text> like /vim and press Enter. Type n to go to the next occurrence and N to 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
Enter fullscreen mode Exit fullscreen mode

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, required
filetype off                  " required

call 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 line
call plug#end()              " required
filetype plugin indent on    " required

"---------AIRLINE SETTINGS------
let g:airline_powerline_fonts = 1
let g:airline_theme='solarized'

"-----COMMENTARY SETTINGS-------
noremap <leader>/ :Commentary<cr>

"-----PRETTIER SETTINGS---------
let g:prettier#autoformat = 1
let g:prettier#autoformat_require_pragma = 0

"------------TABS---------------
set expandtab
set tabstop=2
set shiftwidth=2
set softtabstop=2
" makefile tabs
autocmd FileType make setlocal noexpandtab
" tab completion
set wildmenu

" line numbers
set number
set relativenumber

syntax on

" theme
colorscheme molokai
let g:solarized_termcolors=256
set background=dark

" indention
set autoindent

" highlight found words
set hlsearch

" press left/right and move to the previous/next line after reaching the first/last character in the line
set whichwrap+=<,>,h,l,[,]

" long lines
nnoremap k gk
nnoremap j gj

" disable arrow keys in normal mode
map <Left> <Nop>
map <Right> <Nop>
map <Up> <Nop>
map <Down> <Nop>

" toggling paste mode
set pastetoggle=<F2>

" last command
set showcmd

" disable swap files and backups
set noswapfile
set nobackup
set nowritebackup

" mouse click navigation
set mouse=a
Enter fullscreen mode Exit fullscreen mode

Further learning

Try vimtutor with the following command to dive deep into Vim features.

vimtutor
Enter fullscreen mode Exit fullscreen mode

Top comments (0)