I was wondering whether to autocomplete e.g.
print('') and then navigate within those ticks: is it better to have a macro, or a remap, or neither? Obviously a remap doesn't allow moving after the fact, unless there's something I'm missing.
You can use
<Left> to move the cursor to inside the single quotes after you do the expansion. For example:
inoremap jj print('')<Left><Left>
The main problem with using an insert-mode mapping here is that typing a single
j in insert mode is weird and blocks waiting for another character... Also, typing
jj in any context, including inside a word, also gets affected.
An abbreviation works a little bit better here in that
jj (or whatever you pick) will only expand when it's typed as a single word. A naive attempt would be:
iabbrev jj print('')<Left><Left>
But now the problem with this approach is that you need to type a whitespace or otherwise non-keyword character after
jj to trigger the abbreviation (well, you can actually use
CTRL-] to explicitly trigger the abbreviation, but that's not too handy.)
You can work around the issue by having your abbreviation expansion "eat" the whitespace using
getchar() calls. This is mentioned in an example in
func Eatchar(pat) let c = nr2char(getchar(0)) return (c =~ a:pat) ? '' : c endfunc iabbrev <silent> jj print('')<Left><Left><C-R>=Eatchar('[ \t\r\n]')<CR>
(I also adapted the example to "eat" any of
[ \t\r\n] to cover eating the
jj<CR>, which ends up matching
\r which isn't covered by
If you type a non-keyword argument after
jj, it actually ends up inserted into the printed string, so
jj* expands to
print('*') with the cursor right after the
*. I believe that's desirable, acceptable, intuitive.
With an abbreviation, you're also free to use a prefix that's not so unique as
jj. For instance, using something like
pr would be totally acceptable, as long as you're not using that as a keyword or variable name in the language you're coding on.