Jonathan Komar
Jonathan Komar

Reputation: 3096

How can I apply LaTeX command and environment wrappers to singleline or multiline Visual Selections in ViM?

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

Dealing with LaTeX Commands

Given:

elephant
  1. visually select "elephant"
  2. press F7
  3. enter "test"

Output:

\test{elephant}

Example Code Code for 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

Dealing with LaTeX Environments

Given:

elephant
  1. visually select "elephant"
  2. press F7
  3. enter "test"

Output:

\begin{test}
elephant
\end{test}

Example Code for VisualenvironmentTagWrap

I do not know how to implement functions that cover multiple lines.


Additional Examples

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.

Command Situation:

  1. type function key defined in .vimrc to call function
  2. vim prompts for user input (e.g. "Please enter name of command:")
  3. vim surrounds selection with command.

Result:

\inputstep2{Here is some visual mode selected text.
And another line of visual mode selected text.}

Environment Situation

  1. type function key defined in .vimrc to call function
  2. vim prompts for user input (e.g. "Please enter name of environment:")
  3. vim surrounds selection with environment.

Result:

\begin{inputstep2}
Here is some visual mode selected text.
And another line of visual mode selected text.
\end{inputstep2}

Upvotes: 1

Views: 150

Answers (1)

SibiCoder
SibiCoder

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

Related Questions