7

For the last few days I've been forcing myself to switch from Textmate to Vim. for the most part, my efficiency is pretty much the same after some heavy Vim configuration. one aspect of my workflow I haven't found a replacement for is the drag and drop of files into Textmate, where images would be transformed into img tags and files would be transformed into require calls.

How can I dynamically build an image tag, and an include/require statement?

ergosys
  • 47,835
  • 5
  • 49
  • 70
Matt Ryan
  • 1,717
  • 2
  • 20
  • 30
  • Related: http://stackoverflow.com/questions/1693389/vim-browser-plugin-to-execute-commands-on-files/1696072#1696072 – Matteo Riva Apr 22 '11 at 14:31

1 Answers1

8

I've been thinking about that automatic <img> building since switching.

Vim has a built-in solution for one part of the problem: the omni-completion engine allows you to complete the path of a file you want to include.

If you begin to type a "path-like" string — say <img src="images/ — hit <C-x><C-f> for a little list of possible completions. Coupled with an auto-completion plugin like ACP, and snipMate the process is really fast.

But it doesn't help at all for width and height.

A possible solution would be to use snipMate for the <img> snippet, use <C-x><C-f> for the image's path, capture the path and feed it to some command line utility, fill the width and height attributes with its output.

EDIT

It turns out others have already explored a similar road, this script is interesting but it doesn't work properly with file paths containing spaces. Here is a slightly modified version that works reasonably well. This other one seems to be very cool but it's missing a crucial piece.

END EDIT

ANOTHER EDIT

I've added this snippet to ~/.vim/bundle/snipMate/snippets/html.snippets (attention the indent before $ is a <Tab>):

snippet img custom
  ${1:`HTML_insertImg()`}

which works well but prepends the output of HTML_insertImg() with a 0 like this:

0<img src="future_timeline.png" width="612" height="6370" alt="" />

This 0 is a bit of a problem though. The system is obviously not perfect but once I'll get rid of this nagging 0 it's going to fit rather nicely within the rest of my snipMate-based process.

By the way here is a second modified version of the script I use.

END ANOTHER EDIT

EXTENDED 12" RE-EDIT VERSION

With:

you can:

  • open NERDTree, select an image and hit b to have an almost complete <img src="imagename.jpg" width="128" height="256" alt="" /> tag pasted at the cursor's position in the previous window
  • or, if your Vim flavour allows it, type :IMG<CR> to open a native file selection dialog, chose an image and have an almost complete <img src="imagename.jpg" width="128" height="256" alt="" /> tag pasted at the cursor's position in the current window
  • or, if you are in a terminal, type :IMG path/to/file.jpg to have an almost complete <img src="imagename.jpg" width="128" height="256" alt="" /> tag pasted at the cursor's position in the current window

The snipMate solution is not really baked right now.

Thanks to Petr Mach and @Matteo Riva for actually doing most of the work.

And yes, I'm obviously not a Vim expert.

END EXTENDED 12" RE-EDIT VERSION

Community
  • 1
  • 1
romainl
  • 186,200
  • 21
  • 280
  • 313
  • thanks for the auto-completion tip, really nice but, the dimensions are harder to get than the filenames. which is what i'm looking for. – Matt Ryan Apr 19 '11 at 22:25
  • @matt ryan, I'm still a Vim noob myself. Actually, finding a solution to this has been on my TODO list since months. I'll need to brush-up my regex (non-)skills, I think. – romainl Apr 20 '11 at 05:39
  • in the meantime i'm using the identify package like so: $ identify -format "width=\"%w\" height=\"%h\"" background.png | pbcopy – Matt Ryan Apr 20 '11 at 22:18
  • my new and perfected method, called via `i image.jpg` the function I added to my .profile `i() { identify -format "width=\"%w\" height=\"%h\"" "$1" | tr -d '\n' | pbcopy ; }` for some reason the identify output contains a new line, hence the tr to strip it before it gets pasted to the os x clipboard (`pbcopy`). called in vim via `!i image.jpg` and pasted normally. if someone can figure out how to get it to input at the cursor without pasting they got the answer. – Matt Ryan Apr 21 '11 at 02:04
  • @matt ryan Yes, I've been trying with `identify` but with some native Vim-fu instead of relying on a bash script. I've come up with a bit of regex to get the content of `src=""` and I'm trying to feed it to identify in a reliable way to do something like `:s/width="\zs.*\ze"\sh/`. Right now I'm only playing with this idea with oneliners but I should probably make it into a proper `Function()`. – romainl Apr 21 '11 at 06:28