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.
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 pkg-vim-maintainers@lists.alioth.debian.org
Compiled by pkg-vim-maintainers@lists.alioth.debian.org
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
.
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
endif
if exists("+columns")
set columns=108
endif
" 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 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
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
endif
if exists("+columns")
set columns=108
endif
Source: an article on fandom.com.
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.
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.
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.
<n>
<n>G
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.
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.
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 linux.com & a blog post by Jake Harding.
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.
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,
Alt + Tab
switch focus to Vim."+p
to paste the texts.Here are the steps to copy texts out of Vim to another program.
v
command to visually select texts in Vim."+y
to copy the texts to clipboard.^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.
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.
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.
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
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
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
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
Vim Cheat Sheet is a nice single web page which includes command vim commands.