5 no warnings "qw"; # you know what you doing
6 no warnings "uninitialized"; # save some useless checks for more legible code
8 # 2006-03-29 ~20:00 started work
9 # 2006-03-30 11:58 html version
10 # 2006-05-20 03:44 perl version
11 # 2007-02-07 07:34 cleaned up
12 # 2007-02-25 04:00 v1.0: git repository
15 # - use js to detect optimal key size
16 # - style for .undo (maybe just some css3-only shadow)
17 # - fix mode switching in konq (used to work)
18 # only when <wbr>s present?! use browser sniffing to force ?ascii=0 :(
19 # - safari loading bug?
20 # - good case for gzip compression
21 # - link to source tarball
22 # - default stylesheet selectable
23 # - catch and execute pressed keys
24 # - ghosting option documented and default
25 # - option to follow aliases (default on?)
26 # - custom row combinations (eg for comparing different modes)
31 if (exists $get{ascii}) {
32 $ascii = $get{ascii} ne "0"; # manual override
33 } elsif (defined $ENV{HTTP_ACCEPT_CHARSET}) {
35 for (split ",", $ENV{HTTP_ACCEPT_CHARSET}) {
36 $ascii = 0, last if $_ eq "*" or m/utf-?8/i;
40 my $charset = $ascii ? "us-ascii" : "utf-8";
41 my $ctype = "text/html; charset=$charset";
42 $header{content_type} = $ctype;
44 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
45 "http://www.w3.org/TR/html4/loose.dtd">
49 <title>vi cheat sheet</title>
50 <meta http-equiv="content-type" content="<:= $ctype :>">
51 <link rel="stylesheet" type="text/css" media="all" href="base.css">
52 <!--[if lte IE 6]><style> .help dl.legend dt {margin:0 0 1px} </style><![endif]-->
53 <!--[if lte IE 7]><style> .help dl.legend dd {float:none} </style><![endif]--><:
54 my %styles = map {$_ => $_} qw(dark circus mono);
55 our $style = exists $get{style} && $styles{$get{style}} || "light";
56 printf(qq{\n<link rel="%s" type="text/css" media="all" href="%s" title="%s">},
57 $_ eq $style ? "stylesheet" : "alternate stylesheet", "$_.css", $_
60 our $showkeys = exists $get{keys} && $get{keys} ne "0";
61 print "\n<style> .no {visibility:hidden} </style>" unless $showkeys;
62 print "\n<style> .no, .alias {opacity:.5} </style>"
63 if $showkeys and $get{keys} eq "ghost";
67 function setmode(classname) {
68 // set style for each #rows>li>ul>li to display:none unless it matches classname
69 var showclass = classname ? ' '+classname+'(?!\\w)' : '^$';
70 var parentskip = /^keys/;
71 var row = document.getElementById('rows').firstChild;
73 if (row.tagName == 'LI' && row.firstChild.tagName == 'UL'
74 && !row.firstChild.className.match(parentskip)) {
75 var el = row.firstChild.firstChild;
77 if (el.tagName == 'LI') {
78 el.style.display = el.className.match(showclass) ? 'block' : 'none';
80 } while (el = el.nextSibling);
82 } while (row = row.nextSibling);
84 // update H2 to reflect the first part of a currently active (but hidden) H3
85 var h3s = document.getElementsByTagName('H3');
86 for (var i = 0; i < h3s.length; i++) {
87 if (h3s[i].parentNode.style.display != 'block') continue;
88 document.getElementsByTagName('H2')[0].innerHTML = h3s[i].firstChild.data;
95 <h1>vi/vim cheat sheet</h1>
97 <h2>normal mode (default)</h2>
102 <ul class="keys omni">
103 <li class="mo" onclick="setmode()"><b>Esc</b> normal mode
104 <!-- not as static anymore, but never bothered; just see ^[ -->
110 arg => $ascii ? "." : "·", # described as 'dot'
111 motion => $ascii ? "|" : "↕",
112 alias => $ascii ? "see: " : "»",
113 up => $ascii ? "up" : "▲",
114 right => $ascii ? "right" : "▶",
115 down => $ascii ? "down" : "▼",
116 left => $ascii ? "left" : "◀",
117 sep => $ascii ? "*" : "•",
118 _ => exists $get{ascii} && !$ascii ? "\x{200b}" : "<wbr>",
119 # use the correct ZWNJ only when unicode is forced on
120 # default to use unofficial html for best support
123 my %keys = do "vim-cmds.inc.pl";
125 my @casedesc = qw(ctrl shift);
126 my @rowdesc = qw(numeric top home bottom);
127 my %keyrows = do "vim-keys.inc.pl";
130 my ($key, %tree) = @_;
131 $key =~ s/(\S*?)(\^?\S)($|\s.*)/$2/;
133 return [] unless defined $keys{$mode}{$key};
134 return $keys{$mode}{$key} if ref $keys{$mode}{$key};
135 return if $tree{$key}++; # endless loop failsafe
136 return keyunalias($keys{$mode}{$key}, %tree);
148 our $map = defined $keyrows{$get{map}} ? $get{map} : "qwerty";
149 my $keyrows = $keyrows{$map};
151 for (my $row = 0; $row <= $#$keyrows; $row++) {
152 my $keyrow = $keyrows->[$row];
153 print qq{<li class="row row$row"><ul>\n};
154 while (my ($mode, $modekeys) = each %keys) {
155 for (my $case = 0; $case <= $#$keyrow; $case++) {
156 my $keycase = $keyrow->[$case];
159 printf(qq{\t<li%s><h3>%s</h3>\n}, # XXX insert here to fix msie<=6
160 $mode eq '' ? '' : sprintf(
161 ' class="%s"', "mode mode" . escapeclass($mode)
163 sprintf('%s<small>: %s</small>',
164 $modekeys->{desc} || "mode $mode",
165 "$rowdesc[$row] row $casedesc[$case]"
168 my $modeclass = "keys";
169 $modeclass .= " lead" if defined $modekeys->{lead}; # leading command key shown
170 $modeclass .= " $casedesc[$case]" if defined $casedesc[$case];
171 print qq{\t\t<ul class="$modeclass">\n};
173 for my $key (@$keycase) {
174 my $keyinfo = $modekeys->{$key};
175 $keyinfo = [ $sign{alias}.$keyinfo, keyunalias($keyinfo)->[1] . " alias" ]
176 if defined $keyinfo and not ref $keyinfo; # alias
177 my ($desc, $flags, $mnem) = @$keyinfo if defined $keyinfo;
178 defined $desc or $flags = $key eq '^0' ? "ni" : "no";
180 my $keytxt = $modekeys->{lead} . Entity($key) if $key ne '^0';
181 $keytxt .= $sign{arg} while $flags =~ s/ ?\barg\b//; # argument
182 $keytxt .= "<small>$sign{motion}</small>" if $flags =~ s/ ?\bargm\b//; # motion argument
183 $keytxt =~ s{\^(?=.)}{<small>^</small>}; # element around ctrl-identifier
184 my $onclick = $flags =~ s/ ?\bmode(\S*)// && defined $keys{$1} && sprintf(
185 ' onclick="setmode(%s)"',
186 $1 eq '' ? '' : sprintf("'mode%s'", escapeclass($1))
188 my $keyhint = defined($mnem) && qq{ title="$mnem"};
190 print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
191 print ' ', $desc if defined $desc;
195 print qq{\t\t</ul>\n};
209 <dl class="legend legend-types">
211 <dd>Info command: shows/does something without altering anything.
212 <dt class="pm">motion
213 <dd>Moves the cursor, or defines the range for an operator (<:= $sign{motion} :>).
214 <dt class="po">positioning
215 <dd>Other movement (jumps, window (re)positioning).
216 <dt class="co">command
217 <dd>Direct action command.
218 <dt class="mi">ins mode
219 <dd>Enters Insert or Replace mode.
221 <dd>Enters a different mode.
222 <dt class="mv">vis mode
223 <dd>Enters Visual or Select mode.
224 <dt class="me">key cmd
225 <dd>Additional key commands (click for overview).
230 <dl class="legend legend-options">
231 <dt>key<:= $sign{arg} :>
232 <dd>Commands with a dot need a char argument afterwards.
233 <dt>key<:= $sign{motion} :>
234 <dd>Requires a motion afterwards, operates between cursor and destination.
236 <dd>Not in original Vi (assessment incomplete).
237 <dt class="vim7">vim7
238 <dd>New in vim version 7.x.
241 <ul class="legend legend-set">
242 <li>keyboard <strong>map</strong> is
243 <:= $get{map} ? "set to " : "" :><em><:= $map :></em>
244 <li><strong>ascii</strong> mode is
245 <:= exists $get{ascii} ? "forced " : "" :><em><:=
246 $ascii ? "on" : "off" :></em>
247 <li><strong>keys</strong> are
248 <em><:= $showkeys ? "always shown" : "hidden if unassigned" :></em><:=
249 exists $get{keys} ? "" : " by default" :>
250 <li>default <strong>style</strong> is
251 <:= defined $get{style} ? "set to " : "" :><em><:= $style :></em>
257 <a href="http://<:= $ENV{SERVER_NAME} :>/vim-cheat">shiar.demon.nl/<b>vim-cheat</b></a>
258 <a href="vim-cheat.tar.gz"><:= "v$VERSION" :></a>
259 created by Shiar <:= $sign{sep} :> last update <:
260 use Time::Format qw(time_format);
261 print time_format("yyyy-mm-dd", (stat "vim-cmds.inc.pl")[9]);