vim

HTML syntax highlighting for Silverstripe .ss template files in vim

Posted in silverstripe, vim, xhtml on August 4th, 2009 by David – 1 Comment

By default vim opens .ss files with some other file format syntax highlighting.

To enable HTML (actually XHTML) syntax highlighting on your .SS Silverstripe template files, create (or edit) your ~/.vim/filetype.vim file. Then enter this:

au BufNewFile,BufRead *.ss      setf xhtml

Then open a .ss file and it’ll give you nice HTML syntax highlighting. And because it’s in your home directory, it’ll keep working even after you upgrade vim.

Strip <span> tags from HTML in vim

Posted in vim, xhtml on April 6th, 2009 by David – Be the first to comment

When upgrading a website you might see source code like this:

<span style="font-family: Arial; color: #0000ff; font-size: small;">Some text goes here</span>

You’re using CSS now and all those <span> tags are ruining it. In gvim, do this search and substitute:

%s/<span.\{-}>//g

Then to get rid of the </span> tags, do this:

:%s/<\/span>//g

Strip ^M from file in vim

Posted in vim on February 20th, 2009 by David – Be the first to comment

If there are ^M at the end of every line when you view them in vim, you can do this:
:%s/^M//g
To get the “^M” bit, hit ctrl-v and then ctrl-m.

Assorted Handy vim Commands, Part 1

Posted in vim, xhtml on January 28th, 2009 by David – 1 Comment

To reverse the order of lines, eg 1-5
: 1,5 g/^/m0
For example,

one
two
three
four
five

becomes

five
four
three
two
one

To remove blank lines
: %g/^$/d

Delete all lines that don’t contain “string”
: %v/string/d

Turn all links in an HTML file into ‘#’

Posted in vim, xhtml on January 24th, 2009 by David – Be the first to comment

Replace all in the current file instances of:

<a href=”link_to_somewhere.html”>link</a>

with:

<a href=”#”>link</a>

:%s/\(a href="\)\(\S\+\)\("\)/\1#\3/g

This is great if you’ve got a big HTML file that you want to demo to a client where you don’t want the links to do anything when clicked.

You could also use javascript to do the same thing – just “return false” for all <a> elements on the page.

vim: Quickly assign POST variables in PHP

Posted in vim on January 22nd, 2009 by David – Be the first to comment

You’ve got a web form with lots of fields, and you want to POST them to a PHP script. Open vim, and list the INPUT tag’s NAME attributes, one per line.

<?php
firstname
lastname
address1
address2
state
postcode
?>

Now with some search-and-replace magic we can save ourself a lot of boring typing. Hit ‘escape’ to get out of insert mode, type a colon (”:”) and copy-paste this:

%s/^\(\S\+\)/\$\1 = \$_POST\[\'\1\'\];

I won’t bother explaining it unless someone asks. But what you should end up with is this:

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$state = $_POST['state'];
$postcode = $_POST['postcode'];
?>

Perhaps not worth it when you have 6 variables like this example; but quite handy when you have 60 variables!