0

For my Latex setup in VIM I want to map different text modules where the cursor is automatic placed in between (<>), when I press twice <space> and when the cursor arrives there the (<>) will be removed.

As an example the mapping would look like:

imap ;b \bold{(<>)}

Which command does it need in my vimrc, that the cursor is automatic jumping in here (<>)? Or is this only possible with a specific plugin?

4 Answers 4

1

The simplest approach is to just use <left> a bunch of times.

imap ;b \bold{()}<left><left>

You could wrap this in a function such as

function! Domap(str) abort
    let idx = stridx(a:str, '<>')
    let lefts = len(a:str) - idx - 2
    let cmd = strpart(a:str, 0, idx) . strpart(a:str, idx + 2) . repeat('<left>', lefts)
    execute cmd
endfunction

call Domap('imap ;b \bold{(<>)}')
1
  • 1
    OP's placeholder pattern is actually (<>). (And it's definitively better to prefer inoremap here) Nov 2, 2022 at 3:04
1

Shameless plug: in lh-brackets, I provide functions like lh#map#no_context() that permits to define such mappings (using <++> instead of (<>)). It also supports multiple placeholders. It automatically prevents expansion within comments, and so on. Plus other functions that help defining in one go: mappings (and menus) for insert, normal and visual modes (surrounding actions).

Now, as Mass suggested, you don't need something that complex.

" prefer inoremap to avoid undesired interactions with bracketing plugins
:inoremap ;b \bold{}<left>

is enough. Or almost because you'll then want features that permits to automatically jump over one, or (much more useful!) several closing brackets. That's were bracketing plugins come in handy. Moreover, thanks to them, the mapping definitions could be simplified:

" this time we should NOT use the nore version
:imap ;b \bold{

And if the mappings become a little bit more complex (like environments which are multilines and that may have several placeholders: here an example of what I do with mu-template), a snippet plugin would quite certainly be a better fit.

1

An alternative approach that I would recommend is to use snippets, such as UltiSnips. It has very good support for placeholders, which are used to specify where the cursor is to go.

In your example, the code to setup the snippet would be

# tex.snippets file

snippet ;b "bold"
\bold{($1)} 
endsnippet

where $1 is used to denote the first placeholder.

Then in vim, hit ;b<tab> to get \bold{(|)} where | represents the cursor.

This approach easily extends to other use cases (such as latex code for graphics and figures, tables, tikz code, and so on).

0

I came across the solution of Luke Smith. So there is no need for a plugin:

I include in my .vimrc:

inoremap <Space><Tab> <Esc>/<++><Enter>"_c41
vnoremap <Space><Tab> <Esc>/<++><Enter>"_c41
map <Space><Tab> <Esc>/<++><Enter>"_c41

Like this I have <++> as a guide.

Then I can start to enter my Latex commands. Example:

inoremap <Space><Tab> <Esc>/<++><Enter>"_c41
vnoremap <Space><Tab> <Esc>/<++><Enter>"_c41
map <Space><Tab> <Esc>/<++><Enter>"_c41

Exact what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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