Reputation: 3096
I modified some code I found here to work for LaTeX. I demonstrate the desired output for two cases. Please have a look at
http://vim.wikia.com/wiki/Wrap_a_visual_selection_in_an_HTML_tag
elephant
\test{elephant}
VisualcommandTagWrap
This works for singleline selections. I'd like to make it work for multiline selections. Simply select text to wrap using your visual selector, then press F7.
" Wrap visual selection in a latex command tag.
map <F7> : call VisualcommandTagWrap()
function! VisualcommandTagWrap()
let tag = input("Tag to wrap block: ")
if len(tag) > 0
normal `>
if &selection == 'exclusive' "set selection=exclusive means position before the cursor marks the end of the selection vs. inclusive
exe "normal i\\".tag."}"
else
exe "normal a\}"
endif
normal `<
exe "normal i\\".tag."{"
normal `<
endif
endfunction
elephant
\begin{test}
elephant
\end{test}
VisualenvironmentTagWrap
I do not know how to implement functions that cover multiple lines.
For those unfamiliar with LaTeX. There are two situations to satisfy requirements for LaTeX commands and environments.
Here is some visual mode selected text.
And another line of visual mode selected text.
Result:
\inputstep2{Here is some visual mode selected text.
And another line of visual mode selected text.}
Result:
\begin{inputstep2}
Here is some visual mode selected text.
And another line of visual mode selected text.
\end{inputstep2}
Upvotes: 1
Views: 150
Reputation: 1496
Step 1:
You can store the text you want (like test
) in register a
.
:let @a='test'
(Until you change it, it will remain the same and can be reused. If you want it to be changed, you have to follow the step again.)
Step 2:
Then, select the the text using visual selection (v
, V
or ctrl V
) and then press escape to cancel the selection.
Step 3;
Keep the cursor on anywhere in the screen, (preferably in the line where you want to surround with) and press \s
in normal mode.
Put the following mapping in ~/.vimrc file. Let's create a mapping to wrap around.
For first example: (begin and end type)
:nmap \s '<O\begin{<C-R>a<ESC>}'>o\end{<C-R>a}
For second example: (begin type)
:nmap \s '<O\{<C-R>a<ESC>'>a}
Upvotes: 1