Tuesday, October 6, 2015

Re: How to nmap to a literal curly brace?

Hi Kenneth!

On Di, 06 Okt 2015, Kenneth Reid Beesley wrote:

> I want to edit a LaTeX source file, using (g)vim and suitable names, to add \index{…} commands.
>
> In particular, I want to be able to position the cursor anywhere on a word
> or selection to be indexed, and then type something like \ind
> to add an \index{…} command to the text, where … is the current word.
>
> For example, if the text is
>
> The dog has a bone.
>
> and (in normal mode) I position the cursor on "dog", and type \ind, I want the text to become
>
> The dog\index{dog} has a bone.
>
> If I then proceed to place the cursor on "bone" and type \ind, the text would become
>
> The dog\index{dog} has a bone\index{bone}.
>
> *****
>
> I've tried things like this (in my .gvimrc file)
>
> nmap <Bslash>ind yiwea\index{Pa}<ESC>
>
> i.e.,
>
> yiw yank the internal word (current word) to the default buffer
> e move the cursor to the end of the word
> a go into insert mode
>
> insert literal "\index{" (SOMETHING IS GOING WRONG AT THIS STEP)
>
> <ESC> escape to normal mode
> P print the buffer contents
> a go into insert mode
> insert literal "}"
> <ESC> escape to normal mode
>
> But when I place the cursor on "dog" and invoke the command, I get
>
> The dog\indexdog}{ has a bone.
>
> The left curly brace is appearing after the right curly brace.
> Where am I going wrong?

I think the P might be wrong.

I would do it differently, however:
:nnoremap <silent> \ind ciw<C-R>-\ind{<C-R>-}<esc>

Which uses the C-R to insert from the small delete register without
leaving insert mode.

See :h i_Ctrl-R and :h quote_-

Best,
Christian
--
Je älter man wird, desto toleranter gegen das Herz und intoleranter
gegen den Kopf.
-- Jean Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment