@saginaw answered a question I asked a long time ago, in this thread.

Here's a code snippit of his suggestions that illustrates my problem:

function! s:PadMacro(type) abort
    if a:type ==# 'char'
        normal! `[v`]d
    elseif a:type ==# 'line'
        normal! '[V']d
    elseif a:type ==# 'v'
        normal! `<v`>d
    let string = '\Pad{' . @" . '}'
    silent execute "normal! i\<c-r>=string\r"
nnoremap <silent> ;P :set operatorfunc=<sid>PadMacro<cr>g@
vnoremap <silent> ;P :<c-u>call <sid>PadMacro(visualmode())<cr>


Here are @saginaw's instructions

    If you put the previous code in your vimrc, it should do what you want.

    To use it, hit ;P{motion} and the PadMacro() function should enclose the characters covered by {motion} with \Pad{ and }.

The problem I'm having is with the ;P{motion} command: If

(*) on the last line of the snippet I highlight the three aaa's by hitting v and the spacebar, then hit ;P

I get the following error

Error detected while processing function <SNR>7_PaddingFunction:
line   10:
E121: Undefined variable: g:enclosure
E15: Invalid expression: '\' . g:enclosure . '{' . @" . '}'
Press ENTER or type command to continu

HOWEVER, if I first modify the single a in the line above, by putting the cursor over it and then typing ;P (no 'v' then spacebar required), and AFTER DOING SO, repeat the command sequence on the line that begins with (*), the ;P command works as expected.

My ignorant guess is that something needs to be initialized in order for ;P, together with motion, to work properly

Could somebody please advise how to get the (*) command sequence to work the first time? Thanks for any advice.

At @Christian_Brabandt's request I'm posting the output of :scriptnames

1: ~/LOCALSVN/dotFiles/trunk/vimrc
  2: /usr/share/vim/vim74/syntax/syntax.vim
  3: /usr/share/vim/vim74/syntax/synload.vim
  4: /usr/share/vim/vim74/syntax/syncolor.vim
  5: /usr/share/vim/vim74/filetype.vim
  6: ~/LOCALSVN/multiSystem/trunk/vimPlugins/unicode.vim/ftdetect/unicode.vim
  7: /usr/share/vim/vim74/syntax/nosyntax.vim
  8: /usr/share/vim/vim74/colors/delek.vim
  9: ~/LOCALSVN/multiSystem/trunk/vimPlugins/unicode.vim/plugin/unicode.vim
 10: /usr/share/vim/vim74/plugin/getscriptPlugin.vim
 11: /usr/share/vim/vim74/plugin/gzip.vim
 12: /usr/share/vim/vim74/plugin/logiPat.vim
 13: /usr/share/vim/vim74/plugin/matchparen.vim
 14: /usr/share/vim/vim74/plugin/netrwPlugin.vim
 15: /usr/share/vim/vim74/plugin/rrhelper.vim
 16: /usr/share/vim/vim74/plugin/spellfile.vim
 17: /usr/share/vim/vim74/plugin/tarPlugin.vim
 18: /usr/share/vim/vim74/plugin/tohtml.vim
 19: /usr/share/vim/vim74/plugin/vimballPlugin.vim
 20: /usr/share/vim/vim74/plugin/zipPlugin.vim
 21: /usr/share/vim/vim74/scripts.vim

My vimrc file is posted here and a text file that generates the problem is here

  • E121: Undefined variable: g:enclosure - this means you don't have a variable defined. However the code you have given in your question, does not even use this variable. I feel like we are missing something Jun 8 '18 at 18:20
  • This code is used by function <SNR>7..., so please show the output number 7 of :scriptnames Jun 8 '18 at 19:13
  • Output #7 is /usr/share/vim/vim74/syntax/nosyntax.vim
    – Leo Simon
    Jun 8 '18 at 23:40
  • Are you sure? syntax/nosyntax.vim does not contain any functions. You can also try :verbose :fu <snr>7_PaddingFunction. That one is responsible for your error and I suppose it has a bug. Jun 9 '18 at 8:05
  • Thanks, I posted the entire output of :scriptnames I tried typing :verbose :fu <snr>7_PaddingFunction and received an error: E123: Undefined function: <SNR>7_PaddingFunction
    – Leo Simon
    Jun 10 '18 at 0:47

This seems like a job for Tim Pope's surround.vim.

Add the following to ~/.vim/after/ftplugin/tex.vim:

let b:surround_{char2nr("P")} = "\\Pad{\r}"

Now you can do ys{motion}P or other surround operations (See :h surround). See :h surround-customizing for more information about customizing surround.vim.

There is also vim-sandwich which is an alternative to surround.vim.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.