Vim Essential Plugin: Surround
videos

Vim Essential Plugin: Surround

Tutorial Details
  • Plugin: Surround
  • Screencast Length: 4 Minutes
This entry is part 2 of 8 in the Vim Essential Plugins Session
« PreviousNext »

Surround.vim, by Tim Pope, is easily one of the most useful plugins available. As the name suggests, it allows you to surround, update, and delete wrapping characters and tags with ease!


Usage

Let’s say you have the string “Hello World.” If we want to wrap the word, “World,” within an <em> tag, we can type: viws<em>. This stands for “move into visual mode, select the inner word, and then surround it with an <em> tag.”

Hello <em>World</em>   

But let’s say that we later change our mind, and wish to change the wrapping <em> to a <strong> tag instead. With the surround plugin, that’s a cinch: cst<strong>. Again, this means: “Change the surround tag to an <strong>.”

Hello <strong>World</strong>   

But, ya know, we can never make up our mind. A day later, we decide to remove the wrapping element all together. Rather than using the clumsy mouse to highlight and delete the tags, let’s type three characters and be done with it: dst, or delete surrounding tag.

Hello World

One More Example

Next, let’s imagine that we have a word that needs to be wrapped in an anchor tag:

Link

Well that’s an easy one: viws<a href="http://url.com">. Switch to visual mode, select the inner word, and surround it with an anchor tag, which links to url.com.

<a href="http://url.com">Link</a>   

Be sure to review the video for more examples. I guarantee that you’ll find this plugin to be one of the most useful extensions available for Vim.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Julien

    Jeffrey, this vim session is really amazing! Thank you so much and give us more plugins like this to discover!

  • http://twitter.com/jontyy John

    Really enjoying these Vim plugin videos.

    I have watched the Venturing Into Vim series and it has become my default editor

    Thanks!

  • Johan

    I’d like to see how to wrap an entire line of code with comments. I can’t do it by using Surround plugin.

    Nice video anyways!

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Look for the TComment plugin.

  • http://dennismars.com/ Chi

    You used “git clone https://blahblahblah” to get the file from github. I assume that’s a bash script that everybody knows about except me!

    Where do I get it?

    • SolidSquid

      You’ll need to install git, should be able to use apt-get (or your distro’s equivilent) to install on Linux, or find an installer for Mac or Windows. It’s a version control application, so “git clone https://blahblahblah” will create a clone of the repository at https://blahblahblah in your current directory. In this case it should make a clone of the vim plugin. (might not be current directory though, I’m more familiar with svn than git)

    • SolidSquid

      Just re-watched the start of the video. The “git clone https://blahblahblah” command is what creates the vim-surround directory

      Also, you can get git at http://git-scm.com/download

    • http://www.choiz.fr ChoiZ

      You talk about git?

      You can get it here: http://git-scm.com/

  • http://concordia.duvieusart.net Nicolas Duvieusart Dery

    Was wondering, I think this is the plugin that make this but not sure..

    Anyway, when I am in a PHP document for example, and type ‘(‘ (left parenthesis), it automatically add the closing one. Just like in TextMate. This is cool but is there a way, without touching the arrows or returning to command mode, that after inserting something between the parenthesis, the cursor “exit” the parenthesis ? I found that exiting insert mode every time I have either parenthesis, quotes… is a waste of time..

    Thank you!

  • warjan

    If you want to use . with surround (. stands for repeat last change) plugin install repeat plugin also from Tim Pope. You can find it https://github.com/tpope/vim-repeat

  • Sebastian

    First of all, thank you for introducing me to Vim. The amount of new functions and hotkeys to learn is overwhelming at first but I ‘m sure that it will be worth it.

    Just in case someone else runs into the same problem with Vim for Windows; I had the problem with surround adding an extra whitespace to the end of the selection before wrapping it with the tag. Turns out someone already made a ticket about it and the solution was to remove the following two rows from the vimrc file;

    source $VIMRUNTIME/mswin.vim
    behave mswin

    tpope: “Avoiding those two lines is a good idea; behave mswin basically means ‘make vim imitate a few random behaviors of notepad.’ In this case, it actually changes the behavior of viw by changing the ‘selection’ option, which means there’s nothing I can do about it. Try it with and without ‘behavior mswin’ and you’ll see what I mean. The equivalent normal mode command ysiw’ works the same with and without the behavior line, because in that case I can work around it by temporarily changing the ‘selection’ option back.”

  • http://expectedpayoff.com/ Byron Gibson

    In case anyone is still reading this, any idea why the last example *viws* wouldn’t work? *viw* works for me but *s* then deletes the word.

  • Matt Simmons

    Apparently, ‘s’ in visual mode is “change”, and this plugin doesn’t overwrite that particular mapping, so every time I want to surround something but not indent it, vim deletes it. Handy!

    Anyone have know of a way I can change that behavior?

  • http://twitter.com/bentanweihao Benjamin Tan Wei Hao

    ysiw instead of viws should do the trick