surround.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. *surround.txt* Plugin for deleting, changing, and adding "surroundings"
  2. Author: Tim Pope <vimNOSPAM@tpope.info> *surround-author*
  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. Details about the second argument can be found
  32. below in |surround-replacements|. Once again, examples are in order.
  33. Old text Command New text ~
  34. "Hello *world!" cs"' 'Hello world!'
  35. "Hello *world!" cs"<q> <q>Hello world!</q>
  36. (123+4*56)/2 cs)] [123+456]/2
  37. (123+4*56)/2 cs)[ [ 123+456 ]/2
  38. <div>Yo!*</div> cst<p> <p>Yo!</p>
  39. *ys* takes a valid Vim motion or text object as the first object, and wraps
  40. it using the second argument as with |cs|. (Unfortunately there's no good
  41. mnemonic for "ys".)
  42. Old text Command New text ~
  43. Hello w*orld! ysiw) Hello (world)!
  44. As a special case, *yss* operates on the current line, ignoring leading
  45. whitespace.
  46. Old text Command New text ~
  47. Hello w*orld! yssB {Hello world!}
  48. There is also *yS* and *ySS* which indent the surrounded text and place it
  49. on a line of its own.
  50. In visual mode, a simple "s" with an argument wraps the selection. This is
  51. referred to as the *vS* mapping, although ordinarily there will be
  52. additional keystrokes between the v and s. In linewise visual mode, the
  53. surroundings are placed on separate lines and indented. In blockwise visual
  54. mode, each line is surrounded.
  55. A "gS" in visual mode, known as *vgS* , behaves similarly. In linewise visual
  56. mode, the automatic indenting is surpressed. In blockwise visual mode, this
  57. enables surrounding past the end of the like with 'virtualedit' set (there
  58. seems to be no way in Vim Script to differentiate between a jagged end of line
  59. selection and a virtual block selected past the end of the line, so two maps
  60. were needed).
  61. Additionally, there is a legacy "s" or *vs* mapping which is basically the
  62. same as |vS|. Due to popular demand of wanting to use "s" as Vim does to mean
  63. replacing the selection (also available as "c"), this mapping is going away.
  64. If you were one of these people and would like to disable "s" with the current
  65. release, indicate this to surround.vim by assigning the "s" mapping to
  66. something else.
  67. >
  68. xmap <Leader>s <Plug>Vsurround
  69. <
  70. *i_CTRL-G_s* *i_CTRL-G_S*
  71. Finally, there is an experimental insert mode mapping on <C-G>s and <C-S>.
  72. Beware that the latter won't work on terminals with flow control (if you
  73. accidentally freeze your terminal, use <C-Q> to unfreeze it). The mapping
  74. inserts the specified surroundings and puts the cursor between them. If,
  75. immediately after the mapping and before the replacement, a second <C-S> or
  76. carriage return is pressed, the prefix, cursor, and suffix will be placed on
  77. three separate lines. <C-G>S (not <C-G>s) also exhibits this behavior.
  78. TARGETS *surround-targets*
  79. The |ds| and |cs| commands both take a target as their first argument. The
  80. possible targets are based closely on the |text-objects| provided by Vim.
  81. In order for a target to work, the corresponding text object must be
  82. supported in the version of Vim used (Vim 7 adds several text objects, and
  83. thus is highly recommended). All targets are currently just one character.
  84. Eight punctuation marks, (, ), {, }, [, ], <, and >, represent themselves
  85. and their counterparts. If the opening mark is used, contained whitespace is
  86. also trimmed. The targets b, B, r, and a are aliases for ), }, ], and >
  87. (the first two mirror Vim; the second two are completely arbitrary and
  88. subject to change).
  89. Three quote marks, ', ", `, represent themselves, in pairs. They are only
  90. searched for on the current line.
  91. A t is a pair of HTML or XML tags. See |tag-blocks| for details. Remember
  92. that you can specify a numerical argument if you want to get to a tag other
  93. than the innermost one.
  94. The letters w, W, and s correspond to a |word|, a |WORD|, and a |sentence|,
  95. respectively. These are special in that they have nothing to delete, and
  96. used with |ds| they are a no-op. With |cs|, one could consider them a
  97. slight shortcut for ysi (cswb == ysiwb, more or less).
  98. A p represents a |paragraph|. This behaves similarly to w, W, and s above;
  99. however, newlines are sometimes added and/or removed.
  100. REPLACEMENTS *surround-replacements*
  101. A replacement argument is a single character, and is required by |cs|, |ys|,
  102. and |vs|. Undefined replacement characters (with the exception of alphabetic
  103. characters) default to placing themselves at the beginning and end of the
  104. destination, which can be useful for characters like / and |.
  105. If either ), }, ], or > is used, the text is wrapped in the appropriate pair
  106. of characters. Similar behavior can be found with (, {, and [ (but not <),
  107. which append an additional space to the inside. Like with the targets above,
  108. b, B, r, and a are aliases for ), }, ], and >. To fulfill the common need for
  109. code blocks in C-style languages, <C-}> (which is really <C-]>) adds braces on
  110. lines separate from the content.
  111. If t or < is used, Vim prompts for an HTML/XML tag to insert. You may specify
  112. attributes here and they will be stripped from the closing tag. End your
  113. input by pressing <CR> or >. If <C-T> is used, the tags will appear on lines
  114. by themselves.
  115. A deprecated replacement of a LaTeX environment is provided on \ and l. The
  116. name of the environment and any arguments will be input from a prompt. This
  117. will be removed once a more fully functional customization system is
  118. implemented. The following shows the resulting environment from
  119. csp\tabular}{lc<CR>
  120. >
  121. \begin{tabular}{lc}
  122. \end{tabular}
  123. <
  124. CUSTOMIZING *surround-customizing*
  125. The following adds a potential replacement on "-" (ASCII 45) in PHP files.
  126. (To determine the ASCII code to use, :echo char2nr("-")). The carriage
  127. return will be replaced by the original text.
  128. >
  129. autocmd FileType php let b:surround_45 = "<?php \r ?>"
  130. <
  131. This can be used in a PHP file as in the following example.
  132. Old text Command New text ~
  133. print "Hello *world!" yss- <?php print "Hello world!" ?>
  134. Additionally, one can use a global variable for globally available
  135. replacements.
  136. >
  137. let g:surround_45 = "<% \r %>"
  138. let g:surround_61 = "<%= \r %>"
  139. <
  140. Advanced, experimental, and subject to change: One can also prompt for
  141. replacement text. The syntax for this is to surround the replacement in pairs
  142. of low numbered control characters. If this sounds confusing, that's because
  143. it is (but it makes the parsing easy). Consider the following example for a
  144. LaTeX environment on the "l" replacement.
  145. >
  146. let g:surround_108 = "\\begin{\1environment: \1}\r\\end{\1\1}"
  147. <
  148. When this replacement is used, the user is prompted with an "environment: "
  149. prompt for input. This input is inserted between each set of \1's.
  150. Additional inputs up to \7 can be used.
  151. Furthermore, one can specify a regular expression substitution to apply.
  152. >
  153. let g:surround_108 = "\\begin{\1environment: \1}\r\\end{\1\r}.*\r\1}"
  154. <
  155. This will remove anything after the first } in the input when the text is
  156. placed within the \end{} slot. The first \r marks where the pattern begins,
  157. and the second where the replacement text begins.
  158. Here's a second example for creating an HTML <div>. The substitution cleverly
  159. prompts for an id, but only adds id="" if it is non-blank. You may have to
  160. read this one a few times slowly before you understand it.
  161. >
  162. let g:surround_{char2nr("d")} = "<div\1id: \r..*\r id=\"&\"\1>\r</div>"
  163. <
  164. Inputting text replacements is a proof of concept at this point. The ugly,
  165. unintuitive interface and the brevity of the documentation reflect this.
  166. Finally, It is possible to always append a string to surroundings in insert
  167. mode (and only insert mode). This is useful with certain plugins and mappings
  168. that allow you to jump to such markings.
  169. >
  170. let g:surround_insert_tail = "<++>"
  171. <
  172. ISSUES *surround-issues*
  173. Vim could potentially get confused when deleting/changing occurs at the very
  174. end of the line. Please report any repeatable instances of this.
  175. Do we need to use |inputsave()|/|inputrestore()| with the tag replacement?
  176. Indenting is handled haphazardly. Need to decide the most appropriate
  177. behavior and implement it. Right now one can do :let b:surround_indent = 1
  178. (or the global equivalent) to enable automatic re-indenting by Vim via |=|;
  179. should this be the default?
  180. vim:tw=78:ts=8:ft=help:norl: