
My Emacs Productivity Tricks/Hacks - mycpuorg
http://www.mycpu.org/emacs-productivity-setup/
======
numlocked
The one thing I consistently do in emacs that shocks my coworkers -- is
running this (custom) function, that I call "arrayify":

    
    
      (defun arrayify (start end quote)
        "Turn strings on newlines into a QUOTEd, comma-separated one-liner."
        (interactive "r\nMQuote: ")
        (let ((insertion
               (mapconcat
                (lambda (x) (format "%s%s%s" quote x quote))
                (split-string (buffer-substring start end)) ", ")))
          (delete-region start end)
          (insert insertion)))
    

What it does: Given a list of strings separated by newlines (commonly,
something copy-pasted from a table) like:

    
    
      josh
      sam
      jed
      C.J.
      toby
    

You select the text, and run "arrayify". You can optionally provide a quote
character and, in about half a second, get to:

    
    
      "josh", "jed", "sam", "C.J.", "toby"
      

This is incredibly useful for taking lists of IDs, or email addresses, or
whatever, and transforming them for pasting into documents, or emails, or "in"
clauses in SQL, etc.

It is _unbelievably_ useful.

~~~
Skunkleton
In vim:

    
    
        :'<,'>!awk '{printf("\"\%s\", ",$0)}'
    

or to do the whole file:

    
    
        :%!awk '{printf("\"\%s\", ",$0)}'
    
    

What I find nice about this approach is that I can use my knowledge of awk in
other non-vim contexts. Similarly I can use my knowledge of other text
processing commands to do things in vim, like reformat text into columns
(:%!column -t) for example.

Of course you could use vim commands to do the same thing:

    
    
       0<ctrl-v>GI"<esc>$<ctrl-v>GA", <esc>VGJ
    

Which would have the benefit of making it more customizable at the expense
of....well you can see what you have to type.

~~~
patrec
You can use your knowledge of emacs in other non-emacs contexts as well! Elisp
is a reasonably capable, if sprawling, programming language and comes with a
lot of useful text and code munging stuff, as well as with cross-platform
networking and filesystem support, and more esoteric stuff like calendar and
symbolic/aribtrary precision math. And that's before installing any extra
packages. I've written shebang scripts with Emacs (rather than say python or
bash) on multiple occasions. Prior to windows growing linux support it
probably also used to be one of the easier ways to write some unixy-feeling
but cross-platform scripts; you can even craft a "shebang line" that works for
both windows and unix. Having said that, due to general eco-systems advances,
writing scripts in emacs has probably become less attractive than it used to
be.

~~~
Skunkleton
I really dig that about emacs. Having a programming language that is geared
toward text processing is super useful. Doubly so when your editor is
extensible in the same language.

------
molteanu
This is mostly a listing of packages and very known ones at that.

One of the best features I like is undoing regions. That is, if you know the
approximate location of the text that you want to undo, you just select a
region that contains that location and call undo. It can be used as a quick
git alternative. Say you modify the buffer all over the place. If you want to
undo some text that you've modified an hour ago but you did other
modifications to the buffer in countless other places, you can undo just that
region and you don't have to go back throught the whole undo tree.

Another one that I've currently discoverd and I'm very excited about is
yasnippet (the modern variant of abbrev). You can define a string that stands
for an abbreviation for an expansion. Everytime you type that string, it is
replaced by that expansion. The nice thing about yasnippet compared to abbrev
is that, among others, you can specify where to leave the cursor after the
expansion. One example: "lt" expands to (let ((_))), where _ is the cursor
position after the expansion. This also saves typing of all the parantheses.
Or "mcar" expands to (mapcar (lambda (_))). You can expand anything to
anything, in short. With time, you can type less and faster.

~~~
smabie
Yasnippet is amazing. Being able to tab through different fields is a game
changer. You could make a snippet for every lisp function and never have to
enter a paren ever again! The only probablem is that yasnippet can’t handle
multiple nested expansions: it only keeps track of the current expansion.

~~~
celeritascelery
If you set `yas-triggers-in-field` it will work with multiple nested
expansions.

------
jf
I've used Emacs for decades and still feel like I'm barely scratching the
surface.

What would really help me with articles like this one is more narrative on
_why_ these particular modes are so useful. For example, it took me a long
time to learn org-mode because I just didn't know what to do with it! I feel
like Helm is similar, I've tried it briefly, but I'm not sure if or how it
would fit into my workflow.

~~~
ashton314
I completely agree. I used vanilla Emacs for about a decade before finding
list-packages. (I know, I know. I was really behind.) But stuff like org-mode
never clicked for me. I had to sit down with the manual and read it to finally
get a handle on what it is.

I'm currently drafting an essay on what makes org-mode compelling, rather than
how to use it. I think I'll do the same for other tools I use.

~~~
cannam
> I completely agree. I used vanilla Emacs for about a decade before finding
> list-packages

I think it helps to think of Emacs as just another platform - and a nice
stable one. You may or may not use, or know about, all the "applications"
available for it. New stuff comes along that you haven't heard about; you
might occasionally want to review things; and it doesn't really matter if you
don't.

I've been using it for the usual handful of decades and have gone through
phases of using one exciting new thing or another, but I've dropped most of
them - I no longer run builds from Emacs (I do recompilation in the background
in a separate terminal), nor read mail or use version control software from
within it. I don't use evil and my colour scheme is mostly black-on-white. My
.emacs has ended up almost back where it was 25 years ago. (In fact it still
has references to Epoch and Lucid Emacs in a couple of comments.)

The two additions in the past couple of decades that have made a big
difference to me are Tramp (a nice upgrade from ange-ftp) and the ability to
set non-integer line spacings.

~~~
nothrabannosir
As an Emacs neophyte and vi vicenarian, I'd love to know why you ended up
ditching evil alongside the other goods. I see the ortholinearity of e-mail
and vcs in an editor, but evil seemed to me living within the editor, rather
than atop it. Appropriately: a minor mode rather than a major. Like, say,
editorcfg. Was this not your experience?

~~~
cannam
Oh, evil wasn't something I ditched - I never used it. I just mentioned it
because it's so often cited as a vital thing. (ooh, nice potential pun there
in vi-tal... hm) Same goes for the colour scheme. It's the higher-level
applications that have come and gone. Sorry to be unclear.

------
why-el
Another beautifully designed piece of Emacs software is magit, where the
defaults are extremely sane. The package is self-documented and you can learn
it all by typing ? anytime. Being able to git log the file you are in is also
a huge plus.

~~~
ninjha01
Magit made me a better programmer. Learning `git` proper would probably be as
good (and certainly get more street cred), but magit was more accessible and
more fun to use since I was already emacs-proficient.

~~~
celeritascelery
The thing I love about magit is that it taught me git. You can hit $ and see
exactly what I’d bring run and many of the options are in the pop up.

------
aptidude187
I used to think that Emacs is some weird editor with absurd keybindings, until
I learned lisp and understood what makes it so special. Now Emacs
(Spacemacs[0]) is my main driver.

[0] [https://www.spacemacs.org](https://www.spacemacs.org)

~~~
quickpost
How did learning lisp help you understand the power of Emacs? Just starting
down the Spacemacs road myself (from Sublime Text), and curious to hear more
about your experience!

~~~
molteanu
In short, you can add new functionality to Emacs yourself.

And just like in the world of Lisp, after you add a new functionality, it
becomes indistinguishable from what you already have. That is, it looks like
it has been there all along, from the start. It doesn't look like a 3rd party
plugin that you need to take special precautions to install. It just blends in
nicely, and naturally.

Or, to put it another way, if you learn (Emacs) Lisp and you start to tweak
Emacs to your own liking, you can transform Emacs into your own personal tool,
adjusted exactly the way you like it, no questions asked.

~~~
aptidude187
Thank you molteanu for chiming in. I would also add, that everything has the
exact same interface, meaning I can access every tool in the same way, the
consistency is unparalleled. For example: In VSC when I want to use the shell,
I suddenly lose my vim bindings and thus drop down to the casual user mode. In
Emacs I can treat the shell like any other buffer, apply all my tools and
knowledge, so it's nothing foreign or something that wants to be treated
differently. For instance similar point in VSC regarding the file explorer, it
forces you to abandon the keyboard homerow, I can't use my vim bindings from
the plugin, because the plugin is something 'foreign'. Sure you can memorise
shortcuts, but it's not the same as using your vim bindings for everything,
always.

~~~
molteanu
Yes, indeed. All the tools, all the time, always available. Something like
writing a macro in Lisp. All the language is there at your disposal and it's
nothing compared with C macros where you have huge limitations, it being
actually a different language than C. I feel your pain with the VSC example.

"Consistency" is a good word. The result, for me at least, is that I can
actually think about what I want to do with the tool and not how to use it. It
gets out of the way, it does not try to fight me.

I think maybe that's one interpretation of the saying "Emacs is an operating
system".

------
Koshkin
Emacs is the spaceship of typewriters. Whenever I try using it, I end up
feeling like a centipede who began thinking about which leg it should move
first.

------
anarchyrucks
I installed Doom[0] and called it a day!

[0] [https://github.com/hlissner/doom-emacs](https://github.com/hlissner/doom-
emacs)

~~~
komali2
Looks interesting, but the main page claims it has support for a lot of
languages, but according to this module page it only supports javascript,
typescript, and html?

[https://github.com/hlissner/doom-
emacs/blob/develop/docs/ind...](https://github.com/hlissner/doom-
emacs/blob/develop/docs/index.org#lang)

~~~
NotATroll
The TODO is regarding documentation iirc.

All the languages supported are in the init.example.el file seen here:
[https://github.com/hlissner/doom-
emacs/blob/develop/init.exa...](https://github.com/hlissner/doom-
emacs/blob/develop/init.example.el#L107-L165)

Uncomment them, run doom refresh. Check their module under
[https://github.com/hlissner/doom-
emacs/tree/develop/modules/...](https://github.com/hlissner/doom-
emacs/tree/develop/modules/lang) for any external requirements that they need
or flags that they support.

~~~
snicker7
The comments are hilarious:

;;hy ; readability of scheme w/ speed of python

;;rust ; Fe2O3.unwrap().unwrap().unwrap().unwrap()

;;javascript ; all(hope(abandon(ye(who(enter(here))))))

------
098799
Helm-projectile rant is well deserved. I'm often ashamed looking at my
colleagues trying to find some file with a mouse in their big fat IDEs.

~~~
elcomet
All decent IDEs have a file search command.

~~~
098799
Somehow my colleagues who use VS Code or JetBrains IDEs don't manage to learn
about those features while all of my vim-using colleagues somehow do. (Haven't
worked with anyone using Emacs yet)

~~~
umanwizard
I use CLion almost exclusively and I use shift-shift to find things so often
that I have basically no idea what file anything is in.

Is there anything free that even begins to approach the quality and accuracy
of the JetBrains Rust analysis plugin? If so, I’d love to know about it and
start using vim.

~~~
stjohnswarts
I'm sure there has to be a vim plugin for rls

~~~
umanwizard
Yes, there is. But RLS is much worse than the CLion Rust plugin, in my
experience.

------
mapgrep
> All the packages were installed from Emacs package Manager. By running, `M-x
> list-packages` This should bring up a list of packages available in MELPA.
> Now, don’t worry if you don’t know what MELPA is, just think of it as a
> repository of all packages, as in, analogous to the Debian Package Repo in
> Debian/Ubuntu distros.

Article seems to confuse MELPA with ELPA. MELPA is something you have to add
via your init file. Not terribly hard to do, but just FYI for anyone who is
actually using these tips from scratch.

------
jhoechtl
I use Emacs every day. But only for org-mode which is fantastic.

Besides that I really really tried hard to love Emacs as my go-to editor.
However I found no alternative for

decent CSV file support (like
[https://github.com/chrisbra/csv.vim](https://github.com/chrisbra/csv.vim)
simply great!)

Inspecting large XML files with syntax highlighting and folding. VIM is also
not the fastest here but at least 10 times faster (emacs SGML mode is a joke
for xml files and forcing xhtml mode of web-mode is also not that great)

Maybe all my emacs doing was just wrong.

~~~
smabie
csv-mode does exactly what you want.

~~~
jhoechtl
Filter rows based on a value?

Deleting a Row?

Easily navigating from the nth element in a CSV header to the nth element in
data row 1000?

~~~
smabie
Sure, just use SES and import the CSV. It’s no excel, but it does what you’re
talking about.

------
kwindla
also tramp, for editing files on any machine you can ssh into ...

[https://www.emacswiki.org/emacs/TrampMode](https://www.emacswiki.org/emacs/TrampMode)

~~~
mycpuorg
<Same response as org-mode> A resounding YES! But Org-Mode and TRAMP are so
well written about that although I have "special" setup for these in my config
I felt I could not do more justice than other expert write-ups. Maybe, I
should add them in a separate entry.

------
myrloc
use-package would be an improvement upon most of the code snippets in the blog
post. Loading packages without it these days feels like a sin.

~~~
snicker7
I would also recommend straight.el or perhaps even guix.

~~~
hartzell
I'll give another vote for straight.el and its use-package integration.

------
cmrdporcupine
I'm ashamed to say I've been using Emacs since 1992 but I have never been able
to impress into my brain anything other than the minimal set of keystrokes to
do basic editing. I feel like I need to be trapped on an island where the only
thing I could do was learn emacs for a bit. There's so much there to dig into.

~~~
pmoriarty
This is why I regularly create hydras for every emacs mode that I use. That
way I don't have to remember any extra keystrokes, as the hydras will remind
me.

~~~
cannam
What's a hydra? (in this context)

~~~
NateEag
A cluster of Emacs keybindings that share a common prefix, implemented using
the hydra library:

[https://github.com/abo-abo/hydra](https://github.com/abo-abo/hydra)

~~~
cannam
So it's sort of like a mode switch? You give the prefix, and then you can give
it a whole series of commands, based on a crib sheet that it shows you, that
have that common prefix - without having to retype the prefix - before passing
a different key to drop out of the mode again?

That does sound potentially handy, if I have it right.

~~~
pmoriarty
Yes, though I'd describe it more as a fully-customizable popup menu that
appears when you type a certain keystroke. Once the menu appears you can type
more keystrokes to perform an action or bring up another custom menu.

Also, it's possible to configure it so that there's a delay between the time
you type a keystroke and the menu appears, so that if you remember the key-
sequence you need to type to perform a certain action and you type it fast
enough, then the menu won't pop-up, but if you forget the full sequence then
you can type just the first keystroke and then wait a bit and the menu will
pop up and remind you of what needs to be typed to execute the action you
want.

It's super handy, and is one of my all-time-favorite emacs packages.

------
arminiusreturns
One of my favorites is creating a separate backup dir where all backup "~"
files are created and versioned and rotated in and out etc at specific
intervals, so I no longer get the random files in the dir I'm working in but
can still go find backups if I mess something up.

    
    
      (setq
       backup-by-copying t      
       backup-directory-alist
        '((".*" . "~/.emacs.d/bu/"))
       delete-old-versions t
       kept-new-versions 6
       kept-old-versions 2
       version-control t)

~~~
yoloClin
I already use this in my _shrc files:

    
    
        alias ls='ls --hide="lost+found" --hide="#*" --hide="*~" --hide="*.pyc" --hide="*.egg-info" --hide="__pycache__" --color=auto'
    

But I'll be adding your item into my config for the rotated versioning, I
wasn't aware that emacs does that but I assume it'll save me anything from few
hours to a few weeks yearly. Thanks!

~~~
arminiusreturns
If you like it check out this and see if you like anything else:
[https://sanemacs.com/sanemacs](https://sanemacs.com/sanemacs)

or [https://www.sandeepnambiar.com/my-minimal-emacs-
setup/](https://www.sandeepnambiar.com/my-minimal-emacs-setup/)

------
bitforger
I'm extremely surprised to not see Spacemacs mentioned in the article or the
comments. Spacemacs is, IMHO, the most sane and useful default configuration
of Emacs. It is friendly to people switching from vim and includes all the
packages listed in the article by default (as well as many more).

It has been my go-to editor for many years now; I highly recommend it.

------
arminiusreturns
One of my favorites is creating a seperate backup dir where all backup "~"
files are created and versioned and rotated in and out etc at specific
intervals, so I no longer get the random files in the dir I'm working in but
can still go find backups if I mess something up.

------
kleer001
org f-ing mode

edit: actual package is called "org-mode"

~~~
kickingvegas
org-mode for notetaking/journaling/organizing and writing documentation with
org-babel for interactive (literate) programming and making diagrams (gnuplot,
matplotlib, graphviz, mscgen, plantuml).

Can’t say this for many tools but using org-mode has been life changing for
me.

~~~
kleer001
I'm writing a novel with org-mode and it's the most transparent writing tool
I've ever used, and so flexible too.

------
warp
What is the emacs equivalent of package.json?

(whenever I git clone my dotfiles on a new machine I usually have to comment
out a bunch of stuff in my ~/.emacs because it's referencing packages which
aren't installed, and I forgot where I installed them from, what version,
etc...)

~~~
robto
I use straight.el[0] for a reproducible package list. It basically clones the
packages and stores a map of package->commit, so you can freeze your packages
and reproduce them on another machine.

You could also use the Guix package manager[1] or the Nix package manager[2]
to achieve a similar effect.

Reproducible software is much more stable!

[0][https://github.com/raxod502/straight.el](https://github.com/raxod502/straight.el)

[1][https://guix.gnu.org/#guix-in-other-distros](https://guix.gnu.org/#guix-
in-other-distros)

[2][https://nixos.org/nix/](https://nixos.org/nix/)

~~~
warp
Awesome, thanks!

------
rb808
It seems a lot of people have used emacs for years and love it. I never saw
the point of learning as it seemed cryptic. In 2020 is it worth putting the
effort to learn now? I figure with vim and IDEs I dont really need it.

~~~
fredsir
I switched to emacs in 2019 after using primarily vim for 12 years, and I
think it's more than worth it. It's not an editor, it's not an IDE, it is kind
of like an operating system where you can have a text editor, debugger, notes,
mail, whatever you want. If you dig that, you'll love emacs. It's extensible
in a way you've probably not experienced anywhere else, so if you like honing
your tools so they fit the way you want to work, emacs is for you. I've
actually come to long for an operating system that gives me the same
"interactivity" as emacs does.

Of course, I edit text in emacs with evil: [https://github.com/emacs-
evil/evil](https://github.com/emacs-evil/evil)

~~~
Koshkin
> _not an editor_

It's "editing macros," so yes, it's an editor first and foremost.

> _not an IDE_

It's integrated; mostly used by developers; it's an environment.

~~~
smabie
I mean, I play NetHack in it. I don’t think any IDE has that kind of
flexibility.

~~~
Koshkin
[http://eclipse-games.sourceforge.net/](http://eclipse-games.sourceforge.net/)

~~~
anthk
That's nothing. You can do, in Emacs:

\- Run Nethack

\- Chat over IRC. Double combo with Bitlbee

\- Chat over Slack/$whatever proto is designed in Elisp

\- Write Interactive Fiction

\- _Emulate_ Interactive fiction with a Z machine in Elisp

\- Write and read emails/news with GNUs

\- Listen to music with EMMS

\- M-x doctor because is too complex and you need to talk to someone

\- Comment on HN with eww

\- Do literate programming

\- Do math, and with Maxima/R, algebra/statistics/whatever

\- Use GIT with Magit

\- org-mode. With this you could write three ebooks larger than the Java
specs.

\- org-babel. I forgot it. This is damn magic. Programming as if you were in a
word processor, going back and forth, easily, showing up your results inline.

~~~
smabie
I particularly like calc mode. Top notch programmable rpn calculator with
basic CAS support. Good for most algebraic transformations plus derivatives
and integrals. It also supports graphing!

Also you missed that you can run WebKit inside of emacs. Also you embed X apps
in emacs and use it as a window manager.

~~~
Koshkin
> _embed X apps in emacs_

Not in the console mode, you can't.

------
jaequery
As a long time emacs user, I'm sad to say but I've become pretty content with
Visual Code and I don't think I'm going back. Ofcourse, took a lot of time
customizing Code retaining all my emacs keybindings, etc.

~~~
confounded
What was it that made you move, and what is it that will make you stay?

------
m463
a few I use:

    
    
      (defun shift-left  (beg end) (interactive "r") (shift-region beg end -1))
      (defun shift-right (beg end) (interactive "r") (shift-region beg end 1))
    
      (global-set-key [A-M-right] 'shift-right) ;; >> shift every line of region
      (global-set-key [A-M-left]  'shift-left)  ;; << (and region remains highlighted)
    
      (global-set-key "\M-+" 'text-scale-increase)
      (global-set-key "\M--" 'text-scale-decrease)
      (global-set-key "\M-=" (lambda () (interactive) (text-scale-set 0)))

------
hellofunk
the others I cannot live without:

neo tree

winner mode

god mode (I use instead of evil mode)

avy (visually jump to any char on the screen)

undo-tree (really neat, though I don't use it as much any more)

~~~
tptacek
winner-mode was new to me. Thanks! The most annoying thing about Emacs for me
is random windows popping up; I'm a "separate frames" person (the OS window
manager does fine for me, I don't need much help from Emacs), and anything
that streamlines windows is great.

~~~
hellofunk
then here's another just for you!

    
    
      ;;from https://github.com/m2ym/popwin-el
      ;; prevents annoying popup buffers/windows
      (require 'popwin)
      (popwin-mode 1)

------
rntksi
If the author, @mycpuorg, is reading this comment: please, PLEASE, do write
about your mu4e configuration.

~~~
mycpuorg
@rntski, I will be happy to.

I feel I get a bigger bang for buck for Work Email on Outlook Exchange since
Email on mu4e makes me less distracted.

~~~
jimmyvalmer
This sentence is either missing a negation or correlates "bang" with
"distraction", which is wrong since "bang" is positive and "distraction"
negative.

~~~
mycpuorg
You are right, scratch that and I will try this again. I meant to say:

I have configured and used mu4e on a GMail account and another account hosted
on Microsoft Exchange Server (MSE).

The Linux based email client I had tried before mu4e for MSE account are: *
WebMail (Outlook's less capable younger cousin on a browser) * a slew of Linux
UI email clients like Thunderbird etc.

I disliked all of them equally. However, using mu4e for emails with
Outlook/MSE account makes doing emails less painful and less distracting.
However, using mu4e as a mail client for Google accounts may not be preferable
if you have bought heavily into the Google ecosystem. I have had trouble with
things like Google API authentication on Emacs.

------
antoineMoPa
My favorite these days is a package called find-file-in-project. Ideal for
navigating large projects.

~~~
agumonkey
related, projectile-mode gives a bunch of similar niceties

------
tomThom
I would be productive if there were an emacs that weren't a parenthesis hell
and slow as molasses. I hope there's is a lumacs

------
cletus
Whenever I hear about or see the .vimrc and .emacs files people create,
maintain and constantly modify to me this just looks like the perfect example
of a sunk cost fallacy.

IDEs are usually dismissed by many such people since you can (allegedly) do
the same in vim/emacs but to me this seemed crazy as I'd see people spend
hours configuring plugins, writing Lisp and whatever to save them on some task
they'd get for free with a right click in IntelliJ.

I really do think people completely underestimate how much time they have and
will invest in such endeavours. I mean if this makes you happy or you like
this tinkering, by all means be my guest, but I'll take a hard pass.

~~~
skummetmaelk
Would you rather drive a nice car that needs some loving care and maintenance
once in a while or an old banger that just gets the job done? It's the same
sort of prospect.

Some people like to work in a nice environment where they are able to remove
all the little frustrations that less customizable tools often force upon you.
When emacs annoys you at least you can fix it if it is annoying enough for you
to want to do so.

In any case neither choice is wrong or right. Do whatever floats your boat.

