surround.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. *surround.txt* Plugin for deleting, changing, and adding "surroundings"
  2. Author: Tim Pope <http://tpo.pe/>
  3. License: Same terms as Vim itself (see |license|)
  4. This plugin is only available if 'compatible' is not set.
  5. INTRODUCTION *surround*
  6. This plugin is a tool for dealing with pairs of "surroundings." Examples
  7. of surroundings include parentheses, quotes, and HTML tags. They are
  8. closely related to what Vim refers to as |text-objects|. Provided
  9. are mappings to allow for removing, changing, and adding surroundings.
  10. Details follow on the exact semantics, but first, consider the following
  11. examples. An asterisk (*) is used to denote the cursor position.
  12. Old text Command New text ~
  13. "Hello *world!" ds" Hello world!
  14. [123+4*56]/2 cs]) (123+456)/2
  15. "Look ma, I'm *HTML!" cs"<q> <q>Look ma, I'm HTML!</q>
  16. if *x>3 { ysW( if ( x>3 ) {
  17. my $str = *whee!; vllllS' my $str = 'whee!';
  18. While a few features of this plugin will work in older versions of Vim,
  19. Vim 7 is recommended for full functionality.
  20. MAPPINGS *surround-mappings*
  21. Delete surroundings is *ds* . The next character given determines the target
  22. to delete. The exact nature of the target is explained in |surround-targets|
  23. but essentially it is the last character of a |text-object|. This mapping
  24. deletes the difference between the "i"nner object and "a"n object. This is
  25. easiest to understand with some examples:
  26. Old text Command New text ~
  27. "Hello *world!" ds" Hello world!
  28. (123+4*56)/2 ds) 123+456/2
  29. <div>Yo!*</div> dst Yo!
  30. Change surroundings is *cs* . It takes two arguments, a target like with
  31. |ds|, and a replacement. *cS* changes surroundings, placing the surrounded
  32. text on its own line(s) like |yS|. Details about the second argument can be
  33. found below in |surround-replacements|. Once again, examples are in order.
  34. Old text Command New text ~
  35. "Hello *world!" cs"' 'Hello world!'
  36. "Hello *world!" cs"<q> <q>Hello world!</q>
  37. (123+4*56)/2 cs)] [123+456]/2
  38. (123+4*56)/2 cs)[ [ 123+456 ]/2
  39. <div>Yo!*</div> cst<p> <p>Yo!</p>
  40. *ys* takes a valid Vim motion or text object as the first object, and wraps
  41. it using the second argument as with |cs|. (It's a stretch, but a good
  42. mnemonic for "ys" is "you surround".)
  43. Old text Command New text ~
  44. Hello w*orld! ysiw) Hello (world)!
  45. As a special case, *yss* operates on the current line, ignoring leading
  46. whitespace.
  47. Old text Command New text ~
  48. Hello w*orld! yssB {Hello world!}
  49. There is also *yS* and *ySS* which indent the surrounded text and place it
  50. on a line of its own.
  51. In visual mode, a simple "S" with an argument wraps the selection. This is
  52. referred to as the *vS* mapping, although ordinarily there will be
  53. additional keystrokes between the v and S. In linewise visual mode, the
  54. surroundings are placed on separate lines and indented. In blockwise visual
  55. mode, each line is surrounded.
  56. A "gS" in visual mode, known as *vgS* , behaves similarly. In linewise visual
  57. mode, the automatic indenting is suppressed. In blockwise visual mode, this
  58. enables surrounding past the end of the line with 'virtualedit' set (there
  59. seems to be no way in Vim Script to differentiate between a jagged end of line
  60. selection and a virtual block selected past the end of the line, so two maps
  61. were needed).
  62. *i_CTRL-G_s* *i_CTRL-G_S*
  63. Finally, there is an experimental insert mode mapping on <C-G>s and <C-S>.
  64. Beware that the latter won't work on terminals with flow control (if you
  65. accidentally freeze your terminal, use <C-Q> to unfreeze it). The mapping
  66. inserts the specified surroundings and puts the cursor between them. If,
  67. immediately after the mapping and before the replacement, a second <C-S> or
  68. carriage return is pressed, the prefix, cursor, and suffix will be placed on
  69. three separate lines. <C-G>S (not <C-G>s) also exhibits this behavior.
  70. TARGETS *surround-targets*
  71. The |ds| and |cs| commands both take a target as their first argument. The
  72. possible targets are based closely on the |text-objects| provided by Vim.
  73. All targets are currently just one character.
  74. Eight punctuation marks, (, ), {, }, [, ], <, and >, represent themselves
  75. and their counterparts. If the opening mark is used, contained whitespace is
  76. also trimmed. The targets b, B, r, and a are aliases for ), }, ], and >
  77. (the first two mirror Vim; the second two are completely arbitrary and
  78. subject to change).
  79. Three quote marks, ', ", `, represent themselves, in pairs. They are only
  80. searched for on the current line.
  81. A t is a pair of HTML or XML tags. See |tag-blocks| for details. Remember
  82. that you can specify a numerical argument if you want to get to a tag other
  83. than the innermost one.
  84. The letters w, W, and s correspond to a |word|, a |WORD|, and a |sentence|,
  85. respectively. These are special in that they have nothing to delete, and
  86. used with |ds| they are a no-op. With |cs|, one could consider them a
  87. slight shortcut for ysi (cswb == ysiwb, more or less).
  88. A p represents a |paragraph|. This behaves similarly to w, W, and s above;
  89. however, newlines are sometimes added and/or removed.
  90. REPLACEMENTS *surround-replacements*
  91. A replacement argument is a single character, and is required by |cs|, |ys|,
  92. and |vS|. Undefined replacement characters (with the exception of alphabetic
  93. characters) default to placing themselves at the beginning and end of the
  94. destination, which can be useful for characters like / and |.
  95. If either ), }, ], or > is used, the text is wrapped in the appropriate pair
  96. of characters. Similar behavior can be found with (, {, and [ (but not <),
  97. which append an additional space to the inside. Like with the targets above,
  98. b, B, r, and a are aliases for ), }, ], and >. To fulfill the common need for
  99. code blocks in C-style languages, <C-}> (which is really <C-]>) adds braces on
  100. lines separate from the content.
  101. If t or < is used, Vim prompts for an HTML/XML tag to insert. You may specify
  102. attributes here and they will be stripped from the closing tag. If replacing a
  103. tag, its attributes are kept in the new tag. End your input with > to discard
  104. the those attributes. If <C-T> is used, the tags will appear on lines by
  105. themselves.
  106. If f, F, or <C-F> is used, Vim prompts for a function name to insert. The target
  107. text will be wrapped in a function call. If f is used, the text is wrapped with
  108. () parentheses; F adds additional spaces inside the parentheses. <C-F> inserts the
  109. function name inside the parentheses.
  110. Old text Command New text ~
  111. "hello" ysWfprint<cr> print("hello")
  112. "hello" ysWFprint<cr> print( "hello" )
  113. "hello" ysW<C-f>print<cr> (print "hello")
  114. If s is used, a leading but not trailing space is added. This is useful for
  115. removing parentheses from a function call with csbs.
  116. CUSTOMIZING *surround-customizing*
  117. The following adds a potential replacement on "-" (ASCII 45) in PHP files.
  118. (To determine the ASCII code to use, :echo char2nr("-")). The carriage
  119. return will be replaced by the original text.
  120. >
  121. autocmd FileType php let b:surround_45 = "<?php \r ?>"
  122. <
  123. This can be used in a PHP file as in the following example.
  124. Old text Command New text ~
  125. print "Hello *world!" yss- <?php print "Hello world!" ?>
  126. Additionally, one can use a global variable for globally available
  127. replacements.
  128. >
  129. let g:surround_45 = "<% \r %>"
  130. let g:surround_61 = "<%= \r %>"
  131. <
  132. Advanced, experimental, and subject to change: One can also prompt for
  133. replacement text. The syntax for this is to surround the replacement in pairs
  134. of low numbered control characters. If this sounds confusing, that's because
  135. it is (but it makes the parsing easy). Consider the following example for a
  136. LaTeX environment on the "l" replacement.
  137. >
  138. let g:surround_108 = "\\begin{\1environment: \1}\r\\end{\1\1}"
  139. <
  140. When this replacement is used, the user is prompted with an "environment: "
  141. prompt for input. This input is inserted between each set of \1's.
  142. Additional inputs up to \7 can be used.
  143. Furthermore, one can specify a regular expression substitution to apply.
  144. >
  145. let g:surround_108 = "\\begin{\1environment: \1}\r\\end{\1\r}.*\r\1}"
  146. <
  147. This will remove anything after the first } in the input when the text is
  148. placed within the \end{} slot. The first \r marks where the pattern begins,
  149. and the second where the replacement text begins.
  150. Here's a second example for creating an HTML <div>. The substitution cleverly
  151. prompts for an id, but only adds id="" if it is non-blank. You may have to
  152. read this one a few times slowly before you understand it.
  153. >
  154. let g:surround_{char2nr("d")} = "<div\1id: \r..*\r id=\"&\"\1>\r</div>"
  155. <
  156. Inputting text replacements is a proof of concept at this point. The ugly,
  157. unintuitive interface and the brevity of the documentation reflect this.
  158. Finally, It is possible to always append a string to surroundings in insert
  159. mode (and only insert mode). This is useful with certain plugins and mappings
  160. that allow you to jump to such markings.
  161. >
  162. let g:surround_insert_tail = "<++>"
  163. <
  164. vim:tw=78:ts=8:ft=help:norl: