Vim config (May 2009)

May 22nd, 2009

Here is my Vim configuration as of May 2009. It is a work in progress (and will always be), and is designed on a number of pragmatical choices and compromises, rather than on a universal guideline.

That been said, I think my configuration has become pretty neat. See for yourself :-)

I only use Vim in the terminal, but most of the configuration also works in gVim (you may have to modify some of the bindings for the special characters like <C-Space>).

Although the .vimrc is long, I have tried to avoid making the configuration too heavy, so you will only find mappings that I actually use. Usually, I discover a work pattern that I want to speed up, and create a binding. Then I try it some time to see if I will get used to it; if not, I remove again. That’s the methodology :-)

Major features

  • Usable with both the Colemak and Qwerty layout
  • Readable colorscheme
  • Makes heavy use of alt/meta bindings (as alt is easier to hit than control)
  • Efficient tabbed navigation
  • Custom status line
  • Quick access to often used Ex commands
  • Many navigation improving bindings (examples: next occurence of visual selection, tag jump in new tab reusing existing tabs, search outline ({{{), open visual selection in split window)
  • Plugins are configured to be pervasive (they will not get in your way; at least not my way :-) )

As of writing, I use these external plugins, which you may download from vim.org.

  • AlignPlugin
  • NERD_commenter
  • a (alternate)
  • imaps
  • matchit
  • project
  • rhs (not yet published)
  • securemodelines
  • surround
  • taglist
  • timestamp
  • vcssvn

Of “major” filetype plugins, I use haskellmode and latexsuite.

These are the files of interest. Enjoy :-)

“Danish” Vim-friendy Colemak Layout

September 9th, 2008

I recently switched to the amazing colemak keyboard layout  - well, read the FAQ.

Also, being Danish (and using Vim), I swapped J and Y (qwerty Y and O). Qwerty Y is the only button I have difficulties reaching, and Danish has many “hard to reach” combinations like SKJ. I use AltGr for the Danish characters, Æ, Ø, and Å.

Further, by swapping J and Y, the mappings works well in Vim without any remapping (which breaks the consistency). Instead of HJKL I use HONU (qwerty positions), which are fairly easy to reach. It takes quite some time to get used to (especially for Vimmers), but I think it’s worth it…

Using existing tabs when jumping to tags and files in Vim

August 15th, 2008

Update: New version available. It is ported to native Vim Script, and support has been added for wildcards in &path. Download. Add the file to your .vimrc, or fit it in as a plugin.

On March 27, 2008:

When you use the normal gf and <C-]> bindings, you change the buffer in the current window. There is also an option to create a new window when doing it, but there are no way to reuse windows that already show the corresponding buffer, at least not natively.

You can reuse tabs and windows (like other IDE’s) with findtab.py, which is a simple Python script consisting of three functions. To use it, add something like the following to your .vimrc:

pyfile ~/.vim/extern/findtab.py
nnoremap <M-]> :python TabTag()<Cr>
nnoremap <C-w><M-f> :python TabFile()<Cr>

I have tried to make TabFile() work like gf, supporting path, includeexpr and suffixesadd. I think TabTag() works like <C-]>. The script also supplies a general Python function called FindTag(filename). It works as you think…

Fvwm config of August 2008

August 14th, 2008

I have put myself together and cleaned up my Fvwm config to make it shareable:

fvwm-mntnoe-2008-08-1.tar.bz2

It is a 100 % keyboard driven Fvwm config, that aims to make window handling efficient by making the keyboard commands easy to hit. Two instances of conky (one is double buffered, the other in its own window) and xfce4-panel is visible in the screenshots.

All window handling uses the AltGr (right Alt on the US keyboard) key as modifier, to avoid conflicts with applications. AltGr is easy to reach, but seldom used, except for “local characters”. The most frequently used commands are the easiest to hit. For example, instead of using CTRL-F1,F2,F3,F4 to switch workspace, AltGr-U,I,O,P is used instead. The “root menu” is called with AltGr-L.

The window titlebar is replaced by a colored border; the color which depends on the application. This actually improves the readability (IMHO), as the “real” information comes closer to each other. Also it uses less screen space which is crucial on a 12″ laptop :-)

Also, I found a copy of my original Fvwm config from 2005, so it’s available for download again, thanks to the guys at guistyles.com. The link at the post had been broken for a while.

Vim C++ Indentation Hack

July 12th, 2008

It is possible to customize the behavior of cindent in Vim by writing a wrapper function. All you need is to find out which indentifiers Vim uses for calculating the new indent, and then find an appropriate regular expression.

Suppose we do not want to indent stuff in namespace declarations. I found out that it is enough to adjust the indentation of the first definition (except for a special case when using inheritance (”: public …“), see the code): Read the rest of this entry »

C++ Coding Style Guidelines

June 30th, 2008

I just came along the Inkscape Coding Style Guidelines. While written for the Inkscape project, I think it gives some food for thought when talking about code readability. Nice balance between being strict and pragmatic too. Whether you like the style or not, it worthy a read…

English keyboard layout with Danish characters

June 18th, 2008

I have a Danish keyboard, but I find the English keyboard layout better to code with, so some time ago, I made some customizations using xmodmap to combine these. I wanted to use the English layout in general, but with the Danish characters æ, ø, and Ã¥ available using Alt Gr-;, Alt Gr-’ and Alt Gr-[, respectively. Also, I wanted to be able to use Alt Gr as a modifier for FVWM shortcuts.

This turned out to be tricky to accomplish, actually. If I set XkbLayout=us in xorg.conf (English layout), I couldn’t get Alt Gr to work with the Danish Characters and as a modifier key at the same time. Mode_Switch or ISO_Level3_Shift either switched keycodes or acted as a modifier key, but not both. Even if I restored the previous Danish layout using dumps of keycodes and modifiers from xmodmap, it didn’t work.

So I made a hack: Read the rest of this entry »

Split screen from visual mode in Vim

May 4th, 2008

I like to have a small window at the top of a tab page for reference. Useful when you want to consult the exact wording of a assignment or task description. This function creates a split with the top window showing the area of your visual mode selection. If more than one window is visible, it reuses the top most. Update (2008-06-18): The script now yields two windows: A “main” one, and the one showing only the selection.

Put something like this in your .vimrc:

vnoremap <M-s> :<C-u>call VisualSplit()<Cr>

function! VisualSplit()
	let top = getpos("'<")[1]
	let bot = getpos(”‘>”)[1]
	let height = bot - top + 1
	normal \<Esc>
	only
	split
	execute “1 wincmd w”
	” position and size the top window
	execute “normal ‘<z” . height . “\<Cr>z\<Cr>”
	” return to ‘main’ window
	execute “normal \<C-w>2\<C-w>”
endfunction

Effective use of Tabs in Vim

March 20th, 2008

This is a simple, but important tip:

Tabbed interfaces have emerged everywhere, and also Vim 7 comes with support for it. For projects consisting of multiple files, I find it easier to work with tabs than with split windows and multiple buffers. Of course we need some better keybindings:

inoremap <M-n> <C-o>gt
inoremap <M-p> <C-o>gT
inoremap <M-w> <Esc>:tabclose<Cr>
inoremap <M-t> <Esc>:tabe<Cr>
nnoremap <M-n> gt
nnoremap <M-p> gT
nnoremap <M-w> :tabclose<Cr>
nnoremap <M-t> :tabe<Cr>

Also, making the tab bar always visible encourages you to use them more:

set showtabline=2

Now remember to use the -p parameter when you open Vim with multiple files:

vim -p *.tex

Having multiple files open in for instance LaTeX projects also allows you to use completion in references to labels in the corresponding files.

BTW add colon and underscore to iskeyword to enable completion for labels like fig:nice_figure. Oh yes, and use latex-suite

Directory Listing in FVWM menus using GNU find

February 22nd, 2008

If you need more flexibility than fvwm-menu-directory can give you, you can do the same with GNU find. All you need to do is to create a dynamic menu in your fvwm config

DestroyMenu MenuDaimi
AddToMenu MenuDaimi
+ DynamicPopupAction PipeRead "$[FVWM_USERDIR]/menus/my_menu.sh”

and let the shell script output the menu. I use it, for example, to keep a list of shortcuts to my assignments:

cat << eos
DestroyMenu recreate MenuDaimi
AddToMenu MenuDaimi
eos

echo '+ "dADS1"    Title'
find $HOME/A/aflv_* -name "*.tex" -printf
	'+ %%24x24/mimetypes/gnome-mime-application-x-tex.png%%"%f"\t
	Exec exec gvim -c "cd %H" %p\n'
...

(Note that the find command should be written as one line.) This will list all LaTeX files in $HOME/A/aflv_*, and selecting an entry will start GVIM in the corresponding directory. Nice…

One could get creative and sort the files after date, skip files more than a week old etc. But please be careful not to forget to actually work on the assignments :-)