Need script / function / command to surround text with other text

I am new to Vim scripting. I need to write a script that surrounds part of the text with different text. For instance:

\surroundingtext{surrounded text}

(yes, this is for LaTeX). I want to either select "surrounded text", or issue a command, or "surround text" with the result of a regular expression command.

I think the question is how to put this in a function?

Thanks Jeremy

+5
source share
2 answers

That's what I'm doing:

vnoremap <buffer> <leader>sur "zc\surroundingtext{<C-R>z}<Esc>

This creates a display in visual display mode, where you can visually select ( v) the text you want to surround, enter \sur(provided that by default mapleader \), and the text will be surrounded by the text you specify.

  • "z 'z'
  • c vim , 'z'
  • \surroundingtest -
  • <C-R>z Vim 'z'
  • } -
  • <Esc>

:

nnoremap <buffer> <leader>sur i\surroundingtext{}<Esc>i
inoremap <buffer> <leader>sur \surroundingtext{}<Esc>i

~/.vimrc , filetype.

~/.vim/after/ftplugin/tex.vim, , filetype tex. , .

, tex, :

filetype plugin on
+3

. vim? .

, . , v, , . , :

qa        " record a macro in buffer a
x         " cut the selected text
i         " enter insert mode
prefix    " type the desired prefix
<esc>     " exit insert mode
p         " paste the cut text
a         " enter insert mode after the pasted text
postfix   " type the desired postfix
<esc>     " exit insert mode
q         " stop recording

, , , @a.

, , , . , , vim script, .

+1

All Articles