2

This question is related to Is it possible to bind C-u C-x C-xs behavior into C-x C-x. My main goal is to jump top of the file, type import and paste a package name and jump back where I was.

Scenario:

I am on the line 10 and copied a word. Than I press M-<to jump top of the file, move down to line 1 and typed import and pasted the copied word. Now when I press C-x C-x, it jumps to the starting point of the pasted word and if I press again it jumps to end of the paster word in the line 1.

0                                     | <= jumped here and go one line below
1                                     | import hello_world  # (2) type import and 
2                                     |                       paste hello_world \
3                                     |                                         |
4                                     |                                         |
5                                     |                                         |
6                                     |                                         |
7                                     |                                         |                        
8                                     |                                         |
9                                     |                                         |
10 hello_world()  # (1)cursor is here | <= (3) I want to jump back here <--------

At this point, I just want to jump pack to point where I was (which is line 10), how can I acheive it?

1
  • as you are not intending to operate on what it should be a region, I'd encapsulate the functionality in a custom command just saving the position to get back to it.
    – Muihlinn
    Dec 10 '20 at 21:17
4

Each command that sets the mark pushes the previous mark onto a stack. More precisely, the previous position is added to the mark ring (the difference between a mark and a ring is that accessing the front of the ring doesn't remove the element, but pushes it to the other side of the ring).

To get to the next-to-last position, use C-1 C-SPC or C-u C-SPC. Repeat to get to the previous position, etc. Or use directly C-2 C-SPC to go two positions back, and so on. C-SPC (set-mark-command) is the command to set the mark, but with a numeric argument or a C-u prefix, it jumps back to a previous mark instead of setting the mark.

In your case, both M-< and pasting set a mark, so you want to go two marks back, hence C-2 C-SPC or C-u C-SPC C-u C-SPC.

If Emacs is running in a terminal, you may need to use C-u 2 or M-2 or ESC 2 instead of C-2.

1
  • One thing I've found difficult with rings is reusing an item frequently as they are moving targets. Registers worked better to me in such scenario.
    – Muihlinn
    Dec 11 '20 at 17:02
3

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.

2
  • This is smart, another issue is some times I need to go to top of the page and see the other imports to remember, if I have to use from <import_name> import <module>. That will be helpful if top 15 lines that has all imports could be seen in the mini-buffer incase to let us remember the which from <import_name> we will be using
    – alper
    Dec 13 '20 at 15:13
  • as first approach I'd clone-indirect-buffer, move to the top and will wait for some kind of user input, will have more screen state than minibuffer.
    – Muihlinn
    Dec 14 '20 at 20:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.