Personal Pastebin Tool


Here is a vim script that let’s you turn your website into a personal pastebin service so you can share text files with people. I’ll explain how it works and share some interesting ways to use it. Could be fun.

Why a personal pastebin?

Despite how we may appear sometimes, programmers are actually among the most social engineers. If you work around them long enough, you’ll need to share some code or the contents of some configuration file. It’s generally considered to be bad manners to send anything more than a line or two to someone in a chat client. For this reason, people have created pastebins. Just share a link where the contents are displayed and anyone interested can take a look.

I was using some other pastebin services for awhile, but the web interfaces are tedious to use. There are some good command line tools for hastebin with vim integration if you look for them and don’t mind using a public pastebin. But if you already have a webserver, why not use it to host your pastes? Here are the benefits of using the script:

  • Share your code in just a few keystrokes.
  • Use your own syntax hilighting and color scheme.
  • Share anything that fits in a vim buffer, even a shell (more on that later)
  • It’s a subtle way to promote your site among your friends.

Getting Started

You’ll need something to alter the clipboard selection. This script uses xclip, but you can swap if for xsel or pbpaste pretty easily.

Here is the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
" Personal Pastebin
" Based on <http://connermcd.com/blog/2012/09/17/personal-pastebin-system/>
com! -range=% HtmlPaste <line1>,<line2>call HtmlPaste()
noremap <silent> gH :HtmlPaste<cr>
fun! HtmlPaste() range
  " *********
  " Settings

  let localPaste = "~/projects/octopress/source/paste"
  let remotePublic = "tony@example.com:/srv/www/example.com"
  let remotePasteUrl = "http://example.com/paste"

  " *********

  let hasRN = &relativenumber
  let hasNumber = &number

  " unset line numbers - makes copy/paste easier for people
  set norelativenumber
  set nonumber

  " convert to html
  exe a:firstline.",".a:lastline."TOhtml"
  " get a random alphanumeric string for the filename
  let pasteName = system("cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1 | perl -ne 'chomp and print'")
  " save to local paste directory
  exe "sav! " . localPaste . "/" . pasteName
  " close window the above command creates
  wincmd c
  " sync local/public paste 
  exe "silent !rsync -a " . localPaste . " " . remotePublic
  " copy to clipboard
  exe "silent !echo -n '" . remotePasteUrl . "/" . pasteName . "' | xclip -selection clipboard"

  " restore line numbers
  if hasRN
    set relativenumber
  endif
  if hasNumber
    set number
  endif

  redraw!
endfun

It’s based on this blog post I found. Change the settings to point to your server, put this somewhere in your vim path, such as in your .vimrc, and you are good to go. Type gH or :HtmlPaste and when the script is finished, a link to the paste will be in your clipboard. It will look just like it does in vim, except the line numbers will be removed. If you have something selected, only the selected portion will be put on the pastebin. I think that’s pretty cool.

If you have any questions or if you think of ways to improve the script, post a comment here or on the script’s gist page. I might turn this into a vim plugin at some point in the future.

Advanced Usage

As I find new ways to use the script, I’ll put them here.

Use with a Conque Shell

Now suppose you want to share the output of some command with people. Maybe you are asking someone for help and want to share the error message that results from running some command, or maybe you have found some interesting result from performing some analysis in your data with iPython.

Conque is a vim plugin that uses a buffer as the output of a terminal emulator. For example,

:ConqueTerm zsh
:ConqueTerm mysql -h localhost -u joe -p sock_collection 
:ConqueTerm ipython

Now you can run the command, highlight the important part, and it’s easy to share with people.


Comments