My workday generally revolves around .Net and the Microsoft stack, so for a change I thought it would be interesting to do some development under Linux. Finding a suitable text editor was the first task, but it didn't take me long to settle on Vim, an incredibly flexible and popular text editor, available for most platforms. This post looks at working with Vim for C++ development under Linux.
Vim is very customisable, and a lot of tweaks can be made in the per user Vim.rc file, usually found in your home directory, so let's start there.
Vim.rc Tweaks
We can makes some tweaks to Vim.rc to make working with C++ a little more pleasant by dealing with indentation and tabs in a more sensible way.
Firstly, let's set up the auto-indentation: set autoindent
set cindent
I prefer Visual Studio style tab settings of a 4 stop width and converting to spaces: set softtabstop=4 shiftwidth=4 expandtab
Navigation
Coming from a Visual Studio background, I'm used to working with a full IDE and all it brings, including file navigation through a tree menu as part of the editor.
Vim is a world apart from Visual Studio, so despite the Project type plugins available for Vim, I'm trying to stay away from GUI like treeviews. CTags builds a list of all symbols in your project to enable quick navigation to function definitions, calls, methods etc. Ubuntu 10.10 doesn't seem to include it as part of a standard install, so start with: sudo apt-get install ctags
Then run it against your code directory; using -R will run it recursively: ctags -R *
Now you can use the following commands to leap around your source with glee:
Ctrl + ]
Jump to selected symbol
Ctrl + O
Move back in history
Ctrl + I
Move forward in history
Note: in order for vim to find the tags file, it needs to be launched from the directory in which the file was generated.
Neat Commands
Learning Vim is almost like learning an instrument, commands can be combined in a way that makes text editing almost fun. There are a number of goodintroductions to Vim's commands, but here are a few that I find to be particularly useful:
gg=G
Reindent entire file
w / b
Next Word / Prev Word
^ / $
Start / End of line
cw
Change word
viwc
Select inner word, change
😡
Save changes and quit
bn / bp
Next buffer / Prev buffer
bd
Close buffer
Down The Rabbit Hole
This barely scratches the surface of what you can do with Vim, I haven't even looked at what you can do with buffers, tabs or any of the multitude of plugins.
I'm a big fan of MythTV, having used it for years as a very comprehensive media centre.
MythTV also provides an excellent extension framework, which is a good excuse as any to have a bash at some development under Linux for a change.
This post starts at the beginning: getting a hello world plugin compiled and running. This is based heavily on the HelloMyth page on the MythTV Wiki, with tweaks needed for a vanilla MythTV 0.23 installation under Ubuntu 10.10.
Prerequisites
Assuming that you have a working MythTV installation, you will also need the following packages:
Once those are installed, grab a copy of the standard MythTV plugins from the myth git repository; note that you'll want to work with the branch of MythTV you have installed (unless you're working with the trunk version).
Generate a config file by running the configure script in the mythplugins directory (You may or may not need the prefix argument, this is based on an Ubuntu 10.10 install):
./configure --prefix=/usr
Code
In the mythplugins folder, create a mythhello folder and add the following file:
mythhello/mythhello.pro TEMPLATE = subdirs
# Directories
SUBDIRS = mythhello
Now inside the mythhello folder, create another mythhello folder, and add the following files:
mythhello/mythhello/mythhello.pro include ( ../../mythconfig.mak )
include ( ../../settings.pro )
include ( ../../programs-libs.pro )
/** \brief Creates a new MythHello Screen
* \param parent Pointer to the screen stack
* \param name The name of the window
*/
MythHello::MythHello(MythScreenStack *parent, QString name) :
MythScreenType(parent, name),
m_cancelButton(NULL)
{
//example of how to find the configuration dir currently used.
QString confdir = GetConfDir();
VERBOSE(VB_IMPORTANT, LOC + "Conf dir:" + confdir);
}