Bart's Blog

Now viewing: /20070209172606

Links

pictures
vimblog source

Search

Bookmarks

Tags

abiword adam apt asm ata audio bash bios board-game boot bug busybox c code debian debug desktop devel disk dpkg email firefox font fs fun gdb geode git git-find git-graft go google gpg hardware html inkscape ion3 ipsec irc irssi joke kdb kernel klips kqemu kvm laptop lastfm ldap linux lua lvm mail make meta mouse-free mpd mutt net nfs oclug ogre ols oom openswan opteron osx ottawa perl power pxe qemu raid redhat ruby sbc scm screen scsi security shell ssh svn tags talk thinkpad ubuntu uml unix urxvt usb vi video vim vimgit vpn web weechat wmii wmiirc-lua x x86 x86emu xen xen-box-setup xterm zsh

Posts

[ 20081112150409 ]
splitting patches with git

[ 20081011081638 ]
creating busybox symlinks

[ 20081002215121 ]
wmiirc-lua v0.2.5 release

[ 20080916155113 ]
git-svn strangeness

[ 20080915112959 ]
installilng git man pages quickly

[ 20080913112345 ]
wmiirc-lua v0.2.4 release

[ 20080825100454 ]
Kernel Walkthroughs - booting

[ 20080813210205 ]
Linux Kernel Booting

[ 20080719211329 ]
printable OLS/2008 schedule

[ 20080715214447 ]
wmiirc-lua updates

[ 20080713194704 ]
Git Screencast

[ 20080713143429 ]
four steps to reproducible Debian installs

[ 20080705150651 ]
USB2.0 enclosure benchmark

[ 20080703230924 ]
Linux Kernel Walkthroughs posted

[ 20080702113602 ]
Introducing the Ottawa Ruby folks to Git

[ 20080628160732 ]
Authenticating Linux against OSX LDAP directory

[ 20080627142123 ]
Canada Day Events 2008

[ 20080613162541 ]
Linux Kernel Walkthroughs

[ 20080510083828 ]
is my usb device connected to a fast port?

[ 20080509111534 ]
show more git info on zsh prompt

[ 20080430104202 ]
git-vim

[ 20080412100337 ]
color your word

[ 20080404105620 ]
show current git branch on zsh prompt (2)

[ 20080303200359 ]
how to track multiple svn branches in git

[ 20080301134220 ]
fixing X for GeodeLX

[ 20080108002540 ]
kvm nfs hang

[ 20080107160836 ]
screen -c relative path bug

[ 20080105132854 ]
WeeChat spell suggestions

[ 20071219221358 ]
show current git branch in zsh

[ 20071217141037 ]
wmiirc-lua kitchen sink repository

[ 20071212100316 ]
protecting sshd from OOM killer

[ 20071204234232 ]
wmiirc-lua v0.2.1 remembers a bit more

[ 20071013205336 ]
wmiirc-lua v0.2 has suspend and raw modes

[ 20070929112345 ]
wmiirc-lua debianization

[ 20070924104140 ]
zsh tip of the day - global aliases

[ 20070915094213 ]
wmiirc-lua v0.1.1

[ 20070913130838 ]
comparing two directories

[ 20070909204125 ]
git-rebase --interactive

[ 20070908115905 ]
zsh tab completion awesomeness

[ 20070902000736 ]
wmiirc in lua

[ 20070831150306 ]
debugging with -dbg libraries

[ 20070831142646 ]
svn status like output in git

[ 20070829141847 ]
Git Cheat Sheet

[ 20070821142038 ]
switching to abiword

[ 20070811105746 ]
forwarding ssh and X through screen

[ 20070807112531 ]
git-svnup

[ 20070724082355 ]
reducing power consumption

[ 20070722123734 ]
Makefile template

[ 20070722002649 ]
less, colourful

[ 20070719162359 ]
irssi docs

[ 20070716114553 ]
qemu eats up /dev/shm

[ 20070710214512 ]
git-clean in svn land

[ 20070705113139 ]
ipw2200 not working

[ 20070627191916 ]
git slides updated

[ 20070618220649 ]
unpopular debian packages on my system

[ 20070611125852 ]
git-svn with multiple branches

[ 20070517085321 ]
Linus on Git at Google

[ 20070510134551 ]
vim modelines insecure

[ 20070504205042 ]
bios disassembler

[ 20070504124124 ]
dd hex arguments

[ 20070503013555 ]
urxvt mouseless url yanking

[ 20070502211941 ]
gitdiff.vba v2

[ 20070419234350 ]
india

[ 20070418155857 ]
zsh fun

[ 20070418143632 ]
pipe to pastey.net

[ 20070418094151 ]
vimgrep alias

[ 20070416202545 ]
mouse-free

[ 20070406141850 ]
ATA messages via SCSI layer

[ 20070330221019 ]
GITDiff vim plugin

[ 20070329011735 ]
git presentation for OCLUG

[ 20070328123631 ]
fixing vim's [[ and ]] for bad code

[ 20070316092236 ]
pxeboot and nfsroot with debian

[ 20070312134706 ]
etc snapshots with git

[ 20070307004041 ]
remote power switch

[ 20070222215355 ]
klips-less openswan git tree

[ 20070221041316 ]
git caching for v1.5.x

[ 20070218002214 ]
klips loses zlib

[ 20070209172606 ]
vim and linux CodingStyle

[ 20070207205427 ]
my kqemu install

[ 20070204100100 ]
leaner meaner openswan

[ 20070115111917 ]
wmii+ruby xlock action

[ 20070112131252 ]
cloning xterms in wmii+ruby

[ 20070102010551 ]
wmii+ruby talk for OCLUG

[ 20061228220641 ]
dump and restore

[ 20061218100219 ]
C style

[ 20061101002027 ]
fetching all git branches from remote

[ 20061028111607 ]
local caching for git repos

[ 20061020145437 ]
automatic version creation with git

[ 20061018213306 ]
wmii w/ ruby wmiirc

[ 20061018201907 ]
small fonts

[ 20061007151802 ]
google-codesearch from vim

[ 20060928020813 ]
shell commands

[ 20060920093957 ]
letting screen apps use the ssh-agent

[ 20060908223613 ]
mpdscribble stream support

[ 20060907125149 ]
glGo on ubuntu/dapper amd64

[ 20060906163240 ]
lbdb and mutt

[ 20060902145643 ]
vim tutorial

[ 20060902135722 ]
fixing your terminal

[ 20060828124713 ]
apt-get pdiffs

[ 20060824224842 ]
256 colour xterms

[ 20060824152658 ]
dynamic IPcomp

[ 20060824145428 ]
inkscape++

[ 20060818150516 ]
tags/cscope for system headers

[ 20060805131557 ]
opteron 170, part 4

[ 20060805101941 ]
opteron 170, part 3

[ 20060803233234 ]
opteron 170, part 2

[ 20060802210126 ]
opteron 170

[ 20060729144129 ]
OLS keysigning / 2006

[ 20060728105500 ]
git-find findings

[ 20060727162941 ]
starting on git-find

[ 20060727113632 ]
git-graft and git-find brainstorm

[ 20060726224531 ]
pretty function tracing

[ 20060713174723 ]
uml and multiple network segments

[ 20060707182236 ]
lastfm artist and title to clipboard

[ 20060706162256 ]
reverting a git changeset

[ 20060629212003 ]
user #3

[ 20060628083456 ]
firefox crashes with form input

[ 20060612222204 ]
ldap account management

[ 20060612194523 ]
stupid ldap

[ 20060608092157 ]
rpm hell is right

[ 20060605095726 ]
OpenSSH VPNs

[ 20060604114317 ]
Lenovo lost a customer

[ 20060601234010 ]
generating html colourized sourcecode

[ 20060601211716 ]
ion3 greatness and acting on X selections

[ 20060526085644 ]
software RAID10 performance

[ 20060525234148 ]
learning to love git

[ 20060524121638 ]
recent vim7 articles

[ 20060516095748 ]
bootstrapping debian on my sbc

[ 20060428145140 ]
entropy injection

[ 20060423140628 ]
Adam

[ 20060414202507 ]
converting mp3s to CD

[ 20060413232836 ]
secure apt-get

[ 20060412194423 ]
xen domain0 on debian

[ 20060410220525 ]
LVM2 on RAID1 mirror

[ 20060410102824 ]
building a RHEL4 kernel w/ kdb support

[ 20060407230939 ]
xen on debian

[ 20060407230818 ]
Upgraded look

[ 20060330131334 ]
Flattered by a copy

[ 20060328165153 ]
vim7 from source

[ 20060308123539 ]
Perl, Catalyst, CPAN, and Debian

[ 20060308123302 ]
last.fm

[ 20060128124841 ]
carcassonne and zombies

[ 20060120135931 ]
Election Humour

[ 20050925130002 ]
ldap on debian

[ 20050528190034 ]
Error while mapping shared library sections

[ 20050124130158 ]
IRC over email gateway

[ 20050110225522 ]
brute force attacks sshd?

[ 20050108095026 ]
LDAP authentication (part 1)

[ 20040326082602 ]
bash vi editing mode

[ 20040305163216 ]
cool debian tools

...





RSS Feed - Full Content

Valid XHTML 1.0 Transitional

Created with Vim

Created with Perl


vim and linux CodingStyle

[ link: vim-and-linux-coding-style | tags: vim linux kernel c code | updated: Sat, 16 Jun 2007 21:03:49 ]

It would seem that either no one that codes for the Linux kernel does so under vim, or if they do they don't have the time to share their vim configuration that doesn't conflict with the kernel's CodingStyle.

Below I will discuss some changes I had to make to my .vimrc and .vim/c.vim to work with C efficiently.

Auto Commands

I am using BufEnter auto commands to alter the my configuration based on the type of file I am editing. This is a pretty old feature of vim. Newer versions of vim provide a ftplugin's for doing the same way with less effort. For me the effort was already done.

To have vim use a different set of options for C files you would have something like this in your .vimrc:

    set nocompatible                        " vim defaults, not vi!
    filetype on                             " automatic file type detection
    if has('autocmd')
            filetype plugin indent on

            autocmd BufEnter *.c,*.h,*.cpp,*.hpp,*.cc source ~/.vim/c.vim
    endif

The rest of this entry will talk about things I put into my .vim/c.vim file.

Intenting

Like everyone knows tabs are 8 spaces deep. In addition to that, indentation of the first word should be done with the TAB character and not with spaces. You can drop tis into your config:

    set noexpandtab                         " use tabse, not spaces
    set tabstop=8                           " tabstops of 8
    set shiftwidth=8                        " indents of 8
    set textwidth=78                        " screen in 80 columns wide, wrap at 78

And while we are on the topic of indenting, the next few options make vim behave better when you are editing formated text:

    set autoindent smartindent              " turn on auto/smart indenting
    set smarttab                            " make <tab> and <backspace> smarter
    set backspace=eol,start,indent          " allow backspacing over indent, eol, & start

Vim has a feature which makes it indent code differently based on file type; here is how you would enable it:

    filetype plugin indent on

And some indenting macros...

    nmap <C-J> vip=                         " forces (re)indentation of a block of code

Highlighting

Turn on highlighting. This is a must :)

    syntax on

I like to have my C99 & Linux kernel types show up different. There are also some keywords that linux kernel uses which should stand out.

    syn keyword cType uint ubyte ulong uint64_t uint32_t uint16_t uint8_t boolean_t int64_t int32_t int16_t int8_t u_int64_t u_int32_t u_int16_t u_int8_t
    syn keyword cOperator likely unlikely

Recall that text should not exceed 80 columns, have spaces as the indent, or have trailing spaces. This will create three new Highlight classes that can be used by the colorscheme to show you mistakes in the formatting:

    syn match ErrorLeadSpace /^ \+/         " highlight any leading spaces
    syn match ErrorTailSpace / \+$/         " highlight any trailing spaces
    match Error80            /\%>80v.\+/    " highlight anything past 80 in red

The above just creates rules for marking new regions. To have them show up in different colours you have to modify your colorscheme... or use my hack on inkpot. The changes I made were to add...

    if has("gui_running")
            hi Error80        gui=NONE   guifg=#ffffff   guibg=#6e2e2e
            hi ErrorLeadSpace gui=NONE   guifg=#ffffff   guibg=#6e2e2e
            hi ErrorTailSpace gui=NONE   guifg=#ffffff   guibg=#6e2e2e
    else
            exec "hi Error80        cterm=NONE   ctermfg=" . <SID>X(79) . " ctermbg=" . <SID>X(32)
            exec "hi ErrorLeadSpace cterm=NONE   ctermfg=" . <SID>X(79) . " ctermbg=" . <SID>X(33)
            exec "hi ErrorTailSpace cterm=NONE   ctermfg=" . <SID>X(79) . " ctermbg=" . <SID>X(33)
    endif

C formatting

When editing C code in vim, the program can properly continue comment blocks and fix other. The first chunk, formatoptions deals primarily with text in code -- like comments.

    set formatoptions=tcqlron

Next we configure how to indent parts of the code. Hopefully, this will format exactly as to the CodingStyle specifications (let me know if I missed something). There are great examples in the :h cinoptions-values part of the vim manual.

    set cinoptions=:0,l1,t0,g0

Folds

You can have vim hide code by folding it based on {} blocks. Turn this on with

    set foldmethod=syntax

tags

Tags in vim allow you to jump between symbol usage and symbol definition. I use the code belowto tell vim where to locate tags files. I tell it to search in the current directory, then upto 4 below, and then a few system locations like the running kernel tree and /usr/include.

    let $kernel_version=system('uname -r | tr -d "\n"')
    set tags=./tags,tags,../tags,../../tags,../../../tags,../../../../tags,/lib/modules/$kernel_version/build/tags,/usr/include/tags

I often update the tag indexes with a simple cron job:

    $ cat /etc/cron.daily/system-tags
    #!/bin/bash

    cd /usr/include
    find . -name '*.h' > cscope.files
    cscope -b
    rm -f tags
    cat cscope.files | xargs -n100 ctags -a 

    cd /lib/modules/$(uname -r)/build/
    make -j2 tags cscope

If you want to use cscope, I recommend the cscope_quickfix plugin.





Bart Trojanowski
http://www.jukie.net/~bart
bart@jukie.net