All posts by marco

Window stacking in VIM

Stolen shamelessly from this site.

map <c -J> </c><c -W>j</c><c -W>_
map </c><c -K> </c><c -W>k</c><c -W>_
set wmh=0
</c>

The first two lines allow you to switch between splits much more smoothly — just press to open and maximize the split below the current one and to open and maximize the split above the current one. I chose these mappings because they correspond to vi’s default up and down keys, you might want to use different key combinations if you’ve ever used an editor that had hotkeys for moving from one open file to another.

The last line allows splits to reduce their size to a single line (which includes the filename and position); this saves a lot of space when you have many splits open. By default, vim forces splits to include an additional line that contains the line of text the cursor was on in that file.

This gives you some sort of stacking functionality like in wmii

Too Many Links

There are too many hard links to a file or directory on a filesystem. The exact number allowed is file-system dependent. Eg /usr/src/linux-2.4.19/include/linux/sysv_fs.h contains

enum {
 
XENIX_LINK_MAX = 126, /* ?? / SYSV_LINK_MAX = 126, / 127? 251? / V7_LINK_MAX = 126, / ?? */ COH_LINK_MAX = 10000,
 
};

while /usr/src/linux-2.4.19/include/linux/ext3_fs.h has

define EXT3_LINK_MAX 32000

and /usr/src/linux-2.4.19/include/linux/reiserfs_fs.h has

define REISERFS_LINK_MAX (MAX_US_INT - 1000)

(for maximum unsigned integer on the system).

This could be caused by a directory having too many subdirectories (each subdirectory has .. as a hardlink to it’s parent directory which causes that directory’s hardlink count to be increased by one. So yes, this does mean that you are limited to 32000 subdirectories in one directory in ext3, even if you have hashdirs enabled.) As a consequence of this you can stat(2) a directory and add one (for ..) and you will know how many directories are in the current directory. (or subtract one (for .) to find out how many subdirectories there are).

Some file systems (such as FAT) don’t have hardlinks so the hardlink count can’t overflow, and you can’t rely on the hardlink count of a directory to be representive of how many subdirectories it has.

What a confusing world we live in.

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>