Emacs: (next-line) and (previous-line) behaves incorrectly - emacs

I have added the following code in my .emacs file:
(global-set-key (kbd "C-i") 'previous-line)
(global-set-key (kbd "C-k") 'next-line)
After that I start Emacs and push C-x C-b to open *Buffer List* buffer. Then I push C-x o to make it active.
After these actions I try my new keybindings and they work strangely! C-i prints No buttons! at the bottom of the frame. C-k moves the point to the next line but prints D at the previous one.
Any suggestions?

If you ask emacs to tell you what the keys are bound to, using C-h c C-k RET and C-h c C-i RET in your Buffer List buffer, you will be told that
C-k runs the command Buffer-menu-delete
TAB runs the command forward-button
Just because you bound those keys to other functions in the global key map does not mean that those bindings are active in any particular buffer, because there may be other keymaps in force that take precedence over the global map.
This is a large subject and you must proceed with caution (in particular, note that C-k is somewhat destructive: it marks the buffer for deletion - but it is conceivable that in a different buffer with a different mode, the map in force might make it much more destructive).
You can read about keymaps in the emacs manual.

Related

How to bind C-x SPC locally to gud mode in Emacs 25

Similar to How to change GUD breakpoint keybinding to the old one but I would like to bind CTRL+x followed by SPACE to be gud-break.
What I have working (well it is a hack; keep reading) is:
(define-key ctl-x-map " " 'gud-break)
but ctl-x-map is a global variable akin to the global map. And in fact, if I switch to another C++ buffer and type C-h k C-x SPC I get:
C-x SPC runs the command gud-break (found in global-map), which is an interactive compiled Lisp function.
It is bound to C-x SPC, C-x C-a C-b.
(gud-break ARG)
Set breakpoint at current line.
which means the global definition across all buffers is what was changed, which is not correct AFAIK.
Is there a way to "insert" or somehow affect the local key map for C-x, which I believe is gud-mode-map, because I want that binding to not be global for all C++ buffers. E.g., the global binding for CTRL+x followed by SPACE is rectangle-mark-mode.
Yes I realize the standard binding is C-x C-a C-b for gud-break, but that is asking for RSI.
Update #1
Since I need the local key map for gud-mode-map, this needs to be active only when I'm running a debugger. In my case, this is in C++ mode buffers, but my understanding is that gud-mode-map becomes active in those C++ mode buffers only during the debug session, and is removed from the key bindings when gud mode is finished.
Update #2
This did not work:
(define-key gud-mode-map [(control x ?\ )] 'gud-break) ;; <-- gave "Two bases given in one event" error too.
(define-key gud-mode-map (kbd "C-x SPC") 'gud-break) ;; <-- this does not work either.
Update #3
As an experiment, I commented out my define-key bindings in my hook I add to gud-gdb-mode-hook, reran gdb, then switched to the gud buffer (not the C++ source file) and typed C-h k C-x SPC I get this:
C-x SPC runs the command gud-break (found in gud-mode-map), which is
an interactive Lisp closure.
It is bound to <menu-bar> <debug> <break>, C-x SPC, C-c C-b, C-x C-a
C-b.
(gud-break ARG)
Set breakpoint at current line.
But then when I switch over to the C++ buffer that should also have the same bindings inserted temporarily (while gud mode is active), and then do the same thing I get this instead:
C-x SPC runs the command rectangle-mark-mode (found in global-map),
which is an interactive autoloaded compiled Lisp function in
'rect.el'.
It is bound to C-x SPC.
(rectangle-mark-mode &optional ARG)
Toggle the region as rectangular.
Activates the region if needed. Only lasts until the region is deactivated.
Switching back to the gud buffer, and typing C-h m shows this:
Debugger mode defined in 'gud.el':
Major mode for interacting with an inferior debugger process.
You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
hook; 'gdb-mode-hook', 'sdb-mode-hook', 'dbx-mode-hook',
'perldb-mode-hook', 'xdb-mode-hook', or 'jdb-mode-hook' respectively.
After startup, the following commands are available in both the GUD
interaction buffer and any source buffer GUD visits due to a breakpoint stop
or step operation:
C-x SPC sets a breakpoint at the current file and line. In the
GUD buffer, the current file and line are those of the last breakpoint or
step. In a source buffer, they are the buffer's file and current line.
...
Notice the reference to C-x SPC above. It is as if they intended to bind C-x SPC but it did not work, or something is preventing it from being bound properly in that buffer when gud mode is entered.
I'm not exactly clear on what you want. But if you only want to redefine the key C-x SPC when the gud-mode-map is active, then tell define-key you want to use that map: (define-key gud-mode-map ...).
I've chosen to give up on this altogether. using C-x SPC in the buffer is problematic anyhow. I noticed that the C-x C-a prefix that is used for the gud bindings are left in the C++ buffer even after gud finishes, so the gud mode is not cleaning up after itself anyhow.

How to set keyboard s-up to go move cursor to start of buffer in emacs (and s-down to move cursor to end of buffer)?

What can I add to my init.el to move the cursor to the very first character of the open buffer when I press s-up (i.e. hold "command" key, press "up" key on macOS), and equivalent for s-down?
I know it should be something like (global-set-key (kbd "s-<up>") ... )
(global-set-key (kbd "s-<up>") 'beginning-of-buffer)
(global-set-key (kbd "s-<down>") 'end-of-buffer)
You can find out what command a given key is bound to by using C-h k.
In this case, M-< takes you to the beginning of the buffer, and C-h k M-< tells you this:
M-< runs the command beginning-of-buffer, which is an interactive
compiled Lisp function in simple.el.
It is bound to begin, C-home, M-<, menu-bar search goto beg-of-buf.
(beginning-of-buffer &optional ARG)
Move point to the beginning of the buffer.
With numeric arg N, put point N/10 of the way from the beginning.
If the buffer is narrowed, this command uses the beginning of the
accessible part of the buffer.
If Transient Mark mode is disabled, leave mark at previous
position, unless a C-u prefix is supplied.
Don't use this command in Lisp programs!
(goto-char (point-min)) is faster.
Similarly, for M->, which is bound to end-of-buffer.

emacs cider clear REPL buffer

I simply want to clear the repl buffer so that a single prompt eg (user>) is left on the first line.
I have a keybinding:
(put 'erase-buffer 'disabled nil)
(global-set-key (kbd "C-x C-<backspace>") 'erase-buffer)
But this gives the message :
text is read only
There is the option C-c C-o but this only clears the last return value.
When using python, and run-python the following command C-x M-o which i believe is comint-clear-buffer
cider-repl.el provides a function cider-repl-clear-buffer which by default is bound to:
M-x c-r--bu RET
as C-c M-b is not used by cider-repl as far as I am aware:
(add-hook 'cider-repl-mode-hook
'(lambda () (define-key cider-repl-mode-map (kbd "C-c M-b")
'cider-repl-clear-buffer)))
cider-repl.el also provides cider-repl-handle-shortcut which is bound to ,.
Which will prompt you to many commands, such as clear (which you want), ns (to change namespace), refresh, reload and many others
I find pressing , followd by enter (to choose clear, faster/more convenient than the other answer.)
Note: you need to type , into the repl while the line is empty, it works for both evil and normal emacs keybinds

How to invoke the buffer list in Emacs

I usually type M-x buffer-menu to switch buffers in Emacs. How can I do this with a shorter command? Its quite a long string to type.
Thanks!
You can use C-x b to change buffers. You have to enter the first few letters of the buffer name, and of course you can use completion. If you press TAB (the most useful key in Emacs), a list of (matching) buffers appears. You can click in this list to switch to a buffer.
You can bind buffer-menu to a key. Pick a key that's not used for another command — let's say f12 — and add the following line to the file ~/.emacs:
(global-set-key (kbd "<f12>") 'buffer-menu)
There are many other interfaces to changing buffers in Emacs, and they can be significantly more efficient than C-x b and C-x C-b. Since this tends to be a very personal choice, I recommend you experiment with a few and keep the one(s) you feel most comfortable with.
C-x C-b
As stated here
I'd highly recommend switching to a mode designed for efficient buffer switching.
If your version of Emacs is recent enough (22+):
M-x ido-mode
and then:
C-x b
to switch buffers, with incremental substring matching, C-s and C-r rotate forward and backwards through the matches.
If you have an older version of Emacs, it should have:
M-x iswitchb-mode
and then, as with ido-mode:
C-x b
opens up the minibuffer to let you choose the buffer to switch to.
Bind C-x C-b to buffer-menu. There is no sense leaving it bound to list-buffers. list-buffers is just a eunuch version of buffer-menu. ;-)
And you might want to try this: http://www.emacswiki.org/emacs/BufferMenuPlus
Try bs-show (in my opinion a way better than C-x C-b). You can bind it to F9 by adding this to .emacs:
(global-set-key (kbd "<f9>") 'bs-show)

How do I save multiple buffers (of my choosing) at the same time in Emacs?

When I press C-x s (save-some-buffers) or C-x C-c (save-buffers-kill-terminal), Emacs displays the names of modified buffers one by one and asks what to do with each (save, diff, pass, ...). Pressing y one by one is slow. Pressing ! doesn't let you see what buffers are being saved.
How can I have the names of all modified buffers displayed first so that I can mark off some of them and save all the other quickly?
C-x C-b (M-x list-buffers) displays a list of all the buffers. Modified ones will be shown with a * next to them. You can mark a buffer for saving by pressing s. When you're done, press x to save all the buffers you marked.
Unfortunately, as far as I know, there's no way to show only the unsaved buffers or to sort them so they're all at the top.
(I actually prefer M-x ibuffer to M-x list-buffers, but ibuffer provides a similar feature.)
In emacs 23, with ibuffer :
'M-x ibuffer' (to open a list of buffers)
'*u' (start and u at the same time) to marked all unsaved buffers
'S' to save all marked buffers
Strangely enough, *u does not mark 'special' buffers like scratch, compilation, etc... I suppose i regexps on the name ...
Use ibuffer, which should come with all late-model emacsen. Put the following in your .emacs file:
(autoload 'ibuffer "ibuffer" "" t)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(defun my-ibuffer-load-hook ()
"Hook for when ibuffer is loaded."
(define-ibuffer-filter unsaved-file-buffers
"Only show unsaved buffers backed by a real file."
(:description "unsaved file buffers")
(and (buffer-local-value 'buffer-file-name buf)
(buffer-modified-p buf)))
(define-key ibuffer-mode-map (kbd "/ *") 'ibuffer-filter-by-unsaved-file-buffers)
)
;; (add-hook 'ibuffer-load-hook 'my-ibuffer-load-hook)
(eval-after-load 'ibuf-ext '(my-ibuffer-load-hook))
Then use C-x C-b to bring up the ibuffer list, and / * to show just unsaved buffers backed by a real file (so you don't see *scratch* in the list, for example). Mark the desired buffers with m and then save them with S.
The answer to the question in the title is to pass an argument to save-buffers-kill-emacs (or -kill-terminal), ie. use the key combo C-u C-x C-c which will silently save all buffers and exit (or C-u C-x s to just silently save all buffers).
In emacs 23
C-x C-b (M-x list-buffers) to view buffer list
m to mark buffers to save
S to save marked buffers
u to unmark buffers individually or M-x dired-unmark-all-marks for all
I have googled now for this question and found the solution here
http://johntellsall.blogspot.com.es/2013/03/emacs-save-all-modified-buffers.html
You have to add this config to your ~/.emacs.d/init.el emacs configuration
(global-set-key
(kbd "M-*")
(lambda ()
(interactive)
(save-some-buffers t)))
save and eval the buffer (M-evalb-buffer) of the init.el file, and then when you want to save all the modified files you only have to press Meta key with "*" as is indicated in the second line
I hope this solution works!
Juan