Vim: enclose a word in a tag

I would like to enclose a word in Vim with closing tags, for example, from word to 'word' or from word to (word) .

+8
vim
source share
4 answers

Use Tim Pope vim-surround , you can use: ysw' to enclose the word with "` ", ysw( to surround the word" If you want to surround the word with an actual tag, such as an HTML <code> , you can use yswt , and then enter your tag at the csw you want to modify existing surround elements, use csw , enter the surround element that you want to replace, and press <CR> (enter) and enter a new surround element.

Note that for the above commands, you must place the cursor at the beginning of the word that you want to surround.

From the official page:

Press cs"' (this is c, s, double quote, single quote) inside

"Hello World!"

to change it to

"Hello World!"

Now press cs'<q> to change it to

Hello World!

To go full circle, click cat" to get

"Hello World!"

To completely remove the delimiters, press ds" .

Hello World!

Now, using the cursor on β€œHi,” press ysiw] ( iw is a text object).

[Hello] world!

Let it do the brackets and add some space (use "}" instead of "{" for space): cs]{

{Hello} world!

Now wrap the entire line in parentheses with yssb or yes) .

({Hello} world!)

Return the source: ds{ds)

Hello World!

ysiw<em> hi: ysiw<em>

Hello World!

Finally, try the visual mode. Press capital city V (for visual line mode) and then S<p class="important"> .

Hello World!

For more detailed usage information, install vim-surround , and then use :help surround to call up documents.

+19
source share

Take a look at Tim Papa's surround.vim .

+4
source share

<cr> very convenient for them, allowing you to paste the contents of the register, clipboard, words under the cursor ( <cr><ca> ), etc. For example:

 nmap <space>' ciw'<cr>"'<esc> vmap <space>' c'<cr>"'<esc> nmap <space>( ciw(<cr>")<esc> vmap <space>( c(<cr>")<esc> 

What space+' displays to wrap the current word or visual highlight in single quotes or space+( to wrap the current word or visual highlight in double quotes.

The content of the display means "change the inner word", the selected text, insert a quote, insert a word, insert a quote, insert insert mode ".

+4
source share

I use this snippet to write a macro to the letter M for the first time: qmcw '<cr> "<esc> q Then redo it with @m

Explanation: 10 strokes are required first:

  • qm starts writing a macro to the letter m
  • cw change only the word and put you in insert mode
  • 'record the first quote
  • <cr> "paste the word that we deleted in step 2, remaining in insert mode
  • '- another surrounding quote
  • <& ESC GT; go back to edit word
  • q completes the macro.

Recall this with @m: now we only use two hits to surround ...

+2
source share

All Articles