I want to add import statements in my yasnippets that go the top of the file (where they will be automatically sorted (or removed if the import already exists)).

from subprocess import call # I want this line moved to the top of the 
                            # buffer after snippet expansion

subprocess.call("$0", ${1:shell=True})

I want the import statements moved to the top of the file because if the snippet is expanded within a code block, the import statement ends up within the block, and isort won't be able to sort the import.

Is there any way I can move the import to the top of the file using an elisp snippet within a yasnippet snippet? If so, how do I do it?

(All the elisp yasnippet examples I could find dealt with modifying inserted text, not moving stuff within the buffer after the snippet was expanded...)

  • Yes, see yas-after-exit-snippet-hook (I'll try to answer my q, but will prolly take some time!) – The Unfun Cat Jan 13 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.