9

I am using vim-latexsuite to edit a latex file. The text is originally from google doc and there are many math symbols not written in math mode.

I have to add $ before and after each symbol. But that is painful. (Search/Replace does not work because some equation patterns are complicated.)

Is there a way that allows me to visually select the symbols or equestions using Ctrl-V in visual mode, then after pressing the key, the $ can be automatically added before and after the visual selection?

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
user875367
  • 313
  • 3
  • 9

3 Answers3

9

I don't think there is any standard command for this, but you can use the surround.vim plugin to do this:

http://www.catonmat.net/blog/vim-plugins-surround-vim/

The command is csW$ to surround the current text with $

Jeff
  • 12,147
  • 10
  • 51
  • 87
  • 1
    I love surround.vim. Note if you already have a selection you can just key `s$` to surround with `$`. And since `c`, `s`, `W`, and `$` are all keyed with the left hand, I'd use `ysiw$` to surround the word the cursor is on with `$` (it has a nice back-and-forth drumming motion). Or combine either with a macro and you're set! – Michelle Tilley Dec 02 '12 at 06:16
  • 1
    For me, it's not ``csW$`` but ``cSW$`` (note the capital S) to surround text with the dollar sign $. When erroneously using lower case ``s``, text gets deleted. See also: http://stackoverflow.com/questions/13497990/vim-surround-not-working-for-me-s-key-deletes-instead-of-surrounds – Peter Lustig Apr 22 '14 at 21:12
3

There actually is a standard command for this built into vim-latexsuite. See the vim-latex docs for macros here.

In addition the visual mode macros are provided:

    `(  encloses selection in \left( and \right)
    `[  encloses selection in \left[ and \right]
    `{  encloses selection in \left\{ and \right\}
    `$  encloses selection in $$ or \[ \] depending on characterwise or
                                          linewise selection
cowlicks
  • 1,037
  • 1
  • 12
  • 20
2

You can record a macro to do this.

With a visual selection, do this:

qq – record macro in register q

c – change the content of the visual selection

$$Esc – insert $$

P – paste the original text between the $s (note it's a capital P)

q – stop recording the macro

From then on, you can make your visual selection and just run @q.

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93