GIT bash cheats

•July 9, 2009 • Leave a Comment

When you install GIT you should also install the GIT bash completion with it. (On gentoo you just need to set the bash-completion USE flag when emerging git). This makes your life much easier. This can complete git commands like “git stat[TAB]” to “git status”. Additionally it offers the GIT bash function __get_ps1 for your command prompt. You can integrate this into your “PS1″ variable to show the current branch of your working dir on your command prompt. That’s just great, isn’t it?

Add this to your ~/.bashrc to activate the GIT bash completion and the new command prompt.

# load git bash completion
source /usr/share/bash-completion/git
# Cool trick to show current git branch in the command prompt in gentoo colors.
export PS1='\[33[01;32m\]\u@\h\[33[01;34m\] \w$(__git_ps1 " (%s)") \$\[33[00m\] '

If you enter your working dir the prompt will look like this.

gergap@lt_gergap ~/work/helisim (master) $

If you are wondering what this “\[33...]” is for, this is just to set some nice colors for the prompt. You can remove that if you don’t like colors.

VIM Wild Menu

•July 8, 2009 • Leave a Comment

I was annoyed by the file name completion in VIM. It completes the whole name of the first match, even if there are a lot of possibilities. That feels like DOS :-( so that you need a lot of <TAB> to iterate to the correct file.
I wanted to behave like the BASH to complete only the longest possible part and then show a list of possible completions with the second <TAB>.
No problem in high configurable VIM. I found out that this two settings do what I want.

set wildmode=longest:full
set wildmenu

Does KDE4 on Windows make sense?

•June 9, 2009 • Leave a Comment

I expect there are a lot of opinions to this questions ;-)

KDE4 is another Open Source success story. So why should a user be interested in running it on Windows, when there are free alternatives like Linux? Is it not one step back? By only supporting free operating systems this would be another reason for Windows users, that want to use the cool KDE apps, to switch to free operating systems like Linux. So why are Open Source developers are interested in porting it to Windows?

A lot of good questions, and in my opinion all this is right, but there is this KDE4 Windows port and a lot of Windows users are already using it. So for some users it seems to make sense. One reason I heard is that some users are forced to use Windows in their job and so have no choice. With KDE4 for Windows they can now use all the cool KDE applications, that they are used to use from home and are not limited to the applications that come with Windows.

Open Source means to be free. Let you do with it whatever you want, as long as the result of your work offers the same freedom to all other users. Thus it allows you also to port it to Windows and let it run on a proprietary operating systems.
So at the end – if people like it or not – KDE4 on Windows is also part of the freedom of Open Source.

Howto recursively replace file headers of source files

•June 3, 2009 • 2 Comments

This scripts can replace file headers from C/C++/JAVA header and source files. Often it’s necessary to replace the license conditions in multiple source files, update the copyright year, or simply make all file headers consistent. This can be a lot of work, so that’s why I wrote this scripts.

How it works: The BASH script replace_header.sh is a simple wrapper that executes the AWK script remove_header.awk which implements a basic state machine for recognizing C/C++/JAVA comments (”// …”, “/* … */). All comments at the beginning of the file – also when spanning over multiple lines – are ignored until the first line without comment is reached. The AWK file ignores all this comments, and prints the rest of the file unchanged. The BASH script creates a new file with your new header.template and adds your code without the old file header to the newly created file. Afterwards it copies the new file over the old one.

Example:

find . -name "*.h" -exec ~/rh/replace_header.sh {} \;

Download replace_header_scripts.tar.gz

VIM paste text with line numbers

•June 1, 2009 • 1 Comment

I normally put my code from VIM with syntax highlighting and line numbers turned on on my blog. This is good for readability, but bad if you want to copy paste it from the web site. With vim it’s easy to remove these line numbers from pasted code. Here is how to do that.

  1. Select the code on the website (no CTRL-C/CTRL-V is needed on Linux, selecting does already the copy)
  2. Start up vim and enter :set paste. This turns off the automatic indentation, to allow keeping the indentation of the pasted code.
  3. Press “i” to change to insert mode.
  4. Now click with the middle mouse button into the vim window. That inserts the code.
  5. Press “Esc” to change back to command mode.
  6. Now use vim’s regexp based substitute and replace command to remove the line numbers: :%s/^[0-9 ]\d \=//

New VIM color scheme wombat256

•June 1, 2009 • Leave a Comment

I found this nice GVIM color scheme from Lars H. Nielsen called “wombat”. I wanted to use this also in normal vim, because I prefer working on the console over the graphical vim. For that I needed to convert the scheme to work in 256 color mode of xterm or KDE’s konsole. I found this site very useful to understand why and how to convert the color schemes.
Here are a screenshot and the scheme “wombat256″. To include it store the wombat256 scheme in ~/.vim/colors and add this lines into your .vimrc.

" turn syntax highlighting on
set t_Co=256
syntax on
colorscheme wombat256

VIM Wombat256
Wombat256:


 1 " Author: Gerhard Gappmeier <gerhard.gappmeier@ascolab.com>
 2 " Description: 256C Scheme for console based VIM
 3 "   Based in wombat GVIM scheme from Lars H. Nielsen (dengmao@gmail.com)
 4 " Last Change:  2009-06-01
 5
 6 set background=dark
 7
 8 hi clear
 9
10 if exists("syntax_on")
11   syntax reset
12 endif
13
14 let colors_name = "wombat256"
15
16 " Vim >= 7.0 specific colors
17 if version >= 700
18   hi CursorLine ctermbg=236 
19   hi CursorColumn ctermbg=236  
20   hi MatchParen ctermfg=7    ctermbg=243   cterm=bold
21   hi Pmenu      ctermfg=7   ctermbg=238 
22   hi PmenuSel   ctermfg=0     ctermbg=186  
23 endif
24
25 " General colors
26 hi Cursor       ctermfg=0    ctermbg=241     cterm=none
27 hi Normal       ctermfg=7    ctermbg=235   cterm=none
28 hi NonText      ctermfg=244     ctermbg=236    cterm=none
29 hi LineNr       ctermfg=243  ctermbg=0       cterm=none
30 hi StatusLine   ctermfg=7     ctermbg=238    cterm=none
31 hi StatusLineNC ctermfg=243  ctermbg=238     cterm=none
32 hi VertSplit    ctermfg=238    ctermbg=238   cterm=none
33 hi Folded       ctermbg=238  ctermfg=248     cterm=none
34 hi Title        ctermfg=7  ctermbg=0       cterm=bold
35 hi Visual       ctermfg=7     ctermbg=238    cterm=none
36 hi SpecialKey   ctermfg=244    ctermbg=236   cterm=none
37 hi DiffAdd      ctermfg=0    ctermbg=113     cterm=none
38 hi DiffChange   ctermfg=0    ctermbg=175     cterm=none
39 hi DiffDelete   ctermfg=0    ctermbg=17      cterm=none
40 hi SpellBad     ctermfg=0    ctermbg=161     cterm=none
41
42 " Syntax highlighting
43 hi Comment      ctermfg=246     ctermbg=235
44 hi Todo         ctermfg=245    cterm=italic
45 hi Constant     ctermfg=173     cterm=none
46 hi String       ctermfg=113  cterm=none
47 hi Identifier   ctermfg=186   cterm=none
48 hi Function     ctermfg=186     cterm=none
49 hi Type         ctermfg=186    cterm=none
50 hi Statement    ctermfg=117    cterm=none
51 hi Keyword      ctermfg=117  cterm=none
52 hi PreProc      ctermfg=173     cterm=none
53 hi Number       ctermfg=173   cterm=none
54 hi Special      ctermfg=194  cterm=none
55
56

Minimal .vimrc for C/C++ developers

•May 29, 2009 • Leave a Comment

VIM is a very powerful editor, and a standard developing tool for Linux users. Meanwhile VIM is also available for Windows. The good thing is, it is very customizable and can do almost everything. The bad thing is, by default most features are turned of, because it depends on your use case of the editor, what features should be turned on or off.

I’m a C/C++ developer, so I configured it for developing. Just store this file in your home directory or add it to your existig .vimrc. VIM will read this file at startup and configure it for C/C++ developing.

What this configuration offers:

  1. Visual: Syntax-highlighting and line numbers
  2. Editor Configuration: tab expanding to space, tabwidth 4, linewrap at 120 characters, C indentation, simple save file using [F2] (works in insert mode and normal mode)
  3. Navigation: Goto-Definition with [F12]. The word under the cursor is looked up using the ctags index file.
  4. Autocompletion: Therefore omnicppcomplete has to be installed additionally. See comments in .vimrc.
  5. [F5] automatically generates the tags file for the current folder.
  6. Intelligent Comments: Just type /* Text. The next * will be inserted automatically. / closes the comment automatically.
  7. Doxygen Comments: Creating doxygen comments for function under cursor using [F6]. Therefor DoxygenToolkit needs to be installed. See comments in .vimrc.
  8. Spell Checking using vim-spell: Use Alt-Down/up to jump from own spelling error to the next/previous. Use zg to add a good word, z= to get suggestion.

Another thing that works out of the box: Press SHIFT-K to lookup the function under cursor in the manpage – that’s the integrated help :-)

Vim with OmniCppComplete

Vim with OmniCppComplete


 1 " VIM Configuration File
 2 " Description: Optimized for C/C++ development, but useful also for other things.
 3 " Author: Gerhard Gappmeier
 4 "
 5
 6 " set UTF-8 encoding
 7 set enc=utf-8
 8 set fenc=utf-8
 9 set termencoding=utf-8
10 " disable vi compatibility (emulation of old bugs)
11 set nocompatible
12 " use indentation of previous line
13 set autoindent
14 " use intelligent indentation for C
15 set smartindent
16 " configure tabwidth and insert spaces instead of tabs
17 set tabstop=4        " tab width is 4 spaces
18 set shiftwidth=4     " indent also with 4 spaces
19 set expandtab        " expand tabs to spaces
20 " wrap lines at 120 chars. 80 is somewaht antiquated with nowadays displays.
21 set textwidth=120
22 " turn syntax highlighting on
23 set t_Co=256
24 syntax on
25 colorscheme wombat256
26 " turn line numbers on
27 set number
28 " highlight matching braces
29 set showmatch
30 " intelligent comments
31 set comments=sl:/*,mb:\ *,elx:\ */
32
33 " Install OmniCppComplete like described on http://vim.wikia.com/wiki/C++_code_completion
34 " This offers intelligent C++ completion when typing ‘.’ ‘->’ or <C-o>
35 " Load standard tag files
36 set tags+=~/.vim/tags/cpp
37 set tags+=~/.vim/tags/gl
38 set tags+=~/.vim/tags/sdl
39 set tags+=~/.vim/tags/qt4
40
41 " Install DoxygenToolkit from http://www.vim.org/scripts/script.php?script_id=987
42 let g:DoxygenToolkit_authorName="Gerhard Gappmeier <gerhard.gappmeier@ascolab.com>" 
43
44 " Enhanced keyboard mappings
45 "
46 " in normal mode F2 will save the file
47 nmap <F2> :w<CR>
48 " in insert mode F2 will exit insert, save, enters insert again
49 imap <F2> <ESC>:w<CR>i
50 " switch between header/source with F4
51 map <F4> :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X123X$,.cpp,<CR>
52 " recreate tags file with F5
53 map <F5> :!ctags -R –c++-kinds=+p –fields=+iaS –extra=+q .<CR>
54 " create doxygen comment
55 map <F6> :Dox<CR>
56 " build using makeprg with <F7>
57 map <F7> :make<CR>
58 " build using makeprg with <S-F7>
59 map <S-F7> :make clean all<CR>
60 " goto definition with F12
61 map <F12> <C-]>
62 " in diff mode we use the spell check keys for merging
63 if &diff
64   " diff settings
65   map <M-Down> ]c
66   map <M-Up> [c
67   map <M-Left> do
68   map <M-Right> dp
69   map <F9> :new<CR>:read !svn diff<CR>:set syntax=diff buftype=nofile<CR>gg
70 else
71   " spell settings
72   :setlocal spell spelllang=en
73   " set the spellfile - folders must exist
74   set spellfile=~/.vim/spellfile.add
75   map <M-Down> ]s
76   map <M-Up> [s
77 endif
78

Image shrink bash script

•May 26, 2009 • Leave a Comment

Today I just want to upload a little bash script I often use for shrinking my pictures to reduce their size. This is useful for sending by mail or uploading to facebook.

GUIs are very cumbersome if you want to convert a lot of files, but ImageMagick is a great commandline tool for processing pictures. ImageMagick can do a lot, but here I only use the ‘resize’ function of ImageMagick’s ‘convert’ tool.

How to use? Just enter your folder with jpg images and call ‘/path/to/makesmall.sh’. You can also put it into /usr/local/bin to avoid entering the full path.

You can download it from Pastebin or just copy/paste it from here. This link will stay valid for one year.

This post is not for Linux Gurus, but maybe it’s useful for some newbies ;-)

 1 #!/bin/bash
 2 # Description: Little script to shrink all images in the current folder.
 3 #  That’s convenient if you want to send them by mail or want to upload it to facebook.
 4 # Author: Gerhard Gappmeier
 5 # License: GPL – GNU General Public License
 6
 7 if [ -e small ]; then
 8   echo "folder ’small’ exists"
 9 else
10   echo "creating folder ’small’"
11   mkdir small
12 fi
13
14 for file in *.{jpg,JPG}; do
15   if [ -e "$file" ]; then
16     if [ -e "small/$file" ]; then
17       echo "Skipping ‘$file‘, because ’small/$file‘ already exists."
18     else
19       echo "Converting ‘$file‘…"
20       convert -resize 1024 "$file" "small/$file"
21     fi
22   fi
23 done

Beeing free

•April 23, 2009 • Leave a Comment

It’s always so funny.
A Windows user sees that I’m using Linux and then he’s asking:
WinUser: “What Linux are you using?”
I: “Gentoo.”
WinUser: “What version?”
I: “Gentoo.”
WinUser: “No, what version.”
I: “Yes, Gentoo.”
WinUser: “???”

Then I start to explain that I don’t need to wait for years for a new Linux like with Windows. Gentoo is upgraded continuously, each program individually and you never have to reinstall it.
Hard to believe for somebody who knows only Windows and has never seen the power of free software. “Free” means the user can do what ever he wants, not just “no costs”. Freedom, no limitations. Why should I made myself dependent from software vendors, where I have to accept their conditions, their prices, their update cycles, their guarantee that the software is secure and that there is no built-in spy software, …
Linux is free and it’s open, I can do with it what I want, use it for what I want, can change it like I want. There is no limitation like: if you use MS Exchange you MUST also use MS Outlook, if you use Media Player, you MUST also use a MS video codec, if you write a document with MS Word all users you want to send it MUST also have Word, if you make Website with MS Frontpage you MUST also use MS Internet Explorer, …
Hey! There are open standards!!!
MS has no interest in being open, implementing standards, being useful or being stable.
They are just interested in making money.
But not with me, I’m free!

RC HeliSim

•April 6, 2009 • Leave a Comment

Hi all Linux users.

I’m currently developing an RC helicopter simulator. The focus is realistic flight behavior, but when everything works I also want to work more on the OpenGL rendering to give it a better look.
Why am I doing that? Because I can and because it’s fun ;-) (ok, a little bit nerdy, I know). But I was interested in learning to fly a helicopter for a long time, and I’m also interested in the helicopter physics. So before I crash an expensive helicopter I better write a simulator where I can learn both: Learn to fly and learn the physics.
I also was searching for some free simulators, but there was not very much available. One is FMS (Flying Model Simulator) which is nice but not very realistic. Another big disadvantage is, it’s a Windows program and it’s closed source, so nobody can improve or port it to Linux. Another one is Helix, a JAVA program that’s looks very realistic according to the videos on the website, but I could not manage to get it working. That’s due to the nature of JAVA. On Linux accessing the joystick device did not work, on Windows I got a damn JAVA exception, and on Max OS X the required JVM is not available. JAVA really sucks!!! And it’s damn slow anyway. (Sorry JAVA fans, I don’t like JAVA, but that’s only my personal opinion)
I like to have everything under control – also when to acquire or release memory – and that’s why I’m developing my simulator in C++ using a nice Qt GUI (Qt rocks ;-) ) and OpenGL for 3d rendering. This gives you all advantages of powerful OOP using C++ combined with powerful and easy to use Qt classes (that’s rapid application development my friends), and the big bonus is: everything is portable. The only OS dependent code is accessing the joystick device which I encapsulate via an abstract interface. So it’s easy possible to develop specialized subclasses for Windows, or other OSes. But for now I’ll concentrate on Linux of course.
Reading this may be a little bit dry, so here comes a first screenshot:

Helisim Screenshot

Helisim Screenshot

I also made a screencast to demonstrate the actual state in action.
It’s a little bit jerky, but that’s due to recording not due to the simulator ;-)
You can download it here: helisim.avi

Some more technical details for the interested reader:

  • Programming Language: C++/Qt
  • 3D Rendering: OpenGL
  • 3D Sound effects: OpenAL
  • Physics Engine: My own including correct aerodynamics according to the BEMT (Blade Element Momentum Theory) (Currently implemented is only BET without Momentum, still working on that.)
  • License: Will be GPL (should it be GPLv3?)

Here is a sample gnuplot picture which shows the lifting forces calculated by my physic engine.

Lifting force

Lifting force

And I have already a lot of ideas for the future:

  • The software should be very modular allowing not only the fly, but also use it as a developer platform: Adding or extending the physics engine, replacing the joystick input module with own implementations. Not only for portability but also for developing autonomous flight robots.
  • Improving the rendering with shadows and reflections: Cg may help
  • Also replacing the rendering with a 3D engine like Ogre might be possible (but I actually have no clue about ogre)
  • Making a game like mode with predefined gates to pass.
  • For the game mode a ghost would be nice which shows either the ideal flight line or the last recorded one.

I hope you like my idea and more input is welcome. Also I’m searching for a good name at the moment. HeliSim is not that creative name and may already exist ;-) So please send me your ideas: Lin, Tux, Heli, Sim, … are some good keywords as a starting point.