I am working in macvim with a latex file where I have an index where terminology is being stored (using \usepackage{imakeidx} and \makeindex).

I have a command for inserting terminology with emphasis and then indexing said terminology:

autocmd Filetype tex inoremap :nx \emph{<++>} \index{<++>}<++><Esc>01k<C-j>

The issue is that I have to type the same word or term twice which gets a bit annoying.

Is there a way of typing in both places at once?

  • 1
    Search this site for "multiple cursors". There's plenty of into about plugins as well as reasons for not using them and doing things in a more Vim way. – B Layer Jan 10 at 19:07
  • 3
    One example: Multiple cursors at desired location – B Layer Jan 10 at 19:09
  • 1
    @D.BenKnoble The one I linked/duped mentions vim-multiple-cursors...though it's in the question. Here's one that mentions the same plugin in an answer vi.stackexchange.com/questions/3165/… – B Layer Jan 12 at 14:11

I have not a direct way, but you can use a command as a workaround.

I am assuming the <++> is the terminology you want to insert.

autocmd Filetype tex inoremap :nx <Esc>:NX<Space>
command! -nargs=1 NX execute "normal! i\\emph{".<q-args>."} \\index{".<q-args>."}".<q-args>."01k\<Esc>"

When you type :nx in insert mode, you will be prompted in the command-line mode (juste at the bottom of the screen) with the pending command :NX that is waiting for an argument to be given.


:NX Entry

will create:

\emph{Entry} \index{Entry}Entry01k

Infos: :help :command :help <q-args> :help :execute :help :normal

  • I like this command approach and it works. – aladdinoscopy Jan 10 at 19:56
  • The 01k isnt meant to be inserted literally; it’s a series of normal-mode commands – D. Ben Knoble Jan 10 at 21:01

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