Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

Friday, March 05, 2010

Funny C tricks

Taken from Bill Rowan's Stanford ACM presentation:

The "downto" operator:

int main()
{
int i = 5;
while(i --> 0) // --> is the downto operator!
{
printf("%d\n", i);
}
return 0;
}

Cast any type to "bool" type (that is, 1 or 0):
!!foo
"Computed goto" (compiler-dependent && unary operator):
void print_loop(int s, int e) {
assert(s < e);
top:
printf("%d\n", s);
goto *( &&top + ( !!(s++/e) ) * ( &&end - &&top ) );
end:
;
}

Saturday, August 19, 2006

VIM (and some vi)

Everybody who knows me knows that I love vim. Vim is a powerful version of the vi editor. It usually comes with any *NIX instillation. It rules. Here are some links, and some of my favorite (non-obvious) tricks. If you are new to all of this, a good place to start is the VI lovers home page or, if you are in a hurry, the quick reference card.

VIM

Plain old VI

Tricks
  • Swap two characters xp
  • Swap two lines ddp
  • Reverse the order of all lines :g/^/m0/
  • Remove all lines with only whitespace :g/^\s*$/d
  • Move all lines onto one logical line :%j
  • Remove all blank lines :g/^$/d
  • Insert a newline at the end of every line :%s/$/^m/ (keystrokes to get this one are: Ctrl+V, Ctrl+M)
  • Text completion before, after cursor: Ctrl+N, Ctrl+P (thanks to neha for this link )
  • Get a command prompt :shell
  • Execute a shell command :!<command>
  • Append the contents of a file into your vi session :r!cat <file>
  • Omni-completion Ctrl+X Ctrl+O
  • Whole line completion Ctrl+X Ctrl+L
  • Search for a word forward under the cursor *
  • Search for a word backwards under the cursor #
  • Move to a percentage of the file: type the number, then hit %