:

ex commands

filetype plugins

syntax, highlighting, 2HTML

2Html, CSS and XHTML |

modes

moving

plugins

search & replace

substitution using Ruby or Perl | replace variable number of spaces with 0 | incrementing numbers or letters in substitutions

starting

..watching using strace | CLI start options |

tabs

tags

:

ex commands

filetype plugins


2Html, CSS and XHTML

Purpose

To publish code written in Vim using CSS and XHTML.

Why CSS?

I use CSS so I can use 2HTML from any PC and generate the same syntax colors. I achieve this by only creating the CSS file on the PC with the desired color scheme.

Add the following to your .<g>vimrc file.

let html_number_lines = 0
let html_use_css = 1
let use_xhtml = 1

(Must be added to all .<g>vimrc files for each different instance/location of Vim where you intend to run 2HTML.)

Running 2HTML

Either select 'Convert to HTML' from the 'Syntax' menu or run the ex command :TOhtml. You can specify a range if you want to restrict the output to specific lines. :10,40TOhtml for eg. to print lines 10 to 40 inclusive. Or selecting a visual range and typing : will show an ex command like '<'>. Type TOhtml and them hit enter.

Extracting the CSS rules

Note: If like me you run 2HTML from numerous PCs that may not utilise the same colorscheme (either forced or otherwise) restrict this step to the PC with the desired colorscheme!

At the top of the generated html file there should be a <script> section that includes rules similar to .PreProc { color: #fcaf3e; }. Cut all these to a CSS file. Each time you use 2HTML check the CSS file to see if other rules have been defined. This depends on Vims syntax files so different filetypes may generate new rules. For instance if you've defined 'hi rubyKeyword' in a config file and you run 2HTML on a ruby file, a .rubyKeyword { color: #xxxxxx; } rule will be generated. Obviously you only need to copy new rules to the CSS file.

Example (code.css)

.Boolean { color: #ff0000; background-color: #fff; }
.Comment { color: #336699; background-color: #fff; }
.Conditional { color: #4169e1; background-color: #fff; }
.Constant { color: #a020f0;background-color: #fff; }

Including the CSS file

In the <head> section of each page include:
        <style type="text/css">
            @import url(scripts/code.css);
        </style>

Or if you already have a <script> section just include the @import line. Obviously it needs to specify the correct path!

To include code snippets in html pages

Once you have the CSS file and have linked to it in the <head> section you simply run 2HTML and copy the resulting <pre> </pre> tags (and everything between them) into the html file.

:h :TOhtml

modes

moving

plugins

search & replace

Substitution using Ruby or Perl

Purpose

Simple Integer arithmetic is possible in Vim using:

:%s/\d\+/\=submatch(0)+2/g

for example to add 2 (\=submatch(0)+2) to one or more digits (\d\+).

Where Float arithmetic is required, pipe the substitution to Perl or Ruby. Both examples below divide by 2

Perl
:!perl -pe 's/\d+/$&\/2/eg'
Ruby
:%!ruby -pe 'gsub(/\d+/) {|m| m.to_f/2}'

But they give erroneous results for anything other than simple arithmetic

:!perl -pe 's/\d+.?\d*/$&\/2/eg'

or

<:%!ruby -pe 'gsub(/\d+.?\d*/) {|m| m.to_f/2}'

are better but still not perfect.

:h !

Replace variable number of spaces with 0

Change this:

      time cost is 3.  1234
      time cost is 3.   234
      time cost is 3.     5
      

To This:

      time cost is 3.001234
      time cost is 3.000234
      time cost is 3.000005
      
%s/\(\d\. \{-}\)\@<= /0/g
:h @<= (and good luck understanding that one ;-))

incrementing numbers or letters in substitutions

How to change this

             x() = "Kunden_Nr"
             x() = "Barcode"
             x() = "Vorname"
             x() = "Nachname"
             x() = "Firma"

to this...

             x(1) = "Kunden_Nr"
             x(2) = "Barcode"
             x(3) = "Vorname"
             x(4) = "Nachname"
             x(5) = "Firma"

At the 1st line of your text range set a mark (ma used in the example).

:[range]s/(\zs/\=line(".")-line("'a")+1/

If you visually select the lines [range] will be '<'>. Or you can specify the lines like

:1,10s/(\zs/\=line(".")-line("'a")+1/
Interpretation

\zs and it's opposite \ze (not used above) mean "start search here!" and "end search here!" respectively.

So the search part of the expression above /(\zs/ means "look for an opening bracket and start the search". Note: it hasn't actually searched for anything specifically! It's effectively saying "I want you positioned after that opening bracket when I apply the replacement"

\= in the replacement means "evaluate the following expression". Which uses the current line number line("."), and subtracts the line number where we set the mark (line("'a")). If we wanted 0 based results (0, 1, 2, 3 for eg) that would be it. But we want the results to start at 1 so we add 1 to the expression (+1).

:h \zs
:h \ze
:h \=
:h line
:h mark

Antony Scriven (one of the regex guns) provides another way to address the problem above.

:let i=1 | [range] g/(\zs/ s//\=i/ | let i=i+1
Interpretation of Antonys' method

No marks are harmed as he uses a variable instead.

Note: the pipes (|) are used to separate commands.

He initiates the variable to 1 and then uses a global substitution (a linewise substitution) where the search pattern is similar to the example above ((\zs), but the replacement uses a normal s/// type search and replace (s//\=i). This is saying "search for nothing and replace with the value of the variable i". Which for each line is incremented by 1 (let i=i+1).

:h :bar
:h :global
:h :let
:h \zs
:h \=
Can this be used to increment letters of the alphabet?

Yes! This produces:

             x(a) = "Kunden_Nr"
             x(b) = "Barcode"
             x(c) = "Vorname"
             x(d) = "Nachname"
             x(e) = "Firma"
:let i=97|[range]g/(\zs/s//\=nr2char(i)/|let i=i+1

Caveat: Obviously only effective for 26 replacements! A small 'a' is ascii 97. A capital 'A' is 65. So let i=65 in the expression to produce:

             x(A) = "Kunden_Nr"
             x(B) = "Barcode"
             x(C) = "Vorname"
             x(D) = "Nachname"
             x(E) = "Firma"
:h nr2char()
:h ga

starting

Watching using strace

Open two terminals.
Run the "tty" command in one to find out its name. (/dev/pts/1).

In the other terminal, go:
strace -f -e trace = file vim some-text-file 2>/dev/pts/1
Then save the file and watch the second terminal to see where the pauses take place.

startup options

There are times when you need to start Vim as 'cleanly' as possible. By clean I mean unaffected by config files and/or plugins.

-u NONE (don't load vimrc or plugins)
-u NORC (don't load vimrc but load plugins)

tabs

tags