As you're not going to use the region between point and mark, consider another approach using save-excursion
, like this one:
(defun auto-import-word-at-point()
"Autoimport word at point."
(interactive)
(let ((word-at-point (thing-at-point 'word)))
(save-excursion
(goto-line 2)
(insert (concat "import " word-at-point))
(newline))))
It will copy the thing at point, goto line 2, insert it there and restore things. Bind to whatever you want and it's ready to go, thing-at-point
is just a way to obtain data, but can use anything else that you may prefer.
thing-at-point
will automagically get the specified kind of thing around point; as save-excursion
will save point, it'll return to it after executing its body, but saved point may not be the end of word, so you may want to forward-word
(M-f
) which was left out for clarity.