I have defined several maps that encaspulate a selected piece of text, e.g. to convert “text” to “\texttt{text}”. This is one example for such a map:
vmap <buffer> ,t xi\texttt{<Esc>pa}<Esc>
However, this map does not work as expected when the selected text is at the end of the line.
Take for example this line:
word1 word2 word3
when I execute the normal command viw,t for every word in the line, this is the result that I get:
\texttt{word1} \texttt{word2}\texttt{word3}
with a trailing space, i.e. the insert of the last texttt{ happened at the wrong place.
How can I change my map to work regardless of where it is executed?
Try using
sinstead ofxi. That deletes the selection and goes straight into insert mode avoiding the uncertain cursor position after deleting withx.vmap <buffer> ,t s\texttt{<Esc>pa}<Esc>