Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Monday, September 7, 2009

VIM Modeline is your friend

Abstract: Add modeline to a file can help VIM remember file-specific options.

I've got to port some application to another arch these days, so I have to deal with a lot of files that does not follow my usual coding standards, and I don't want to edit my VIM settings now and then, so I turned to modeline for help.

To add modeline to all the files, I simply did a find & echo like this:

for f in `find . -name '*.cpp' -o -name '*.h' -print0| xargs -0i echo {}`;do echo -e "\n/* vim: set ts=2: */" >> $f;done

Saturday, September 5, 2009

How to insert formatted date in VIM

Abstract: Remember to escape the percent sign

I find that I cannot simply prefix a r! before plain date command to insert a date in VIM today, When I tried to insert date with

:r !date +%u

I got an error, the reason lies in the percent sign, which stands for the current file in VIM ex-mode. For example, you can count the current file like this:

:! wc %

Thus to insert a formatted date, you have to run:

:r !date +\%u

Tuesday, July 28, 2009

SnipMate is fabulous

Abstract: A textmate-like snippet script for VIM.

I've been using snippetemu for about one year and happen to find another snippet script for vim - snipMate. After a try, I do think snipMate is much better than snippetemu, for tow reasons:

  • Its snippet format is simple and clear.
  • No placeholder.

I do suggest give it a try even if you're not a fan of TextMate, because it really makes coding easier.

You can find snipMate's VIM page here.

Sunday, May 17, 2009

Script to synchronize VIM settings

Abstract: My scripts to backup vim setting and synchonize between Windows and Linux.


Finally I cannot bare the boring work of copying my vim settings around my labtop(with both windows and linux installed), lab pc and working server. So I write a few scripts to automatic this work for me:)


The first script is used to copy the scirpts to a certain dir on my laptop that can be share accross Windows and Linux systems. tr and sed are used to convert file format and handle some syntax difference.

#!/bin/bash
CURDIR=`pwd`
BACKDIR="/cygdrive/e/vim-setting"
if [ -d $BACKDIR ] ; then
echo "Backup to $BACKDIR"
else
mkdir "$BACKDIR"
echo "Make backup dir $BACKDIR"
fi
cd "$BACKDIR"
rm .vimrc
rm .gvimrc
rm -rf .vim
cp '/cygdrive/c/Documents and Settings/Yuanjie/_vimrc' .vimrc
tr -d '\15\32' < '/cygdrive/c/Documents and Settings/Yuanjie/_gvimrc' | sed 's/set guifont=\([^:]*\):h\([0-9]*\)/set guifont=\1\\ \2/' >.gvimrc
cp -r '/cygdrive/c/Documents and Settings/Yuanjie/vimfiles' .vim
cd $CURDIR

After files are moved to the synchronize folder, I init a git repo and push it to a host. The synchronize script on the Linux side goes like this:

#!/bin/bash
CURDIR=`pwd`
EMOUNT=0
BACKDIR="/media/e/vim-setting"
if [ ! -d "$BACKDIR" ] ; then
echo "trying to mount drive e ..."
pmount /dev/sda9
if [ "$?"=0 ] ; then
echo "drive e mounted"
EMOUNT=1
else
echo "drive e cannot be mounted"
exit
fi
fi
cd $HOME
tar cjf .vim-setting.$(date +%F).tar.bz2 .vim/ .vimrc .gvimrc
rm -rf .vim/ .vimrc .gvimrc
cp -r $BACKDIR/.vim $HOME
cp $BACKDIR/.vimrc $HOME
cp $BACKDIR/.gvimrc $HOME
if [ "$EMOUNT"=1 ] ; then
pumount /dev/sda9
if [ "$?"=0 ] ; then
echo "drive e unmounted"
else
echo "drive e cannot be unmounted"
exit
fi
fi
cd $CURDIR

The scirpt above tar the original setting files and copy the new settings from the synchronize dir. Since the drive is not always mount, a dir test is used and drive will be mounted with pmount. The synchonize scirpt on Windows is similar but simpler than this, but guifont setting in gvimrc will have to be changed back like this:

sed 's/set guifont=\([A-Za-z\\ ]*\)\\ \([0-9]*\)/set guifont=\1:h\2/'

Notice that you may have to modify this in case there's number in your font name...

Sunday, March 22, 2009

VIM in GNU coding style

I guess I'll start working on GCC recently, so I tried to prepare my VIM for GNU coding style. However, it's not as easy as I hoped, since VIM seems to favor BSD style more than GNU style which is a great waste of screen space in my view...Anyway, after a vast amount of Googling and attempts, it works now. Here's what I did to VIM: create a file named c.vim in my "~/.vim/after/ftplugin" directory, with the following content:
"GNU Coding Standards
setlocal cindent
setlocal cinoptions=>4,n-2,{2,^-2,:2,=2,g0,h2,p5,t0,+2,(0,u0,w1,m1
setlocal expandtab
setlocal shiftwidth=2
setlocal tabstop=8
setlocal softtabstop=2
setlocal textwidth=80
setlocal fo-=ro fo+=cql
I have to put it here otherwise the settings might be overrode by VIM's default ftplugin settings.

Wednesday, March 18, 2009

Vim春季新装面世


Vim是什么?Vim是一种异空间里的宗教……那就让我来展示一下我看到的Vim世界的样子吧=)
color scheme: molokai