📜 ⬆️ ⬇️

VIM plugins that you should know about, part 1: surround.vim

The topic is a free translation of the article Vim Plugins You Should Know About, Part I: surround.vim by Peter Krumins.

UPD: the second part

What is the surround.vim plugin? Here is what the author says about him, Tim Pop (Tim Pope):
')
Surround.vim works with everything that “surrounds”: brackets, quotes, XML tags, etc. The plugin provides keyboard shortcuts that can easily remove, modify, and add pairs of such surrounding elements.

For example, you can quickly enclose a string in an html tag, or remove a pair of curly braces, add quotes around a word, etc.

Here are a couple of examples of how to remove, change, or add an environment. Symbol | indicates the position of the cursor.

Examples of deleting environments:

The environment can be deleted with the “ds” command. After typing "ds", it is expected to enter a target for deletion. This can be a quotation mark: ', ", or`, brackets :, (,), {,}, [,], <,>, and a special target' t 'that will cause the removal of 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 


See how easy it is? Just three clicks. Compare this with how you would act in the old manner:

 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 


Hemorrhoids, agree? (how else to translate messy, I did not think of - note. Lane.)

Examples of changing environments:

To change them, there is a command "cs". Like "ds", it requires you to enter a target for the substitution, and then something to replace. There are several goals for this team. There is a goal w for a word, W for a word with surrounding characters, s for a sentence, p for a 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!' 


Examples of adding environments:

You can add environments with a “cs” command that is waiting for a target to be entered, or with a “ys” command that takes a standard VIM displacement as an argument. The special command “yss” surrounds the whole line, and “ySS” places the surrounding elements on separate lines and moreover adds an indent.
 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 i " 


Environments can also be added in input mode using the <CTRL-s> combination.

Please be careful with CTRL-s. In many terminals, this stops the output and the session is frozen! If this happens, use CTRL-q to defrost it. To remove the CTRL + s combination from your terminal, add the line “stty stop ''” to the file with the terminal settings (.bashrc, .kshrc, etc).

  (   ): Command New Text ------- ------------ <CTRL-s>" "" <CTRL-s><CTRL-s><html> <html> | </html> 


I implore you to try these combos! You can not become a hockey player, looking at the game, you have to try it yourself!

Here is a complete list of plugin commands:

 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 . Archive contains plugin and documentation.
2. Unpack surround.zip to the ~ / .vim folder (Unix / Linux), or ~ \ vimfiles (Windows).
3. Restart Vim (or connect the script with ": so ~ / .vim / plugin / surround.vim" in Unix or ": so ~ / vimfiles / plugin / surround.vim" in Windows).
4. Generate help tags with ": helptags ~ / .vim / doc" on Unix and ": helptags ~ / vimfiles / doc" in the window vents.

Source: https://habr.com/ru/post/114813/


All Articles