Hitting TAB key while entering text into scratch buffer does not do anything. I would like TAB key to behave exactly as it would in other normal buffers(move point forward by inserting some X number of spaces or insert a TAB character). Could you please help me achieve that? Thank you.
I have come across below question, but it doesn't have an answer and the link mentioned in its comment is not working. I do not have enough score to add a comment to it.
How to enable tab key in scratch buffer of emacs?
Feel free to close the current question if you could add an answer to above question.
TAB's behavior in most programming-language modes is not "insert a tab" but "make sure the code on this line is indented correctly according to the current rules". The default mode for the scratch buffer is lisp-interaction-mode, and since you have not written any Lisp code, there is no indenting to do, so TAB does nothing.
There are a few ways you could change this. You could change the major mode to, say, text-mode or fundamental-mode, either for a single session (with M-x text-mode) or permanently (by putting (setq initial-major-mode 'text-mode) into your .emacs file).
Or you could leave the mode alone, and rebind the TAB key entirely. One way to do this would be
M-: (global-set-key (kbd "TAB") 'self-insert-command)
I'm sure there are many other alternatives, depending on how exactly you want your scratch buffer to act.
If all you're interested in is to insert the TAB character (i.e. \t), then you could use the quoted-insert function. By default it's bound to C-q. It captures the next input character and inserts it verbatim. So in your case that would be C-q TAB.
I posted that comment with the now dead link, so I'll quote from the Wayback Machine copy:
Emacs isn't inserting anything!!
If you feel like I do, you probably are considering this a fault. You keep pressing the TAB key, but nothing happens.
In programming modes, such as when you're editing C or Perl or Lisp source code, the TAB key is bound to special indentation rules. That is, instead of being bound to indent-relative as in text-mode, the TAB key is pre-bound to cc-indent-line or lisp-indent-line (if editing your .emacs file), and so on. In c-mode, pressing the TAB key will move the cursor to the first indentation level, and then may not move the cursor forward after that, no matter how many times you press it.
If this behavior isn't what you want, you can do one of these things:
Press Ctrl-q <TAB> to insert a TAB character right now
Temporarily reassign the TAB key to self-insert-command while staying in the same editing mode
Switch to a different editing mode for this session; the TAB behavior will change with the editing mode
Change your .emacs file to permanently change the editing mode for the filetype you're using now
I'd recommend reading the entire page, as it explains well how Emacs treats the TAB key and tab characters different from pretty much everything else.
Related
In Emacs, I don't want the *Completions* buffer to pop up, even when I press tab to autocomplete in the minibuffer and there are multiple results.
It's distracting and jarring.
How can I do this?
Even better, I would like an alternative that isn't distracting or jarring -- such as requiring one tab for autocomplete if available, but requiring two tabs to open a Completions buffer. This way, I don't get the Completions buffer when I'm expecting an autocomplete. This is what the OS X terminal does to show tab completion possibilities.
I think the cause is the minibuffer-completion-help command, which is run automatically, described here: https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-Commands.html
I use ido and smex, but the problem occurs in a vanilla Emacs too.
EDIT: I found a hack to fix this. Using M-x find-function, I found and copied the function definition of minibuffer-completion-help into my .emacs.d/init.el file. Then, I renamed the version I copied my-minibuffer-completion-help and changed (with-displayed-buffer-window *Completions* to '(with-displayed-buffer-window *Completions* (putting a quote in front so it is just interpreted as a string. Finally, I overrode the call to minibuffer-completion-help by putting
(advice-add 'minibuffer-completion-help
:override #'my-minibuffer-completion-help)
after the my-minibuffer-completion-help function in my .emacs.d/init.el file. There must be a better way.
EDIT 2: quoting out (message "Making completion list...") in my-minibuffer-completion-help has the added benefit of getting rid of the flicker in autocomplete that is caused by flashing another message during autocomplete. Is it possible to do this another way?
I believe you just want to set completion-auto-help to nil:
(setq completion-auto-help nil)
I believe what you're looking for is either temporarily modifying display-buffer-alist, probably setting it to use display-buffer-no-window for *Completions* or M-x customize-group completion and setting Completion Show Help to nil
So I've heard about the goodness of emacs and have only recently started using it. Forgive me if this is a stupid question, but why does emacs display tabs, differently? It seems as though it doubles the number of spaces, but it doesn't, at least, I guess. Here're some pictures to describe what I'm talking about:
And this is what it looks like in emacs:
As I've previously stated, it seems as though it doubles the number of spaces. When I add this line to .emacs:
(setq c-basic-offset 4)
and reindenting the code using C-x h C-M-\ makes it look normal in emacs, but the secondary indentation are in-line with the first indentation (as in 2 tabs are now 1 tab) when viewed in other text editors, and again, I couldn't understand why. Changing it to
(setq c-basic-offset 8)
makes it save and display normally in other text editors though. At this point I'm really, really confused.
Can someone please explain why? Thanks.
The variable tab-width is the distance between tab spaces in columns, and defaults to 8. If you'd like it to default to 4, you can (setq-default tab-width 4). If you'd like to untabify everything and convert tabs to spaces, you can do M-: (untabify (point-min) (point-max)).
And you might find this thread helpful, especially the point on tab-stop-list when you want to ADD your own tabs.
You can also adopt sanity and not use TAB chars in your code. ;-)
To prevent inserting TAB chars when you hit the TAB key (and RET or C-j, depending on your Emacs version) set the value of option indent-tabs-mode to nil.
To remove pre-existing TAB chars from code you are editing, use command untabify.
See also Tabs Are Evil and Untabify Upon Save.
And note that, in Emacs, whether or not you use TAB chars is unrelated to how much and whether code is indented. For example, option c-basic-offset governs indentation amount regardless of whether TABs are used for some of the indenting.
Note too that after you kick the TAB habit, any TAB chars left in your code that are meaningful to the code are much easier to find. They are not lost in an ocean of insignificant-whitespace TABs.
Finally, note that there are various ways to highlight TAB chars. Command hc-toggle-highlight-tabs in library highlight-chars.el is one way. See Show Whitespace.
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.
I personally keep all lines under 80 characters, but I also work on projects in teams where other programmers don't care about line length.
I love using whitespace-mode, but the long line visualization is really annoying when I'm working on projects where I shouldn't interfere with the long lines. It seems like it should be easy to turn off the long line visualization---I hit m-x global-whitespace-toggle-options l, and then can hit m-x global-whitespace-toggel-options ? to confirm that the "long-line visualization" is turned off. But long lines are still highlighted. I kill buffers and reload them, and highlighting is still there. I'm definitely using global, not local, whitespace-mode.
Why can't I turn off the long line visualization?
The last time I customized whitespace-mode, I noticed that my changes to the settings didn't have any effect in buffers that already existed; try recreating the buffer, or leaving and reentering whitespace-mode. In case you don't already know, you can use M-x customize-group whitespace to turn off that particular option entirely, rather than doing it manually.
Edit: Specifically you want to customize the whitespace-style variable. This lets you turn on and off individual styles. In this case you should turn off the ones labelled "(Face) Lines" and "(Face) Lines, only overlong part". The former changes the face of the whole line when it is overly long, while the latter only changes the face of the part that extends past the threshold.
(Other options in this group define the faces that whitespace-mode will use to highlight the styles you've turned on, the regexes it uses to identify certain situations, etc, but usually you only care about whitespace-style).
Set whitespace-line-column to a higher value (default is 80), so the highlighting of long lines doesn't kick in:
(setq whitespace-line-column 250)
I'm assuming that you already have whitespace-mode activated somewhere in your init.el or similar. If so, you can adapt duma's comment above, and either
Edit the elisp that sets whitespace-style to remove lines-tail. E.g., Emacs Prelude sets
(setq whitespace-style '(face tabs empty trailing lines-tail))
Simply change that to
(setq whitespace-style '(face tabs empty trailing))
If you don't want to directly edit that elisp, but rather override it later with your own code, do something like
(setq whitespace-style (delete 'lines-tail whitespace-style))
Unfortunately, if running Prelude with auto-loaded buffers (using something like Emacs Desktop), the initial setting will take precedence: for each buffer on which you want to see whitespace-style displayed as directed, you must [1]
kill the buffer
re-open the buffer
[1]: Note to OP: if there's another way to reload a buffer, please edit or comment this answer. I was hoping to find something like M-x reload-buffer but am not seeing anything like that with C-h a buffer.
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.