How can I get backward window cycling (cmd-shift-`) to work with Emacs.app? - macos

Cmd-` works fine, but when I try cmd-shift-`, I get the message, "s-~ is undefined."

Try this:
(global-set-key (kbd "s-~") 'ns-prev-frame)

Related

What is proper way to define define-key in emacs

I am trying to make toggle insert/normal mode in evil in spacemacs.
I have successfully accomplished that with code down there. But LED light next to Caps-lock on my macbook pro 2018 13' stoped working. So i am trying to switch led on my caps-lock with script.
I am using karabiner-elements to remap caps-lock to f13. When emacs records f13 it changes state to evil-normal-state or evil-insert-state.
The problem starts, when i want to add another command to run when f13 aka caps-lock ist pressed. Its (shell-command "/Users/atrumoram/setleds +caps"). Which turns the light on caps-lock or turns it off. I was trying to make my own function defun. But i really cannot make it work together. In the end i would like to have something like this.
This is code that toggles insert/normal mode in evil using capslock
(define-key evil-insert-state-map (kbd "<f13>") 'evil-normal-state)
(define-key evil-normal-state-map (kbd "<f13>") 'evil-insert-state)
In the end i would like to have something like this.
(define-key evil-insert-state-map (kbd "<f13>") 'evil-normal-state (shell-command "/Users/atrumoram/setleds +caps"))
(define-key evil-normal-state-map (kbd "<f13>") 'evil-insert-state (shell-command "/Users/atrumoram/setleds -caps"))
Is there some way somebody can help me out? Looking forward for your ideas.
A key can only be bound to one function at a time, so in order for two things to happen when you press the key, you need to create a function, make it "interactive", and bind that to the key you want.
This function performs the two actions, one after the other:
(defun my-evil-normal-state-and-set-caps-led ()
(interactive)
(evil-normal-state)
(shell-command "/Users/atrumoram/setleds +caps"))
Since it's declared as an interactive function, you can test it with M-x my-evil-normal-state-and-set-caps-led.
Then it's just a matter of:
(define-key evil-insert-state-map (kbd "<f13>") 'my-evil-normal-state-and-set-caps-led)
And vice versa for switching to insert state.

emacs lisp: how to set tab key to bind different function?

I'm trying to set the tab key to map a function.
In my application, with tab I have to select next window in screen.
I tried to define a simple function that displays a message; I used (global-set-key) to map this function on "M-h"
(global-set-key (kbd "M-h") 'hello)
It worked, message is displayed.
Now, I tried same thing with tab-key
(global-set-key "\t" 'hello)
Apparently I don't receive any error.
But, tab key doesn't display anything.
I'm using emacs on windows, but tab key works correctly in other situations.
Any suggestions?
Hmm, I'm not sure what's wrong, doing
(defun hello () (interactive) (message "hi!"))
(global-set-key "\t" 'hello)
seems to work fine for me. Does your hello function look similar? The only thing I can think to suggest is trying (kbd "TAB") rather than "\t" in the call to global-set-key. A lot of code I've seen in the wild and that I've written in my config does this. I'm not sure if there's any difference, but it's worth a shot!

Making Aquamacs scrolling more like Emacs

When I used emacs, I used to be able to set the mark and highlight full pages for yanking using C-v, or scroll-up. However, in Aquamacs if I set the mark then hit C-v it loses the mark and stops highlighting. I noticed that in Aquamacs C-v is instead mapped to aquamacs-page-down, so I tried adding the following command to my site file:
(define-key osx-key-mode-map "C-v" 'scroll-up)
and this didn't successfully remap the key. I then tried something similar:
(define-key global-map "\C-v" 'scroll-up)
and still nothing. Aquamacs very stubbornly hangs onto the mapping to aquamacs-page-down. I noticed, however, that there's an additional function, aquamacs-page-down-extend-region, which does exactly what I'm talking about. Its key sequence, however, is , and I have no idea how to input that. I tried "shift-control-v" to no avail.
Has anyone been able to get Aquamacs to scroll pages while maintaining the mark?
I've found a way to get this to work, for posterity's sake.
Paste this into the .emacs file:
;; Enable scrolling to maintain mark if set
(defun scroll-down-maintain-mark ()
(interactive)
(if mark-active
(aquamacs-page-down-extend-region)
(aquamacs-page-down)))
(defun scroll-up-maintain-mark ()
(interactive)
(if mark-active
(aquamacs-page-up-extend-region)
(aquamacs-page-up)))
(define-key global-map "\C-v" #'scroll-down-maintain-mark)
(define-key global-map "\M-v" #'scroll-up-maintain-mark)
hitting C-SPC C-v C-l, the last to recenter screen, seems to show that it does indeed preserve the mark.
and subsequent copy and yank works fine
perhaps this behavior added on newer Aquamacs.

Emacs question - hash key

I have a Mac Laptop and I am connecting to server running Linux. As Alt+3 is already bound in EMACS to a command, so I cannot insert the hash symbol in a file.
I have tried the following solution I found online:
(global-unset-key (kbd "C-3"))
(global-set-key (kbd "C-3") '(lambda() (interactive) (insert-string
"#"))) //I know that C is for CTRL not Alt - I have tried with
M-3 instead as well
and some others as well, but none seem to work. Can you tell me any
other way in which I might be able to enter the hash sign (#) in a
file.
Aso tried (did not work):
(fset 'insertPound "#")
(global-set-key (kbd "M-3") 'insertPound)
Thank you!
From http://jimbarritt.com/non-random/2010/11/07/typing-the-pound-or-hash-key-in-emacs-on-uk-macbook
Typing the pound, or hash (#) key in emacs on UK Macbook:
The problem with OS X and the UK keyboard is that the pound key actually has a £ on it. To get “#” you have to press Alt+3
Of course, in emacs, the alt key is the meta key which is trapped by emacs. The simple function below inserted into your .emacs file should map the keys correctly.
;; Allow hash to be entered
(global-set-key (kbd "M-3") '(lambda () (interactive) (insert "#")))
I assume that you have a Mac UK keyboard so Shift-3 is £. On most other keyboards Shift-3 is # as others have said.
The way I get round it is to change the input source to Australian the only difference is that Shift-3 is now # and Alt-3 is £ (or leave as the emacs binding)
Input Source setting
was System Preferences->Language&text->Input Source
On later OSX versions (OSX 10.11 definitely but would have been earlier) Input Source setting is System Preferences->Keyboard->Input Source By default this will just show the UK keyboard to see more hit the + at the bottom of the list and add Australian
The reason I prefer this rather than adding code in emacs is that Shift-3 is # for all apps e.g. including Xcode/Eclipse so I don't have to switch the key according to the app or according to wether I am on a US keyboard or on Windows/Linux etc.
I know this is a bit late and the answer has been accepted. However, I have just moved from Linux to MacOS with a UK keyboard and had the same problem.
Note: I am using the emacs from here: http://emacsformacosx.com/. The below may be different for Carbon Emacs/Aquamacs etc.
The global-set-key method above is fine if you just need the # sign, but what if you also need to access the € character? (Which is Alt-3 on a UK keyboard)
The solution for me was to add this to my init file:
(setq ns-right-alternate-modifier (quote none))
This removes the emacs bindings for the right alt/option key.
You can see all the available options with
M-x customize-group RET ns RET
Credit goes to http://emacsformacosx.com/tips
A lot of the solutions given here and elsewhere work for typing # in a normal buffer, but they don't make it work like a normal keypress; in particular, it will abort an incremental search, which makes it hard to write macros that deal with Python comments, or C #includes, for example. So, it's best to transform the key much earlier, so it just acts like another typing keystroke.
I've found that adding this command to your Emacs configuration works very well:
(define-key key-translation-map (kbd "M-3") (kbd "#"))
...and remove all the (global-set-key...) attempts.
If -- like me -- you switch your modifier keys around, Opt ⌥ is mapped to Hyper, so I just go belt-and-braces with:
(define-key key-translation-map (kbd "M-3") (kbd "#"))
(define-key key-translation-map (kbd "M-£") (kbd "#"))
(define-key key-translation-map (kbd "H-3") (kbd "#"))
(define-key key-translation-map (kbd "H-£") (kbd "#"))
(define-key key-translation-map (kbd "S-3") (kbd "#"))
(define-key key-translation-map (kbd "S-£") (kbd "#"))
My solution (note escape sequence):
;; Even though we may have set the Mac OS X Terminal's Alt key as the emacs Meta key ...
;; ... we want to be able to insert a '#' using Alt-3 in emacs as we would in other programs
(fset 'insertPound "#")
(define-key global-map "\M-3" 'insertPound)
As S.Lott said, it's S-3 to insert a number sign (or hash, pound, octothrope).
Why do you want to use the meta modifier to insert it? Also, what is M-3 bound to on your setup? You can get it by doing an C-h-k and then hitting the key combination.
Assuming you are referring to Alt properly and that it's setting the Meta modification bit you can shove
(global-unset-key (kbd "M-3"))
into your .emacs and eval it to disable this from happening.
All "normal" keys are bound to self-insert-command. The shift modifier simply upcases the 'key' which is used to call this function so you get a # instead of 3 when you do a S-3.
Also, I still don't understand why you're using Alt rather than shift to display the # symbol. What do you do when you want to type a #?

Carbon-emacs: re-enable hash key?

How can I re-enable the hash (#) key in carbon-emacs on the mac? I've tried everything I've come across in google and still can't get it working.
My config file currently looks like this:
(require 'redo+)
(require 'mac-key-mode)
(mac-key-mode 1)
(setq default-input-method "MacOSX")
(setq mac-command-modifier 'alt mac-option-modifier 'meta)
The above has enabled all the Command+Key bindings (such as Cmd+S for saving), but Alt+3 isn't working.
I'd normally work around it but I'm programming in Python and # is rather useful for comments! ;)
How about adding this to .emacs, setting up a macro and then binding M-3 to it:
(fset 'insertPound
"#")
(global-set-key (kbd "M-3") 'insertPound)
I know this is a bit late and the answer has been accepted. However, I have just moved from Linux to MacOS and had the same problem.
Posted my response on a similar question:
https://stackoverflow.com/a/21722619/1308653
I hope this helps someone.
Have you thought about moving to Aquamacs?

Resources