Software Engineer's Blog

Vim Setup with vim-plug on Ubuntu

Vim Setup with vim-plug on Ubuntu

Vim 8+ has native package loading (:packadd), but no built-in way to download and update plugins — so the first thing you set up on a new Ubuntu box is a plugin manager. vim-plug is a lightweight single-file manager (one plug.vim script, plus Git for cloning plugins), which makes it a quick path from a bare vim to a working editor — a common first setup on a fresh machine.

Four steps: create the working directories, install vim-plug, write a .vimrc, then install the plugins.

Step 1: Create Necessary Directories

Vim can keep backup, swap, and undo files. Swap files land next to what you’re editing by default, and the .vimrc below also turns on backups and persistent undo — so point all three at dedicated directories to keep those files out of your project folders. The config sets backupdir, directory, and undodir to exactly these paths, and Vim won’t create the directories for you:

$ mkdir -p ~/.vim/backup ~/.vim/swap ~/.vim/undo

Step 2: Install vim-plug

Download the plug.vim script from the vim-plug GitHub repository and save it to the ~/.vim/autoload/ directory.

$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Step 3: Configure .vimrc

Copy the following settings into the ~/.vimrc file in your Linux (Ubuntu) user account.

" Basic settings

set hlsearch " Highlight search results
set nu " Show line numbers
set autoindent " Enable automatic indentation
set scrolloff=2
set wildmode=longest,list
set tabstop=4 " Set tab width
set shiftwidth=4 " Set indentation width
set expandtab " Use spaces instead of tabs
set smartindent " Enable smart indentation
set cindent " Enable C-style indentation
set smartcase " Case-insensitive search unless uppercase is used
set incsearch " Show matching results while typing search query
set backspace=eol,start,indent " Enable backspace in insert mode
set history=256 " Set command history length
set laststatus=2 " Always display the status line
set showmatch " Highlight matching parentheses

" File encoding settings

set encoding=utf-8
set fileencoding=utf-8
set fileencodings=ucs-bom,utf-8,cp949,latin1

" UI settings

set ruler " Show the cursor position
set statusline=%<%l:%v\ [%P]%=%a\ %h%m%r\ %F\
colorscheme codeschool " Set color scheme

" Backup and undo settings

set backup " Enable backup file creation
set undofile " Save undo history to file
set backupdir=~/.vim/backup// " Set backup file directory
set directory=~/.vim/swap// " Set swap file directory
set undodir=~/.vim/undo// " Set undo file directory

" Enable syntax highlighting

if has("syntax")
  syntax on
endif

" Locale settings

let $LANG = 'ko_KR.UTF-8'

That last line sets my locale to Korean — change it to your own (en_US.UTF-8, etc.) or drop it entirely. One thing this base config doesn’t have yet is a plugin list, so read on before running :PlugInstall.

Declaring plugins

vim-plug only installs what you list between a plug#begin() / plug#end() block. Add one near the top of your .vimrc — without it, :PlugInstall has nothing to do. A minimal, widely used starter set:

call plug#begin('~/.vim/plugged')

Plug 'preservim/nerdtree'                            " file tree sidebar (:NERDTree)
Plug 'itchyny/lightline.vim'                         " lightweight status line
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }  " the fzf binary + base plugin
Plug 'junegunn/fzf.vim'                              " the Vim commands (:Files, :Rg)

call plug#end()

Note the colorscheme codeschool line in the config above assumes that colorscheme exists. If Vim complains with E185: Cannot find color scheme, either add a colorscheme plugin (e.g. Plug 'morhetz/gruvbox' and colorscheme gruvbox) or switch to a built-in one like desert or habamax.

Step 4: Install Plugins

Open Vim and install the plugins by running the following command:

$ vim +PlugInstall +qall

The command “vim +PlugInstall +qall” is used to open Vim and automatically install the plugins specified in the .vimrc file using vim-plug.

+PlugInstall

  • This is an Ex command passed to Vim when it starts up.
  • PlugInstall is a command provided by vim-plug that installs all plugins listed in the .vimrc file.

+qall

  • This is another Ex command that tells Vim to quit all open windows.
  • After installing the plugins with PlugInstall, +qall will close Vim.

Verify the setup

Open Vim normally and run :PlugStatus — each plugin from your plug#begin block should report OK. If :NERDTree opens a file sidebar, the manager is wired up correctly. If :PlugInstall instead prints Nothing to install, you’re missing the plug#begin() / plug#end() block from Step 3 — a common reason a “vim-plug setup” ends with no plugins actually installed.