4

I am new to emacs and emacs-lisp so i don't have any idea how to write extensions to emacs but i came accross awesome extensions like 'pretty-lambdas' which swaps

(lambda (x) (+ x x)) ---->  (λ (x) (+ x x))

In emacs-lisp mode. I was able to make it work for clojure such that i get :

(fn [x] (+ x x)) ----->     (λ [x] (+ x x))

I would like to do the same for javascript, such that In javascript mode instead of

function(x){}

I get . f(x) {}

how do i achieve this? Thanks.

1 Answer 1

6

I've never used pretty lambdas, but there are a number of other libraries that can do this.

In Emacs 24.4, there is a new built-in mode called prettify-symbols-mode. You should be able to do something like

(add-hook 'js-mode-hook
          (lambda ()
            (push '("function" . ?ƒ) prettify-symbols-alist)
            (prettify-symbols-mode)))

Thanks to Stefan, who points out in the comments that there is also global-prettify-symbols-mode, in case you want to enable this mode all the time.

Alternatively, you could use pretty-mode, pretty-symbols or purty-mode, all available in MELPA, and pretty-mode is also in MELPA stable.

Something like this should work for purty-mode:

(purty-add-pair '("\\(\\bfunction\\b\\)" . "ƒ"))
(add-hook 'js-mode-hook #'purty-mode)

Note that purty-mode only seems to have a single replacement table, so the functionƒ replacement will affect all buffers where purty-mode is active.

Sign up to request clarification or add additional context in comments.

2 Comments

when I am editing javascript files my emacs shows, the mode as Javascript not js. So my purty-add-pair seems to not work. so I still don't have "function" . "ƒ" working. However, I like purty.
Regarding prettify-symbols-mode, it is worth noting that there is also a global-prettify-symbols-mode you can activate once and for all in your ~/.emacs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.