Vim Tips

Posted on with tags: vim / tip / linux

Ubuntu 18.04 comes with a stripped down version of Vim. If you want to use Vim for serious work, you want the full version.

Vim Installation

The default version is started via command vi. The vi --version command shows the version information. Note the Line 5 below shows “Small version without GUI”. The output also contains the list of setting files Vim will load during startup.

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Apr 10 2018 21:31:58)
Included patches: 1-1453
Modified by
Compiled by
Small version without GUI.  Features included (+) or not (-):
+acl               -extra_search      -mouse_netterm     -tag_old_static
-arabic            -farsi             -mouse_sgr         -tag_any_white
+ex_extra          -mouse_jsbterm     +tag_binary 
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -g -O2 
-fdebug-prefix-map=/build/vim-NQEcoP/vim-8.0.1453=. -fstack-...

If you type command vim in bash, it will tell you that “command vim not found”. Those three commands remove the existing vim-tiny and install the full version with GUI vim-gtk3 in Ubuntu 18.04 or Xubuntu.

sudo apt-get remove vim-tiny
sudo apt-get update
sudo apt-get install vim-gtk3

You can then check the vim version by command vim --version.

Basic Vim Settings

Vim automatically loads ~/.vimrc file during startup. Some default settings of vim do not make sense. Below are some most basic vim settings.

" ~/.vimrc file

set nocompatible              " required
filetype plugin on
syntax enable

"path and find, fuzzy file finder
set path+=**  " search subdir recursively, find ...
set wildmenu

set number

" Set ignore case, highlight, and incremental searches
set ignorecase
set hlsearch
set incsearch

if exists("+lines")
  set lines=32
if exists("+columns")
  set columns=108

" Ctrl + s to save file
noremap <silent> <C-S>  :update<CR>
vnoremap <silent> <C-S> :<C-C>:update<CR>
inoremap <silent> <C-S> <C-O>:update<CR>

"shortcut ^l to mute highlighting
nnoremap <silent> <C-l> :<C-u>nohlsearch<CR><C-l>

Vim Tips


Vim loads ~/.vimrc by default, and you can change the behavior by using -u option on command line.

$vim -u ~/.simple.vimrc filename
$vim -u NONE  # do not load any config file

Set Initial Console Window Size

On Linux terminal, you can use set lines=50 columns=100 to set initial console size. I have those lines in my ~/.vimrc file.

if exists("+lines")
  set lines=32
if exists("+columns")
  set columns=108

Source: an article on

Fuzzy Find

The following two settings help the :find command to do file fuzzy finding.

set path+=**
set wildmenu
:find *cache # Press Tab key to find file name with cache

Source: a video talk on youtube.

Auto Complete

Vim itself has auto complete function built in. In the insert mode, you can type a few letters such as ‘Com’, and then press ^n to bring up auto complete menu. It is very handy when you are programming because you often need to type variable and class names multiple times on a file.

search in this file, ^ represents Ctrl key
search filename
search tag
search by default
^n ^p
next prev on the menu
confirm selection, yes
exit menu

Current Filename

Command ^g shows the name of the current file. Or you can type the following vim commands.

:echo @%
:!ls %:p  # will show absolute path of file


Keys h j k l are the basic navigation commands in normal mode. Commands w b e jump cursor to next word, begin of word, or end of word. Other common navigation commands are listed below.

move cursor to top
move cursor to bottom
jump cursor to line number n, or <n>G
show an extra line
opposite of ^e
move down half screen
move up half screen
matching ( { or [
H, M, L
move cursor to top, middle, and lower corner
go to old cursor position
go to next cursor position
move down one displayed line when a long line is auto wrapped

Common Shortcuts

Vim is in a different league comparing to other text editors. It does not have some common shortcuts other editors have. But some of those shortcuts are so common, it is better to customize vim to support them. For example, the command :saveas is sometimes very convenient.

The settings below add Ctrl+s shortcut for saving file. Command :update is like :write, but only write when buffer has been modified.

noremap <silent> <C-S>  :update<CR>
vnoremap <silent> <C-S> :<C-C>:update<CR>
inoremap <silent> <C-S> <C-O>:update<CR>

This shortcut also needs a setting in .bashrc file to work.

stty -ixon  # stop tele-typewriter, 
            # -ixon enable xon/xoff flow control -i

By default command ^s freezes vim in Ubuntu Linux, and ^q un-freeze it. This stackoverflow Q&A explains it very well.

Ctrl + s is a common command to terminals to stop updating, it was a way to slow the output so you could read it on terminals that did not have a scrollback buffer. Ctrl + q command gets terminal going again. Put this line in .bashrc to disable flow control for terminal entirely.

Spell Check

Vim has a built in spell checker. You use set spell to turn it on and set nospell to turn it off. Here are some other commands on how to use it.

setlocal spell
for current buffer only
move to next misspelled word
previous misspelled word
cursor on the word, list of suggested words
the word is correct, add it to personal dictionary

The spelllang settings is to specify the language, and the default is en. You can set it to en_us to specify American English. You can also use spellfile setting to specify a personal dictionary file for zg command.

Source: an article on & a blog post by Jake Harding.

GNOME Terminal Shortcuts

The Linux Mint comes with Gnome terminal. You can open additional tabs by pressing shortcut Ctrl + Shift + t, and move to next tab by pressing Ctrl + PageDown. This does not necessarily relate to Vim, but I often use them so they are listed here.

Another useful terminal shortcut is Ctrl + z which puts current application Vim in the background and suspended. After running some bash commands, you can type fg command to bring Vim back to foreground. Or you can use :! followed by a bash command to execute the command inside Vim.

Copy and Paste via Clipboard

Copying texts to and from Vim using the Ctrl + c and Ctrl + v does not work. The command ^c is to generate a signal which tells the current process to terminate, and command ^v invokes “verbatim insert” in bash (see an online article) . You could use Gnome terminal shortcuts Ctrl + Shift + c and Ctrl + Shift + v, but sometimes it does not work very well.

I often use the clipboard register ("+) to copy and paste texts in and out of Vim. Here are the steps to copy texts into Vim,

  1. Copy texts in another program such as Firefox.
  2. Alt + Tab switch focus to Vim.
  3. Use command "+p to paste the texts.

Here are the steps to copy texts out of Vim to another program.

  1. Use v command to visually select texts in Vim.
  2. Type command "+y to copy the texts to clipboard.
  3. In another program, use ^v to paste the texts.

There is an online Q&A on how to map ^c and ^v to copy and paste behavior, but I have not set it up in my .vimrc file.

Work Sessions

When you have 10 or more text files open in Vim and need to reboot your computer, you can use session commands to save the Vim work status and load them later.

:mksession ~/work1.session
save the current work status
:source ~/work1.session
load the saved session in Vim
vim -S ~/work1.session
bash command to load vim and work session

Manage Buffers

Text files opened in Vim are called buffers. You can open multiple files with bash command like this vim *.py, and all python files in current directory will open in Vim as buffers. The command :ls shows all buffers in current vim session. Note that command :!ls will execute a single bash command ls.

On the list generated by the :ls command, % represents current buffer and # represents alternate buffer. You can switch between the current buffer and alternate buffer. The + symbol on the list represents the file has been modified but not saved. The commands listed below are for navigating between buffers.

When writting Python programs in Vim, you can run the command :!python3 % to run the current file.

:bnext, bprevious, bfirst, blast
next, previous, first, and last buffer
open buffer number 5
:bdelete 5, 6, 8
close buffer numbers 5, 6, and 8
delete buffer numbers 5, 6, and 7
:edit file1
open file1 as an additional buffer
:find file1
open file1, this command also searches sub-directories
:b# or ^6
switch between current buffer and alternate buffer

Map Caps Lock Key

Some people recommend to map CapsLock key to Esc key when using Vim. Stackoverflow has a Q&A on how to do it. The following two lines of code in .vimrc file will do the trick. When leaving Vim, it will remap the key back to Caps Lock. The xmodmap software is already installed in Linux Mint, so no installation is needed.

au VimEnter * silent! !xmodmap -e 'clear Lock' 
    -e 'keycode 0x42 = Escape'
au VimLeave * silent! !xmodmap -e 'clear Lock' 
    -e 'keycode 0x42 = Caps_Lock'

I am used to press Esc key to enter normal mode, and I find the Caps Lock key is not as straightforward as the Escape key.

Source: an stackoverflow Q&A

Vim Command to Reflow Texts to 80 Columns.

When writting articles in Vim, I often need to reflow texts after some editing. To reflow texts, the first step is to set the textwidth and the second step is to select texts and reflow with command gq.

You can set the textwidth to 80 (:set textwidth=80 or :set tw=80), then use gg to move cursor to the start of the file and type gqG to reflow the whole article. The command gq also works with visual selection. You can use command vipgq to select the paragraph first and then reflow. Or you can use gqap to reflow current paragraph, and gq} reflow texts from current cursor to end of the paragraph. Note you can select a paragraph by typing vip or vap

Command gw is similar to gq. Here is the quote from help page.

gw: Format the lines that {motion} moves over. Similar to gq but puts the cursor back at the same position in the text.

Source: an stackoverflow Q&A

Run Commands on Multiple Lines

If you want to run a normal mode command on a range of lines, you can use the normal command. For example if you want to comment out line 4 to 6 of .bashrc file, you can use V to select those lines and apply command :'<,'>normal i# to insert a character (#) in font of each line.

# Load pyenv automatically by adding
# the following to ~/.bashrc:

export PATH="/home/george/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Source: Practical Vim Second Edition by Drew Neil Page 63, Tip 30

Sudo Save

Here is an online post about how to save read only files in vim.

The easy way is to add a new line to ~/.vimrc file and use sudow command.

cnoremap sudow w !sudo tee % >/dev/null

Links and References

Vim Cheat Sheet is a nice single web page which includes command vim commands.