Category Archives: misc

vim tips

replace foo with bar from line 10 to 20:

:10,20s/foo/bar/g

Using !cmd to apply a command to a visual selection or a whole file. (e.g. “!sort -nr“)

'gd' (go to definition)

:earlier 5m takes you back 5 minutes in the editing session (ignoring undos/redos)

in cmd mode ctrl-r ctrl-w pastes the word that’s currently under the
cursor in your active buffer. So if you want to replace aVeryLongWord
you’d do :%s/ctrl-r ctrl-w/ctrl-r ctrl-w edit a bit/g.

^a and ^x to incr/decr a number in the text.

Sometimes after making many edits to an open file, you end up wondering what changes you made since it was last saved. In that case, use

:w !diff – %

qz ” record keystrokes into “z register;
” hit ‘q’ again to stop
@z ” play back macro in “z
10@z” play back macro in “z 10 times
@@ ” play back last macro

:registers z ” show macro/register contents

gt ” go to next tab
gT ” go to previous tab

Vim cheatsheet

logging with script

While you can log everything with screen’s logging functionality, or tee it to some file, you can also use script.

Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an inter‐active session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

% script foo
Script started, file is foo
% ps
  PID TTY          TIME CMD
11448 pts/3    00:00:00 zsh
11511 pts/3    00:00:00 ps
%
Script done, file is foo
 
% cat foo
Script started on Fri Jun 18 10:13:14 2010
% ps
  PID TTY          TIME CMD
11448 pts/3    00:00:00 zsh
11511 pts/3    00:00:00 ps
% <ctrl +d>
Script done on Fri Jun 18 10:13:25 2010                            
</ctrl>

c++ notes

Stupid C++ syntax:

Class::StaticFunctionName 
 
Table* t = new Table()
t->FucntionName()
 
class BaseClass
{
  public:
    BaseClass()
  void something();
private:
  int foo;
  int bar;
};
 
//instantiate:
 
class OtherClass : public BaseClass
{
  public:
  int meh;
  void duh();
 };
 
 
scoped_ptr<>

create graphviz image from textfile

inspired by text2mindmap.com i decided to create something similar in python.

Here’s the result:

Code text2mindmap.py

#!/usr/bin/python
 
import pydot
import sys
 
 
def ReadFile(filename):
  fh = open(filename)
  return [x.rstrip() for x in fh.readlines()]
 
 
def CreateEdges(lines):
  edge_list= ['' for x in range(100)]
 
  edges = []
  for line in lines:
    pos = line.count('\t');
    edge_list[pos] = line.replace('\t', '')
    if pos:
      edges.append((edge_list[pos -1], edge_list[pos]))
  return edges
 
 
def CreateGraphFromEdges(edges, output):
  g=pydot.graph_from_edges(edges, directed=True)
  return g.write_png('%s.png' % output, prog='dot')    
 
 
def main(argv):
  CreateGraphFromEdges(CreateEdges(ReadFile(argv[1])), argv[2])
 
 
if __name__ == "__main__":
   main(sys.argv)

Source file months.txt

Months of the year
	Spring
		March
		April
		May
	Summer
		June
		July
		August
	Autumn
		September
		October
		November
	Winter
		December
		January
		February

How to generate the image:

$ ./text2mindmap.py months.txt months

pydot + graphviz

Install pydot
Install graphviz

$ python
Python 2.5.1 (r251:54863, Feb  9 2009, 18:49:36) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pydot
>>> edges=[('1','2'), ('1','3'), ('1','4'), ('3','4')]
>>> g=pydot.graph_from_edges(edges)
>>> g.write_jpeg('graph_from_edges_dot.jpg', prog='dot')
True
>>>

result:
example

rxvt-unicode with 256 colors

# Make a place for rxvt-unicode
mkdir rxvt-unicode
cd rxvt-unicode/
 
# Get the source
apt-get source rxvt-unicode
cd rxvt-unicode-8.4/
 
# Apply 256 color patch, it's included with the source
patch -p1 < doc/urxvt-8.2-256color.patch
 
# Make sure you have all depdencies to build it
sudo apt-get build-dep rxvt-unicode
 
# Build it
dpkg-buildpackage -us -uc -rfakeroot

This will build 3 packages.