Vim as a Developer Environment

Michael's Site About Martial Arts Aikido Jinenkan Tatsumaki Dojo Projects Security My Toolkit GitHub Links Linux Vim as a Developer Environment By MichaelMarch 4, 2021 [Original version posted on my WordPress site]

Basic Keybindings Perhaps the most basic and useful keybindings for modifying code are those for copying and pasting 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 common 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. Sometimes, though, we might want to display two source files simultaneously. Enter command mode, and use the ‘:split [filename]‘ command. e.g .’:split NewSourceFile.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 just ‘:q‘).

There is a variation of this command that splits the window vertically, and this might be 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. Sections can be moved between windows, using 'dp' and 'do'

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.

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. 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.

Adding a File Browser to a Session The editor could be made to have a layout rather like Visual Studio, with a file browser displayed in one of the windows, using the ‘:Vex‘ command. 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 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.

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

This article was updated on September 4, 2021