I am now on Twitter! Meet me on Twitter here (my nick is pkrumins.)
Or on Google Buzz and Facebook.

Hi all. I am starting a new article series called “Vim Plugins You Should Know About“. This series of articles is going to be about Vim plugins that you should know about and perhaps even be using. The first article in this series will be about one of my favorite plugins called “surround.vim“.
If you are intrigued by this article series, I suggest that you subscribe to my posts!
Don’t worry, I’ll finish all the previous article series that I have started (creating and marketing a video downloading tool, famous awk one-liners explained, sed one-liners explained and mit algorithms). I just wanted to utilize my excitement and get this article published ASAP.
Okay, so what is surround.vim plugin? Here is what Tim Pope, the author of this plugin, says:
Surround.vim is all about “surroundings”: parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.
That’s what the plugin does. It allows you to easily delete, change and add various “surroundings” in pairs. For example, you may quickly wrap a line in an html tag, or you may delete a pair of { }, or you may add quotes around words, etc.
Let’s take a look at a few examples for each surrounding command: delete surrounding, change surrounding, and add surrounding. In the examples | indicates the position of the cursor.
Examples of deleting surroundings:
Surroundings can be deleted with the “ds” command. After you type “ds“, the command expects the surrounding target you want to delete. It may be any of three quote marks, ', ", and `, the eight punctuation marks, (, ), {, }, [, ], <, > , and a special ‘t’ target for deleting the innermost HTML tag.
Text Command New Text --------------- ------- ----------- 'Hello W|orld' ds' Hello World (12|3+4*56)/2 ds( 123+4*56/2 <div>fo|o</div> dst foo (| is the position of cursor in these examples)
See how easy it was to delete surroundings? Just 3 keystrokes. Compare it with doing it old fashioned way:
Text Command New Text --------------- ------- ----------- 'Hello W|orld' F'x,x Hello World (12|3+4*56)/2 Bxf)x 123+4*56/2 <div>fo|o</div> Bdf>wdf> foo (| is the position of cursor in these examples)
That was really messy, wasn’t it? I hope you start to see now how handy surround.vim is!
Examples of changing surroundings:
Surroundings can be changed with the “cs” command. The “cs” command, just like “ds” command takes a surrounding target, and it also takes the surrounding replacement. There are a few more surrounding targets for this command. They are w for word, W for word + skip punctuation, s for sentence, and p for paragraph.
Text Command New Text --------------- ------- ----------- "Hello |world!" cs"' 'Hello world!' "Hello |world!" cs"<q> <q>Hello world!</q> (123+4|56)/2 cs)] [123+456]/2 (123+4|56)/2 cs)[ [ 123+456 ]/2 <div>foo|</div> cst<p> <p>foo</p> fo|o! csw' 'foo'! fo|o! csW' 'foo!' (| is the position of cursor in these examples)
Examples of adding surroundings:
Surroundings can be added with the same “cs” command, which takes a surrounding target, or with the “ys” command that takes a valid vim motion. There is a special “yss” command that applies a surrounding to the whole line, and “ySS” that applies the surrounding to the whole line, places the text on a new line and indents it.
Text Command New Text
--------------- ------- -----------
Hello w|orld! ysiw) Hello (world)!
Hello w|orld! csw) Hello (world)!
fo|o ysiwt<html> <html>foo</html>
foo quu|x baz yss" "foo quux baz"
foo quu|x baz ySS" "
foo quux baz
"
(| is the position of cursor in these examples)
You can also add surroundings while in insert mode. That is supported via <CTRL-s> mapping.
Please be very careful with CTRL-s. On many terminals it stops the output and your session will appear to be frozen! If that happens, use CTRL-q to unfreeze it. You may want to remove CTRL+s mapping from your terminal altogether. Add “stty stop ''” to your shell startup file (.bashrc for bash, .kshrc for ksh, etc).
For example (while in insert mode):
Command New Text
------- ------------
<CTRL-s>" ""
<CTRL-s><CTRL-s><html> <html>
|
</html>
(| is the position of cursor in these examples)
I urge you to try these mappings out. You won’t become a hockey player by just looking at the game, you have to play it yourself!
For a complete reference, here are all the commands/mappings surround.vim provides:
Normal mode ----------- ds - delete a surrounding cs - change a surrounding ys - add a surrounding yS - add a surrounding and place the surrounded text on a new line + indent it yss - add a surrounding to the whole line ySs - add a surrounding to the whole line, place it on a new line + indent it ySS - same as ySs Visual mode ----------- s - in visual mode, add a surrounding S - in visual mode, add a surrounding but place text on new line + indent it Insert mode ----------- <CTRL-s> - in insert mode, add a surrounding <CTRL-s><CTRL-s> - in insert mode, add a new line + surrounding + indent <CTRL-g>s - same as <CTRL-s> <CTRL-g>S - same as <CTRL-s><CTRL-s>
How to install surround.vim?
- 1. Download surround.zip. It will actually be a zip file containing the surround.vim script and documentation.
- 2. Extract surround.zip to ~/.vim (on Unix/Linux), or ~\vimfiles (Windows).
- 3. Restart Vim (or just source surround.vim script with “:so ~/.vim/plugin/surround.vim” on Unix or “:so ~/vimfiles/plugin/surround.vim” on Windows).
- 4. Regenerate helptags with “:helptags ~/.vim/doc” on Unix or “:helptags ~/vimfiles/doc” on Windoze.
Type “:help surround” to read help.
Comments appreciated!
What plugins do you use? Your comments will help me and my blog readers to find more plugins which we might have not heard about. You may also share any tips and aha-moments that you have had while using vim. I might add my own and turn them into a valuable article. Thanks!
Did you like this post? Subscribe here:
If you really enjoyed the post, I'd appreciate a gift from my geeky Amazon book wishlist. Books would make me more educated and I could write even better posts. Thanks! :)

(14 votes, average: 4.5 out of 5)
|
|
|


December 8th, 2008 at 2:07 pm
What does this plugin do that text objects doesn’t provide already?
December 8th, 2008 at 2:23 pm
JB, this plugin uses text objects to apply surroundings.
December 8th, 2008 at 3:28 pm
Comment copied from HN:
I tried this plugin a few days ago, but it seemed to break the s (substitute) command, so I removed it. Does someone know how to make it work?
The problem is the following: Write a line of text, select part of it using v, then press s to remove the selection and enter insert mode start replacing it. This doesn’t work anymore when surround.vim is loaded. Too bad, it sure looks great…
December 8th, 2008 at 3:47 pm
thamer, replied to you on HN. Copied here:
Try ‘R’ instead of ’s’ that you were used to.
OR
Edit surround.vim and comment out the ’s’ mappings in visual mode.
December 8th, 2008 at 3:54 pm
Instead of ’s’ for substitute, try ‘c’ for change.
December 8th, 2008 at 4:22 pm
Peteris, I seem to be hitting this bug: http://cygwin.com/ml/cygwin/2003-09/msg01125.html when I hit Ctrl+S in my VIM. Then I had to hit Ctrl+Q to unfreeze.
I’m on a Solaris machine running dtterm and ksh. Any idea on how to get around this problem?
December 8th, 2008 at 4:37 pm
Srikanth, that is not a bug at all. It’s your terminal controlling the flow. With Ctrl+S you stop the terminal, with Ctrl+Q you restart the output.
To avoid this “bug”, try redefining the Ctrl+S at terminal level:
And put it in your .profile or .kshrc file.
December 8th, 2008 at 4:42 pm
Surround is one of the plugins featured as one of the 15 best vim plugins here..
15 Best Vim Plugins
Check out the other 14 to have a highly customized efficient vim environment.
December 8th, 2008 at 5:06 pm
surround.vim looks very useful. I’ve wanted something similar in the past but I was too lazy to search for it. Thanks for the tip!
Vim by itself is usually enough for me, so I haven’t tried many plugins. The only plugin that I use regularly is vim-latex; it’s amazing how fast one can type LaTeX documents with it (more efficient than using a wysiwyg, me thinks).
December 8th, 2008 at 6:20 pm
Thanks for blogging about this plugin, I absolutely dig surround.vim!
FuzzyFinder.vim and taglist.vim are two other staples I would definitely add to the list.
Tim
December 8th, 2008 at 6:23 pm
Apart from surround.vim, my favourite editing script is yankring.vim. Works really well, with lots of delightful features.
I also use bufexplorer.vim winfileexplorer.vim taglist.vim and winmanager.vim to get all that ide functionality.
Another nifty plugin is the ZoomWin.vim plugin, which allows you to maximise a vim window, and then restore your previous window configuration. I have tons of plugins lying around, many of which I can’t remember what they do.
Oh yeah, the themes.vim with all the colourschemes is necessary as well.
December 8th, 2008 at 7:42 pm
Wow nice plugin! Already using it :)
December 8th, 2008 at 8:19 pm
I can do that surround removal shit in under a second with clever use of cmd+cursors/option+cursors shift and delete when I’m in textmate.
and I can do it instantly without having to remember that obscure command to perform something this abstract.
You think your saving time because your calling automation, but really the amount of time you’ll be wasting in remembering that wont be saved at all.
If we both did it at exactly the same time I would be finished before you even started typing.
Shortcuts are mostly a myth, you feel more productive when your actually slower.
December 8th, 2008 at 11:51 pm
dogmachine:
You mock vim users for using shortcuts to delete text, whilst giving the example of . . . yourself using shortcuts to delete text. *laughs*
How is remembering a “clever use of cmd+cursors/option+cursors shift and delete” any easier than remembering ‘ds’?
December 9th, 2008 at 12:07 am
Unfortunately, commands added by this plugin are not repeated by the ‘.’ command — they are treated as a couple of separate commands. For example, if you have two words and you move the cursor to one and do ds”, then you move the cursor to the other and hit ‘.’ and it won’t preform ds”, it will just delete the word under the cursor. Kind of a drag — anybody know how to change this?
December 9th, 2008 at 1:06 am
As usual, people hide behind pseudonyms to spew their craziness on the net. I guess you know you look a bit silly to any prospective employer who may read your response.
December 9th, 2008 at 1:22 am
When im adding surroundings, i usually select the target text in visual mode then hit s) or s” etc. This requires less thinking than using text objects which often makes it faster.
Legend: if you use repeat.vim (by the same author) then some of the commands are repeatable with “.” but not all :(
December 9th, 2008 at 4:35 am
Legend: install repeat.vim
http://www.vim.org/scripts/script.php?script_id=2136
December 9th, 2008 at 11:41 am
Thanks Peteris, of course it’s not a bug; it’s a feature. What was I thinking! :)
Nice plugin. Autoclose is also very useful.
December 9th, 2008 at 2:07 pm
A few comments:
- Use <CTRL-g>S if you want to keep <CTRL-s> for your terminal.
- Debian makes it easy to install vim addons: apt-get install vim-{scripts,addon-manager} && vim-addons install surround
Cheers.
December 9th, 2008 at 5:50 pm
You can also have a look at the project plugin which let you load all files of a project, grep some pattern in the files, run a script on all files of the project, etc.
You can find a small introduction here.
December 10th, 2008 at 3:09 am
come out, we’ve got the place surrounded!
December 13th, 2008 at 11:32 am
I have been using VIM for a long time, but I have not heard about this plug-in. Thank you!
December 15th, 2008 at 11:21 pm
[…] fixes an important functionality problem in the surround.vim plugin that I wrote about last week. The problem with surround.vim lies in the repeat command […]
December 17th, 2008 at 1:14 pm
[…] http://www.catonmat.net/blog/vim-plugins-surround-vim/ : surrond.vim est un plugin Vim très utile. […]
December 24th, 2008 at 5:59 pm
[…] I am now embarking on the journey from beginning user to vim power user. Thanks to posts like this one from a series on Vim plugins you should know about, I can customize my programming environment to be the way *I* want. No matter what editor you use, […]
February 5th, 2009 at 3:56 pm
[…] If you are intrigued by this topic, I suggest that you subscribe to my posts! For the introduction and first post in this article series, follow this link - Vim Plugins You Should Know About, Part I: surround.vim. […]
February 5th, 2009 at 7:25 pm
I noticed that in visual mode s is redundant because c does the same thing. So having s rebound in visual mode is not a problem for me.
It’s a pretty sweet plugin IMO.
April 9th, 2009 at 9:59 pm
[…] Vim Plugins You Should Know About, Part I: surround.vim - good coders code, great reuse - […]
August 7th, 2009 at 10:00 am
[…] Vim Plugins You Should Know About, Part I: surround.vim […]
August 14th, 2009 at 7:36 pm
[…] Vim Plugins You Should Know About, Part I: surround.vim: http://www.catonmat.net/blog/vim-plugins-surround-vim/ […]
August 20th, 2009 at 1:51 am
Acutaly, I don’t find this very helpful. There are still to many commands, and even with repeat.vim, the repeat action doesn’t always work.
I’ll try it out a little more, but not going to hold my breadth.
August 25th, 2009 at 2:55 am
[…] bufexplorer插件是我使用vim后接触的第一个插件,看到别人说好就想装一个试用一下。这里是下载地址安装过程简单,把下载的zip包在$HOME/.vim/目录下解压缩,得到两个文件:plugin/bufexplorer.vimdoc/bufexplorer.txtbufexplorer.vim是vim脚本文件,bufexplorer.txt是帮助文件。很多人都说重新运行vim就能使用插件了,可是我试了不行,说是和runtimepath有关,在vim中运行:set rtp的结果显示:runtimepath=~/.vim,/usr/local/share/vim/vimfiles,/usr/local/share/vim/vim72,/usr/local/share/vim/vimfiles/after,~/.vim/after,里面有~/.vim,不知道为啥不行,也没查到怎么改,说是在编译的时候指定的,也懒得重新编译安装vim。本来想放弃了,这时看到一篇不错的文章:Vim Plugins You Should Know About, Part I: surround.vim里面说运行:so ~/.vim/plugin/bufexplorer.vim来使插件起作用,一试之下果然管用。再运行:helptags ~/.vim/doc重新生成帮助。下面把学习使用bufexplorer时觉得最简单常用的功能总结一下。 […]
November 5th, 2009 at 10:46 pm
[…] For the introduction of this article series see part one - surround.vim. […]
November 6th, 2009 at 2:30 am
If you’re looking for Vim plugins that people should know about… You might want to check out Txtfmt (The Vim Highlighter), which provides a sort of “rich text” highlighting capability in Vim: fg/bg colors and all combinations of bold, underline, italic, etc…
Vim download page:
http://www.vim.org/scripts/script.php?script_id=2208
Screenshots:
http://www.freewebs.com/txtfmt
Disclaimer: Yes, I am the author, but my experience has convinced me that there are still a number of people looking for this capability in Vim, who aren’t aware that this plugin exists.
January 10th, 2010 at 1:52 am
[…] Vim Plugins You Should Know About, Part I: surround.vim […]
January 18th, 2010 at 2:57 pm
[…] Part I: surround.vim - find and edit surrounding parens, quotes, html tags, etc. […]
February 28th, 2010 at 10:54 pm
I noticed that in visual mode s is redundant because c does the same thing. So having s rebound in visual mode is not a problem for me.
so i think it is sweet plugin
March 2nd, 2010 at 4:08 pm
How to keep s unmapped in Visual mode:
This is directly taken from surround.vim’s help file, thus the author pretty much is aware about the issue.
March 16th, 2010 at 12:53 am
Social comments and analytics for this post
This post was mentioned on Twitter by delicious50: Surround http://bit.ly/2sUs7F vim