Wednesday, April 30, 2008

Character Above, Below

If you're in insert mode, and you want to insert the character above the cursor, hit ctrl-y. To insert a character below the cursor, hit ctrl-e. Alternatively, if you want to grab entire words above and below, you can use the following mappings (which use text objects).

inoremap <C-Y> <Esc>klyiWjpa
inoremap <C-E> <Esc>jlyiWkPa

Rebuild a Debian Package

If you use Debian, Ubuntu, or some other Debian derived Linux distribution, there may come a time when a packages default configuration options don't meet your needs. This generally leaves you two options. You can either build from source and no longer have the luxury of the package manager maintaining your package, or you can rebuild the package with the custom options included in the build. This is easier than it sounds and really only involves a few steps. In this example, I'll rebuild netcat.

1) make sure you have the packages required to re-build a .deb

apt-get install devscripts build-essential fakeroot

2) fetch the source for the package to re-build

apt-get source netcat

3) fetch dependent packages for the build

sudo apt-get build-dep netcat

4) unpack vendor specific source archives

# determine appropriate dsc file
ls *dsc
netcat_1.10-33.dsc
# run dpkg-source on dsc file
dpkg-source -x netcat_1.10-33.dsc

5) cd into source directory

cd netcat-1.10

6) modify source if necessary, apply patches, etc...

7) set custom build options if desired (optional)

# fake DEB_BUILD_OPTIONS here, just for the sake of example...
DEB_BUILD_OPTIONS="--enable-sockets" CC=gcc-3.4 fakeroot debian/rules binary

8) build the package

dpkg-buildpackage -rfakeroot -b

9) install the package ;-)

sudo dpkg -i ../netcat_1.10-33_i386.deb

Quick Paste

You may have noticed that pasting outside text into Vim from insert mode can lead to awkwardly stair-stepped text. You may also know that this is easily avoidable via :set paste from normal mode. I paste from outside often enough, that I've added the following to my vimrc making it that much easier.

set pastetoggle=<F5>

Repeat Last Macro

There are times I want to repeat a macro just a few times. I know you can do count@{0-9a-z":*} but sometimes I don't feel like counting. You can use @@ to repeat the last macro, and it's an easy command to type very quickly, so it fits the bill.

Bash For Loops

I use bash and zsh quite a bit in system admin tasks. One thing I find myself needing to do repeatedly is run a command in a loop a given number of times. I'm very well acquainted with cron, but there are often instances where I prefer to run a bash loop inside of a screen session while logging to a file. This gives a lot more control over bleeding edge scripts, and it's easier to "check in" and see what's going on. Here are some simple ways to loop with bash:

# C style for loop
for ((i=0; i<=100; i+=1)); do
# some command
sleep 1
done

# quick loop

for I in 1 2 3; do
echo $I
sleep 1
done

# infinite loop

while [ 1 ]; do
# some repeated command
sleep 60
done

# file loop

for I in `ls *.txt`; do
echo "file $I"
done

Wednesday, April 23, 2008

Vim Visual Reference

For any visual learners in the crowd, here's a very nice visual Vim reference. There are a lot of cheat sheets out there, and I may add a few more to this post down the road.

Vim Visual Reference.

Friday, April 18, 2008

Finding a File's Owner

I generally gravitate towards Debian derived Linux distributions; although, I spend quite a lot of time on the Red Hat side of the tracks as well. My primary reason for liking Debian and Ubuntu is the package management. I plan on doing a whole series on apt and dpkg tricks in the future, but for the moment, here's a really easy way to find out what package a file belongs to.

travis@travis-desktop:/home/travis% dpkg -S /bin/bash
bash: /bin/bash

Only install from tarballs as a last resort, and you should be able to track down over 90% of the files on your system.