Reputation: 14013
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?
Upvotes: 0
Views: 88
Reputation: 5963
Try
:nnoremap <buffer> ,t ciw\texttt{<C-R>"}<Esc>
You don't need to select the word first, just make sure that the cursor is on it somewhere.
See :help text-objects
and :help i_CTRL-R
. Also :nmap would work as well as :nnoremap
in this case, but :nnoremap
is good practice since it prevents the RHS of the mapping triggering any nested or recursive mappings.
Upvotes: 1
Reputation: 2946
Try using s
instead of xi
. That deletes the selection and goes straight into insert mode avoiding the uncertain cursor position after deleting with x
.
vmap <buffer> ,t s\texttt{<Esc>pa}<Esc>
Upvotes: 5