Emacs on Mac - how to enter brackets, pipes, #, etc? - macos

I'm quite new to Emacs on my Mac and I have trouble entering signs like: []|{}# etc. Instead the minibuffer shows M-5, M-6 or jumps a word forward (M-l).
I think I got my Emacs here: http://emacsformacosx.com/
and I did install this starter kit: http://kieranhealy.org/emacs-starter-kit.html
(but I hat this problem before)
I know this answer: Unable to type braces and square braces in emacs
but I couldn't get it to work yet.

I use the following -- it will place the Emacs meta key on the Mac CMD key, and free up the ALT key for normal Mac use:
(if (boundp 'ns-command-modifier)
(setq ns-command-modifier 'meta))
(if (boundp 'ns-option-modifier)
(setq ns-option-modifier nil))

Related

How to use a different diff command for Emacs and Shell (in terminal)?

I'm using the graphical Emacs-w32 launched from a Cygwin terminal (on Windows) for all editing, and Zsh (in a MinTTY) for running commands.
I've setup up diff-cmd = colordiff in ~/.subversion/config, and, while that makes nice colors in the terminal (for svn log), that causes the problem of adding color escape codes in the output, when diff'ing in Emacs, leading to uncolorized, unreadable diffs in Emacs.
What would be the most sensible fix to this?
Try this solution found in the EmacsWiki:
if you use colordiff in svn, maybe you need this:
(add-hook 'diff-mode-hook
'(lambda ()
(require 'ansi-color)
(ansi-color-apply-on-region (point-min) (point-max))))
Source: https://www.emacswiki.org/emacs/VersionControl
By the way: searching the web for 'emacs svn colordiff' produced this as a top 5 hit...

Option key in Cocoa Emacs not entering accented characters

Using an international keyboard with TTY emacs works fine for entering characters:
alt-e + a enters á
alt-i + a enters â
etc
The problem is that in Cocoa Emacs that same doesn't hold true. These keys get interpreted as emacs commands.
I tried to unbind these keys globally, even unbinded they don't enter the correct escape character needed for international accented characters.
How to I get back to the TTY behaviour in Cocoa Emacs?
I like to have the best of both worlds on OSX, so I set the left alt/option key to META and the right alt/option key to the default native OSX settings. Here are my settings for the commonly used Emacs modifier keys:
(setq ns-alternate-modifier 'meta)
(setq ns-right-alternate-modifier 'none)
(setq ns-command-modifier 'super)
(setq ns-right-command-modifier 'left)
(setq ns-control-modifier 'control)
(setq ns-right-control-modifier 'left)
(setq ns-function-modifier 'none)

Emacs on Mac OS X Lion forward delete

I updated my iMac desktop from Snow Leopard to Lion. I use an extended USB keyboard which has two delete keys: one together with the usual keys (above \) and one in the extended part (below fn).
In my Emacs under Snow, the first delete key worked as "backward delete" and the second one as "forward delete". However under Lion, they are both "backward delete". On the other hand, they work as expected in other contexts (TextEditor, MS Word, Terminal, this text, etc).
I presume I have to insert some additional key configuration in my .emacs file, but I don't know what. Any hints?
My Emacs is Emacs 23.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.35).
Put this in your Emacs init file:
(global-set-key '[(kp-delete)] 'delete-char)
You might also need to add the following:
(global-set-key '[(delete)] 'delete-char)
(global-set-key '[(meta delete)] 'kill-word)
(global-set-key '[(meta kp-delete)] 'kill-word)
(global-set-key '[(control delete)] 'kill-word)
(global-set-key '[(control kp-delete)] 'kill-word)
Try M-x global-set-key RET key forward-delete -- if it works, try M-x repeat-complex-command but instead of reissuing the command, copy and paste it into your .emacs file.

Home / end control characters don't work in emacs under Mac OS X terminal

I'm running emacs from within a terminal window on Mac OS X. From the bash shell at the same terminal, I can use Shift + <Home> and Shift + <end> to go to the beginning and end of the line respectively. When I'm inside emacs, these don't work. I get the error:
M-[ h is undefined
and
M-[ f is undefined
Is this the fault of emacs or the terminal emulator? I can see how to change the control characters that the terminal sends, but as far as I can tell these are the right control characters to send (and it works outside of emacs).
Put these in your .emacs:
(global-set-key (kbd "M-[ h") 'beginning-of-line)
(global-set-key (kbd "M-[ f") 'end-of-line)
I'm pretty sure it's a problem with Terminal.app as I've got some similar problems with it. I don't know how to fix it, but for your particular problem, you can use C-a and C-e to go to the beginning and end of line, respectively.

Opening browser from emacs script?

I have the following emacs lisp snippet that will launch my browser from within emacs and open the specified pages. However when I run it as a script from a shell nothing happens. What more do I need to do? I tried dropping (interactive).
#!/usr/bin/emacs --script
(defun surf-news ()
(interactive)
(progn
(browse-url "http://news.ycombinator.com")
(browse-url "http://stackoverflow.com")
))
(surf-news)
A neat function I discovered yesterday is M-x webjump which has recently been added to emacs.
As Ryan Thompson mentioned above, you can use xdg-open in Emacs.
Change:
(browse-url "http://news.ycombinator.com")
(browse-url "http://stackoverflow.com")
To:
(browse-url-xdg-open "http://news.ycombinator.com")
(browse-url-xdg-open "http://stackoverflow.com"))
I found the answer on this site, at the bottom, where it describes a Mac OS pty bug. I had to add this line and a slight delay between urls.
(setq process-connection-type nil)
Here's the complete solution.
#!/usr/bin/emacs --script
(setq process-connection-type nil);; pty's broken on the Mac
(defun surf ()
(progn
(browse-url "http://news.ycombinator.com")
(sleep-for 0.5); We need a delay
(browse-url "http://stackoverflow.com")
))
;;
;; This is what's going on behind the scenes
;;(setq url "http://www.google.com")
;;(start-process (concat "open " url) nil "open" url)
(surf)
On linux, if you are in GNOME, KDE, or probably almost any graphical environment, there is probably a command called xdg-open. This command basically takes its argument and opens it with the desktop default application. It handles both files and URLs. So generally when I need to say "Open this in the default application," I use xdg-open.
I think Mac OS has a similar command just called open.

Resources