Surround a string with hashes

CJam, 22 20 bytes

qa{4/3*' +f+W%z}8/N*

Test it here.

Explanation

How do you wrap a 2D grid of characters in one layer of spaces (or any other character)? Correct: four times, you append a space to each line and then rotate the grid by 90 degrees. That's exactly what I'm doing here with eight rotations: four for spaces, four for #:

qa      e# Read the input and wrap it in an array, to make it a 2D grid.
{       e# Execute this block for each value from 0 to 7.
  4/3*  e#   Divide by 4, multiply by 3. Gives 0 for the first four iterations and
        e#   and 3 for the other four.
  ' +   e#   Add the result to a space character (space + 3 == #).
  f+    e#   Append this to each line of the grid.
  W%z   e#   Reverse the lines, then transpose the grid - together these rotate it.
}8/
N*      e# Join the lines of the grid by newlines.

vim, 28 27 keystrokes

I# <esc>A #<esc>Y4PVr#G.kwv$3hr kk.

Assumes input is provided as a single line of text in the currently open file.

Explanation:

I# <esc>        put a "#" and space at the beginning of the line
A #<esc>        put a space and "#" at the end of the line
Y4P             copy the line 4 times
Vr#             replace the entirety of the first line with "#"s
G.              do the same for the last line
kwv$3hr<space>  replace middle of the fourth line with spaces
kk.             do the same for the second line

This can also be run as a "program" like so:

echo 'Hello World' | vim - '+exe "norm I# \<esc>A #\<esc>Y4PVr#G.kwv$3hr kk."'

Which is a bit convoluted, but it works.


pb - 89 bytes

v[4]w[Y!-1]{b[35]^}w[B!0]{t[B]vvv>>b[T]^^^<}v>>>w[Y!4]{b[35]v}w[X!0]{b[35]^[Y]b[35]v[4]<}

This is the kind of challenge pb was made for! Not that it's competitive for this kind of challenge or anything. It's still a horrible golf language. However, challenges like this are a lot less of a pain to solve in pb than others are. Since pb treats its output as a 2D canvas and is able to write to any coords, anything involving positioning text/drawing around text (i.e. this challenge) is handled rather intuitively.

Watch it run:

This visualization was created with an in-development version of pbi, the pb interpreter. The line with the blue background is Y=-1, where input is stored when the program starts. The rectangle with the red background is the current location of the brush. The rectangles with yellow backgrounds are anywhere the ascii character 32 (a space) is explicitly written to the canvas. Any blank spaces without this background actually have the value 0, which is converted to a space.

Here's the code with the comments I used while writing it, with some thematically relevant section headers ;)

################################
#                              #
# Handle first column oddities #
#                              #
################################
v[4]           # Start from Y=4 and go up (so we land on input afterwords)
w[Y!-1]{       # While we're on the visible part of the canvas
    b[35]^         # Write "#", then go up
}

#########################
#                       #
# Insert text of output #
#                       #
#########################

w[B!0]{        # For each character of input
    t[B]           # Save input char in T
    vvv>>          # Down 3 + right 2 = where text part of output goes
    b[T]^^^<       # Write T and go to next char
}

###############################
#                             #
# Handle last column oddities #
#                             #
###############################

v>>>           # Go to Y=0, X=(X of last text output's location + 2)
w[Y!4]{        # Until we reach the last line of output
    b[35]v         # Draw "#", then go down
}

###########################
#                         #
# Loop to finish Y=0, Y=4 #
#                         #
###########################

w[X!0]{        # Until we've gone all the way left
    b[35]^[Y]      # Print "#" at Y=4, go to Y=0
    b[35]v[4]      # Print "#" at Y=0, go to Y=4
    <              # Move left, printing until output is complete
}