mntnoe.com

Tips and tweaks making you more efficient with your favorite Linux tools.

Browsing Posts in Editors

I like to be able to find the next occurence of a string without exiting the command line. Here is a hack to do that:

set incsearch
cnoremap <expr> / (getcmdtype() == '/' \|\| getcmdtype() == '?' ? (getcmdline() == '' ? "\<Up>" : "\<Cr>/\<Up>") : "/")
cnoremap <expr> ? (getcmdtype() == '/' \|\| getcmdtype() == '?' ? (getcmdline() == '' ? "\<Up>" : "\<Cr>?\<Up>") : "?")
  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon

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 :-)

  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon

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…

  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon

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): continue reading…

  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon

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
  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon

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

  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon

Taking a course in Web Technology, I suddenly found myself editing a lot of XML files, so I hurried to fine-tune VIM to make editing more efficient. Please note that I am by no means an expert in XML, so please correct me, if I have misunderstood something. Configuration in xml.vim. Features:

  • Tag closing with code snippets from xmledit. Press <C-k> once to close the tag, and twice to close it and leave an empty line between.
  • Tag closing using omni-completion: <C-b>. This is used when the cursor is not at the end of the starting tag. Will insert the closing tag for the inner most unclosed starting tag.
  • Cycle between start and end tags with <LocalLeader>5 (typically \5 or ,5)
  • Remove outer tag with <LL>r
  • Goto parent tag with <LL>u (working on it, doesn’t work with <x/> tags. Anybody got a clue how to do this correctly?)
  • Document pretty printing with <LL>xf
  • Wellformedness checking (<LL>xx), DTD validation (<LL>xd) and XML Schema validation (<LL>xs) with XML Catalog support using xmllint (see below)

Have a look at Vim as XML Editor by Tobias Reif to find more tips. continue reading…

  • Facebook
  • Twitter
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon