Tags

, , , , , ,

Basic Keybindings

Perhaps the most basic and useful keybindings for modifying code relate to the copying/moving/pasting of code sections within a source file. To do this, use the ‘v‘ key in visual mode to mark the beginning and end of the section to be copied/moved, and press the ‘y‘ key to copy that section. The ‘p‘ key will paste the section wherever the cursor is positioned.

Line Numbers

A feature that’s pretty much universal to code editors is line numbering, obviously to make it easier to find something referenced by a debugger or an exception message. Either use the ‘:set number‘ command, or add it to vimrc.

Splitting Windows and Comparing Source Files

The easiest way to have multiple files open in a session is to use the ‘:tabedit [filename]‘ command, and use the ‘gt‘ keybinding to switch between tabs. If you’re using gVim, you can click on the tabs just like a conventional GUI editor.
Sometimes, though, we might want to display two source files simultaneously. Enter command mode, and use the ‘:split [filename]‘ command. e.g .’:split AD-Authentication-Model.cs‘. Another window will appear in the editor, with the content of the specified file.

We can switch between the windows using Ctrl+W. This works in both visual and insert mode. To close the current window, use the ‘:close‘ command (or even ‘:q‘).

There is a variation of this command that splits the window vertically, and you might find this more conducive for comparing files, especially with the line numbers displayed.

Replace the ‘:split‘ command with ‘:diffsplit [filename]‘ to compare a file with an earlier version of itself.

For copying/moving sections between windows, ‘dp‘ shifts a selected block from the current window to another, and ‘do‘ fetches a block from another window into the current one.

Folding Code Sections

Using ‘zf‘ and ‘zo‘, we can hide marked sections of code, to make a source file easier to read. This is done in visual mode, not command mode. Here we have two methods displayed in the editor, and we’ll fold them so they’re represented by single lines.

Use the ‘Esc‘ key to enter visual mode, and ‘v‘ key to highlight the section of code to fold, and then ‘zf‘ to fold the highlighted section. Here I’ve done this with two methods.

After using ‘zf‘ to fold marked sections of text, we should use ‘zc‘ to close and ‘zo‘ to open. This is important if there are folded sections nested within other folded sections.

Omni Completion

This is vim’s equivalent of Visual Studio’s IntelliSense. Use Ctrl+N before, or Ctrl+P just after, the keyword in Insert mode. My installation doesn’t appear to list the autocomplete options specific to the current programming/scripting language.

Browsing the Filesystem

The editor could be made to have a layout rather like Visual Studio, with a file browser displayed in one of the windows. Here I’ve used ‘:Vex‘ to open vim’s own file browser. This will create a new window in the browser automatically.

To open a file, move the cursor to the file name and press the Enter key.

Semi-GUI Menu


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.

Errors Window

When compiling a source file, use the ‘:copen‘ command to open a window listing errors, and ‘:cclose‘ to close it.