Eclipse style alt left/right navigation within emacs - emacs

I am switching to emacs and in the process of configuring my emacs environment, I am stuck with implementing a functionality which I used to have in eclipse and IntelliJ. Move back and forth in code with alt left/right keys.
I want it to be able to work across all major modes and across all buffers(not just for java or any specific language). For example,
say I'm at beginning of file1.txt(say p1). I search for some text and go to line 10(p2) in file1.txt. Then I open another file, file2.txt and repeat the same , start at line 1(p3) and then go to line 10(p4). Upon Alt + Left and Alt + right (or any other similar keybinding), I should be able to cycle between positions p1 <-> p2 <-> p3 <-> p4
I am aware of the following.
ctags/etags specific approach of generating tags, jumping to functions and then back. But I want it to be applicable to text files and a lot of files for which ctags cant be generated or not worth generating as I use them rarely e.g Makefiles generated from cmake builds etc.
Questions, answers and comments in
https://superuser.com/questions/241939/how-to-jump-back-to-the-last-position-of-the-cursor-in-emacs
,
In Emacs, how to go back to previous line position after using semantic Jump to Symbol?
where answers are mentioned about using mark ring and registers for
storing mark. But after trying such approach initially, I found that
once if I go forward, then backward, I pop stuff from the register.
I need it to be there forever(:D )
I read really bad things about icicles and bit hesitant to try
it.
Any other approach that I have missed ? Any plugin or anything ? Implementing this is a bit complex. I hoping this is a common problem and hoping some piece of elisp code exists for solving this problem.

maybe this is useful .maybe not
https://github.com/joodland/bm
(setq-default
bm-recenter nil
bm-highlight-style 'bm-highlight-line-and-fringe
bm-cycle-all-buffers t
bm-in-lifo-order t)
;; (global-set-key (kbd "M-.") 'bm-toggle); you need set mark before you jump
;; (global-set-key (kbd "M-/") 'bm-next)
;; (global-set-key (kbd "M-,") 'bm-previous)
and I know evil-mode
C-o evil-jump-backward
C-i evil-jump-forward
whenever you use gg G / it can remember last position

Related

Better defaults for emacs

I've been using Vim for a several years. And now I want to give a try to Emacs.
For Vim I have a general config file (here) where I'm overriding defaults (e.g. hey, Vim, show me the line numbers; save more history, don't create these stupid backup files, etc...)
I want the same thing for Emacs. While searching, the best thing I've found is better-defaults.el from technomancy. I'm still digging in Prelude and Emacs-Starter-Kit sources, but there are too many overrides and plugins.
So, what I want:
ability to see a list of variables, which I can customize (e.g. indent-tabs-mode or newline-and-indent). I know about C-h v variable-name but this command requires me to know a name of variable, but I want a list of them
sample config file for Emacs which sets helpful defaults with comments for each command
For your first question: M-x customize-option.
C-h v TAB is not what you want, as it shows you also non-option variables (e.g., internal variables).
However, if you load library help-fns+.el then C-u C-h v TAB shows you only the user options (in buffer *Completions*).
My advice would be to not look for an existing "sample config file", if you intend to start with it, as opposed to just seeing how another user redefines things. And for help with the latter, I would still recommend the Emacs manual over looking at someone elses init file. Especially to start with.
However, if you really want to look at init files from other users then this is the place to start. (And this is a good place to start, other than the manual (which is the best place), to learn about customizing Emacs.)
Finally, my (unsolicited) advice wrt learning Emacs, including customizing, is to start by not customizing it at all. I say that without irony as one who has heavily customized Emacs.
If you want to "get it", i.e., to get a feel for the Emacs design and what makes it different, then let yourself get used to Emacs as it is out of the box -- for maybe a month or so. At that point you can think about customizing, and your customizations are likely to be much wiser (in your own terms, i.e., for whatever it is that you want).
Another way of putting this is that until you know Emacs a bit, you really do not know what it is that you want or need in terms of customization. In particular, it would be a mistake, IMO, to start out by trying to think of Emacs in terms of Vim or trying to make Emacs do what you've done in Vim. There is plenty of time for that later, if, based on understanding Emacs, you really do want to do that.
Welcome to Emacs. Enjoy.
I'm going to take a reasonable dissent from Drew's excellent answer, there are some things you really ought to set in your emacs-file immediately, that aren't set out of the box that you really ought to set.
Issue number 1: THAT $(generate-swearing) BELL!
The bell will ding like a madman. That's annoying. You can turn it off.
In your init-file, do this:
(setq visible-bell 1)
Issue number 2: Emacs has an interesting view of backup files.
If you edit a file, say "foo.txt", emacs will create little backups of the file with the name "foo.txt~" in the same directory.
This is annoying as all hell, and you can fix it by doing this:
(setq backup-directory-alist '(("" . "~/.emacs.d/emacs-backup")))
Issue number 3: Emacs uses C-w differently than bash does, and that's a bit annoying.
C-w usually deletes a word backwards. By standard in emacs, it deletes the marked region. That's a bit silly.
It is better to do something like this:
;; This is my preference, your mileage may vary.
(global-set-key (kbd "C-x C-k") 'kill-region)
(global-set-key (kbd "C-x k") 'kill-buffer)
(global-set-key (kbd "C-w") 'backward-kill-word)
Issue number 4: Alt-X is a clunky way of running an interactive command.
It is better to do something like this instead, avoid your hand cramping up all the time.
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
You also may want to check out Steve Yegge's Effective Emacs: https://sites.google.com/site/steveyegge2/effective-emacs
It's pretty amazing. One thing to note though is that the caps-lock to ctrl thing is also available through a microsoft tool here:
https://learn.microsoft.com/en-us/sysinternals/downloads/ctrl2cap
This is better than the manual hack Yegge suggests, and you can turn it off if you don't like it.

Let Emacs move the cursor off-screen

Is it possible to let Emacs have the cursor be moved off-screen, like most GUI text editors work? This is one of the biggest things that bothers me when I use Emacs over any GUI editor. When I scroll down, the cursor is "pushed forward" by the top of the buffer.
I had previously thought that this was completely impossible, because this is hard-wired into the architecture of Emacs, but then I saw multiple-cursors, which does exactly this for the secondary cursors (assuming you prevent the scrolling functions from acting on the secondary cursors). Is it maybe possible to use multiple-cursors to have the main cursor in some hidden buffer, and the effective cursor being what I actually edit with? Or maybe some other clever trick? Or maybe my Googling has failed me and this is really already possible without any magic?
There is a new package available on GNU ELPA called scroll-restore that attempts to remedy this problem. So far, I have encountered a few bugs, but the package seems to work as-advertised for the most part.
You can test it out by installing it with
M-x package-install RET scroll-restore RET
After the package is installed, you can enable the minor mode with
M-x scroll-restore-mode
Personally, I am binding it to the Scroll Lock key because it seems so incredibly apropos! This is what I am adding to my init file:
(require 'scroll-restore)
(scroll-restore-mode 1)
;; Allow scroll-restore to modify the cursor face
(setq scroll-restore-handle-cursor t)
;; Make the cursor invisible while POINT is off-screen
(setq scroll-restore-cursor-type nil)
;; Jump back to the original cursor position after scrolling
(setq scroll-restore-jump-back t)
;; Toggle scroll-restore-mode with the Scroll Lock key
(global-set-key (kbd "<Scroll_Lock>") 'scroll-restore-mode)
This is a direct copy of an answer posted here: https://emacs.stackexchange.com/a/2273/93
Strictly, speaking you can't move the cursor offscreen, because the underlying C code won't let you do it.
This said, I suspect that your problem can be fixed. Basically, there are 2 aspects:
you don't like the way things look when "the cursor is pushed forward". You could work around that by (temporarily) making the cursor invisible.
you want to "jump back" to the pre-scrolling position as soon as you issue a non-scrolling command. There's probably some package out there that does it for you, but you can do it yourself with some pre-command-hook hacking.
BTW, I'd welcome patches in Emacs which provide some of that functionality. I hate the "auto jump-back" behavior of other editors, but it would be good to record the "pre-scroll" position and then offer a way to jump back to it.
Judging by the context and description of your problem, it looks like you want to browse the buffer while preserving your place of editing. There are at least two tricks for that purpose: marks/registers and splitting in two windows.
https://stackoverflow.com/a/3777598/308668 describes the Emacs' registers that act like Vim's marks. You can check in your place in the file with keying C-x r SPC a (a being a letter of your choice) and you can always come back with C-x r j a.
Some sort of automation is achieved by an external script (goto-last-change.el), described here: https://superuser.com/a/184402/49046.
Alternatively, split the window in two with C-x 2. The newly split windows both show the same location and you can use the other to explore. C-x 0 closes a window when you're done.

Additional modifier keys in emacs?

I have been modifying my Emacs setup quite alot recently but I reached a problem which is starting to annoy me. I would like to be able to introduce additional modifier like keys. What I am trying to do, to make things clearer, is when I am in dired-mode (which doesn't accept textual input so normal letters can be rebound) I would like it so that when I hold down the letter s and press j or l the cursor moves to the next and previous directory line respectively. Effectively making the s key act like a modifier.
I have looked into making the s apply a modifier such as super or hyper but those are all used for global things. Is this possible? if not then that's a shame.
Edit:
There seems to be some confusion with what I'm after. If I define a normal key sequence such as
(define-key map (kbd "s j") 'dired-next-dirline)
Then I have to keep pressing the s key every time before I press j to move to the next directory line. This is not what I am looking for (not to sound angry :P) I want s to act like a modifier where I can keep the s key held down and keep tapping j to move down the lines.
I hope I have made this more clear. Thanks.
I dug around in the code and came up with this. It binds s j to my-dired-next-dirline, but right after you've done that, just j is enough to do it again. Any other key resets the temporary binding.
Note that the function set-temporary-overlay-map was added in Emacs 24.2, which at the time of this writing hasn't been released yet, so you'll need to build Emacs from git.
(defun my-dired-next-dirline ()
(interactive)
(dired-next-dirline 1)
(set-temporary-overlay-map
(let ((map (make-sparse-keymap)))
(define-key map [?j] 'my-dired-next-dirline)
map)
nil))
(eval-after-load "dired"
'(progn
(let ((prefix-map (make-sparse-keymap)))
(define-key prefix-map "j" 'my-dired-next-dirline)
(define-key dired-mode-map "s" prefix-map))))
Key chords may be what you are looking for.
This question pre-dates it, but the Hydra package allows you to do this and more.
https://github.com/abo-abo/hydra
See examples here: https://github.com/abo-abo/hydra/blob/master/hydra-examples.el and here https://github.com/abo-abo/hydra/blob/master/hydra-test.el
Wiki also has more useful examples https://github.com/abo-abo/hydra/wiki/Hydras-by-Topic
Hydra originally only had a rather opaque metaphorical syntax, which made it quite difficult to get started with. I'm not sure why such a bizarre double-layer of metaphors from colors to mythical stories and monsters with many heads being "vanquished" was used to describe features which are essentially:
do action and remain in the key-mode (effectively what you're asking for.) (a so called blue hydra)
do action and quit the key-mode (a red hydra)
and so on.
Thankfully a more literal syntax was added in Feb 2015, inspect the tests to see the color and literal syntax side by side. (also this commit https://github.com/abo-abo/hydra/commit/0a3cc60f5856eb4a38204b9075d67d058ba56bef)
See also this article by the author. http://oremacs.com/2015/02/02/colorful-hydrae/
If key chords is not what you are looking for (as you suggested on a comment to Tom's answer), then you seem to want a simple define-key that uses a key sequence. i.e.
(define-key dired-mode-map (kbd "s j") 'dired-previous-line)
This has the disadvantage of disabling the original functionality of the s key, but you can rebind that to the sequence s-s, I suppose.

Refactoring a big file in emacs

I'm refactoring a big piece of code in one file in Emacs.
What is the best way to simplify jumping to several places in a big emacs buffer?
Currently I'm using search (C-S) and custom comments - "markers".
This becoming quiclkly unreliable.
Ideally I would like to have the same file open in several buffers, so I can switch between them using C-X B.
What are your solutions?
It seems that what you are looking for is Indirect-Buffers.
Personally, I find that splitting my window (C-x 2) is a great help.
Also bookmarks come to mind.
See Emacs Bookmarks.
Going to a particular bookmark switches to the correct buffer automatically (a bookmark is associated to a buffer).
Registers are useful for marking and jumping to positions. If you only have a small number of spots to mark and remember at any time, it may be faster to use single-character registers than named bookmarks.
C-xrSPC runs point-to-register
C-xrj runs jump-to-register
When prompted for the register, you can type any character.
Use autonamed bookmarks. No need to specify a name each time you create a bookmark -- just hit a key. Like using C-SPC to set a mark, but bookmarks are (by default) persistent; marks are not.
With Bookmark+ you can also organize bookmarks (including autonamed bookmarks) into sets etc. Tag them in various ways, for instance.
Visual bookmarks is extremely useful for this case. You can get it from here or install it using
M-x package-install RET bm
and add this to your config.
(global-set-key (kbd "<f5>") 'bm-toggle)
(global-set-key (kbd "<f7>") 'bm-next)
(global-set-key (kbd "<f6>") 'bm-previous)
Now, any where in your file press f5 and it creates a mark there. If you want to remove it, just press f5 again.
You can create any number of markers and now using f6 & f7 you can quickly go to any point you want.

Simple Emacs keybindings

I have two operations that I do all the time in Emacs:
Create a new buffer and paste the clipboard. C-S-n
Close the current buffer. C-S-w
Switch to the last viewed buffer. C-TAB
I feel like a keyboard acrobat when doing the first two operations. I think it would be worth trying some custom key bindings and macros.
A few questions about this customization:
How would I make a macro for #1?
Are these good key bindings? (I know this is a bit subjective, but they might be used by something popular that I don't use.)
Has anyone found a C-TAB macro that will act like Alt+Tab in Linux/Windows? Specifically, I want to have a stack of buffers according to the last viewed timestamp (most recent on top). I want to continue cycling through the stack until I let go of the Ctrl key. When the Ctrl key is released, I want the current buffer to get an updated position on the stack.
Have you tried using vertically or horizontally split windows for this (via C-x 3 or C-x 2)? It seems like it would give you fewer steps - even if you implement something like you're talking about.
I find split windows really speed up copy and pasting operations. I use the arrow keys on my num pad to switch among windows (windmove-left/-right/-up/-down), so it's only one key to press and you go to the window you want.
I guess this is a little different from what you're asking for, but it sounds like it might help speed things along a bit.
C-x left and C-x right cycle through buffers, but you have to hit it multiple times, you can't just keep the key pressed down.
For creating a macro for #1, you just start a macro, hit the keys you usually do to create a new buffer, and stop the macro.
So it would be something like:
C-x ( C-x b NEW RET C-x )
You can then save NEW to a file once you're done pasting, so you can use the macro again to create a new buffer. C-x e to try out the macro. If it works you can save it into your init.el file. This is done with:
M-x name-last-kbd-macro
Then you'll get a prompt to enter the name of your choice. This is only good for the current session. Then you save the named macro to your initialization file. First you open your .emacs or init.el file. Then you place point where you want the macro definition to go, then you type:
M-x insert-kbd-macro
Now you can run your macro using its name via M-x <macroname> . You can bind your macro to keys too (in your .emacs or init.el file):
(global-set-key (kbd "C-c a") '<macroname>)
For example this is how your init.el would look after creating a macro that opens a new buffer called NEW that is not associated with a file and binding this macro to C-c n:
;; Creates a new unassociated buffer called NEW
(fset 'new-buffer "\C-xbNEW\C-m");
;; Shortcut for new-buffer
(global-set-key (kbd "C-c n") 'new-buffer)
You can also throw in the paste, buffer close, and buffer switching operations. I guess you'd have to save the buffer to a file manually.
Some resources
Information about macros on EmacsWiki
Possibly useful: Swap text between buffers
start by invoking start-kbd-macro, finish by with end-kbd-macro. Afterwards you may immediately test the new macro with call-last-kbd-macro. If you're happy with the result you might want to save the macro.
Emacs generally doesn't use C-S keybindings and they are easy to use, so I'd call them good. They might cause problems if you're using the terminal version of Emacs, but I assume that's not the case with you.
I use this simple snippet:
(global-set-key (kbd "<C-tab>") 'bury-buffer)
bury-buffer basically makes the current buffer the last in the buffer-list so you'll be able to cycle buffers in a predictable order.
I wouldn't make a macro for that but write a function like someone else posted on this page. Instead of (cua-paste nil) you could also use (yank). I'm not sure which one's better and why.
I don't like them that much. For things that I use often I'd like to do as little finger acrobatics as possible, so that would mean modifier+key instead of modifier1+modifier2+key.. or use a function key if you don't feel tied to the homerow.
no comment