Posts Tagged ‘VIM’

VIM sessions

This post basically provides the same info as Reference [1] does.

I’m tired of typing tabe many times every time I continue with a project that has many files.

Sure, there is no reason for an omnipotent editor* not to support sessions. Command :mksession (mks) saves the session info to a file (Session.vim by default) that can be source‘d the next time we use VIM. Tell VIM what should or should not be saved by using set sessionoptions.

I’m too lazy to type all these commands once and again, so I added the following lines to ~/.vimrc:

set sessionoptions=sesdir,folds,tabpages
com SL source Session.vim
com SX call SessionSaveAndExit()
function SessionSaveAndExit()
    wa
    mks!
    qa
endfunction

Every time I decide to continue with working, I start VIM and type :SL. When I’m finished, I type :SX and VIM saves all my files and exits.

I dislike to define too many keyboard shortcuts since I frequently press the wrong keys and have no idea what’s just happened (fortunately we are using VIM instead of the original Vi and we can undo and redo easily), so I use com (abbr. command) instead of nmap.

* I don’t mean to offend Emacsers – Emacs is an OS rather than an editor…

References
[1] lambda.oasis: Vim Sessions
[2] VIM on-line help, of course (e.g. type :help sessionoptions in VIM)

Tags:

VIM modelines

I was wondering why all Emacs modeline samples I found online worked so well, but no VIM ones worked at all. In fact, VIM by default disables modelines because it is a potential security hole. (This is too bad. They can enable only safe instructions by default.) Add set modeline to ~/.vimrc to enable it.

A modeline is some instructions to the editor put in the comments, usually in the beginning of a text file. The most common use is probably to set the syntax highlighting rule, tab width and indent.

Modeline example:
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ft=cpp ts=2 sw=2 sts=2 cindent: */

The first line is for Emacs and the second for VIM. Note that an Emacs modeline must appear in the beginning of a file, while VIM modelines can be put anywhere.

Basicly, these two lines force the file type to be C++ (especially useful for .h files, which are by default treated as C sources), set the width of a tab to be 2 characters and enable auto indention. (My knowledge about Emacs is very limited, so probably this is not accurate.)

Some resources:
1. GNU Emacs modeline documentation
2. VIM modeline documentation

Tags: ,