Vim as a code editor

In the last post, I covered some very basic stuff for editing text in Vim. There are a few more advanced features I picked up while using it as an actual code editor.

Basic keybindings

Use ‘Esc’ key to enter visual mode.

  • Select text, use ‘v’ and ‘Ctrl+Right
  • Cut text: ‘d
  • Copy text: ‘y
  • Paste text: ‘p

Line numbers

Either use the :set number command, or add it to /etc/vim/vimrc.


Opening tabs, splitting windows and comparing source files

- :tabedit <filename>
- :split <filename>
- :diffsplit <filename>
  • To switch between tabs, use ‘gt’.
  • To switch between windows, use ‘Ctrl+W’.

Sections can be moved between windows, using ‘dp’ and ‘do


Folding code sections

Use ‘Esc’ to enter visual mode, and select text or code.

  • Fold selected text: ‘zf
  • Expand selected text: ‘zo
  • Close folded section: ‘zc

Omni Completion

This is Vim’s equivalent of Visual Studio’s IntelliSense, and I think it’s dependent on the programming language detected from the file extension. Use ‘Ctrl+N’ before, or ‘Ctrl+P’ just after, the keyword in Insert mode.


Adding a file browser to a session

- :Vex

This will open a file browser window. Switch between that and an editor window using ‘Ctrl+W’, and use ‘:q’ to close whichever one.


Semi-GUI command window

Add the following lines to vimrc:

source $VIMRUNTIME/menu.vim
set wildmenu
set cpo-=<
set wcm=
map :emenu

Now, when the F4 key is pressed in the editor, a semi-graphical menu will be displayed along the footer of the window.


Viewing compiler messages

- :copen
- :cclose

Status Bar Plugin

Maybe this is superfluous, but it’s a nice touch to have a semi-graphical status bar at the bottom of the screen. For this, I installed the vim-airline plugin, using apt-get vim-airline vim-airline-themes in the command line.

And I added the following to vimrc to set the colour scheme:

let g:airline_theme='papercolor'