31 January 2016

There’s a lot Emacs literature and config files floating on the internet. I’ll most likely have my own requirement/taste. I’ll focus on the tiny details that work for me. I’ll improve over my own copied-edited-hacked-together workflow.

emacsclient

Emacs startup can take a few seconds. If that’s unbearable for quick editing, then have an emacs server running.

$ emacs --daemon

Whenever required, use emacsclient to connect to it. emacsclient expects a file name argument to open.

$ emacsclient foo.txt

If I don’t have a file to open, then I use the -nw option.

$ emacsclient -nw

I usually alias the above to em.

alias em="emacsclient -nw"

To kill the Emacs server I can run M-x kill-emacs from an emacsclient. The Emacs wiki suggests the following:

$ emacsclient -e '(kill-emacs)'

I will alias this to whatever I see fit. But I’ve found shutting down emacs is something I’ve rarely done though. So I’ll just leave it for later.

Backgrounding emacs

There were times when I didn’t have an emacs server running. If I had to quickly get back to the shell, I would press ctrl-z to background emacs. And then bring it back with the fg shell command.

Fuzzy file search with helm

Helm’s introduction on github says this:

Helm is incremental completion and selection narrowing framework for Emacs

AFAIK I have helm in my emacs config for a couple years now. I haven’t yet figured out what that means. But it’s useful because it gave me the fuzzy search I found handy in vim (ctrl+p), SublimeText & Atom.

(global-set-key (kbd "C-x f") 'helm-find)

Now using C-x f would invoke helm’s fuzzy file search, which is awesome.

projectile & helm-projectile

projectile is a project management package for Emacs. helm-projectile provides helm bindings for projectile. I found very good use for it a few hours before this writing.

I sometimes have 3 repositories I switch between frequently. Projectile makes that seamless.

;; turns on projectile mode by default for all file types
(projectile-global-mode)

;; asks for file to open when project is switched
(setq projectile-switch-project-action 'helm-projectile-find-file)

;; turns on helm bindings for projectile
(helm-projectile-on)

Here’s a workflow:

  • Edit app/app.js in the client repository
  • Open files in the project with M-x helm-projectile-find-file
  • To edit a file in server repository, I run M-x helm-projectile-switch-project and choose the file

This works because projectile recognizes .git directories to identify a project’s root folder.

helm-projectile also allows grepping the project with M-x helm-projectile-grep. Grepping is something that I haven’t yet configured properly. It doesn’t seem to respect gitignore irrespective of my config to do so. Saving it for the future.

;; ask projectile to use git grep
;; so that files in gitignore wont be grepped
(setq projectile-use-git-grep 1)

Emacs swiss knife

It’s going to be daunting to invoke those helm-projectile commands frequently with M-x. I know I’ll come across such commands everywhere.

M-x describe-function shows keystroke bound for a function (along with more details). I’m going to be meta and find the key shortcut for M-x describe-function. I can run the command and enter describe-function as the function name.

I find C-h f is bound to describe-function. That C-h f is my swiss knife.

Using helm-projectile shortcuts

I use my new-found knowledge of C-h f and find out the following shortcuts.

  • C-c pp for helm-projectile-switch-project
  • C-c pf for helm-projectile-find-file
  • C-c psg for helm-projectile-grep

pp is projectile project. pf is projectile find. psg is the name of the college a friend studied at.

Publishing this blog post

All this while I’ve been typing this blog post in a file, in an unrelated project directory. Wasn’t confident if this will actually turn into a post or few-minutes rambling that I will rm soon after.

So now I attempt to copy this by starting to mark the entire buffer line by line.

mark the entire buffer. Oh. mark- something maybe?
C-h f, then mark- then TAB.
I find mark-whole-buffer in the suggestions. C-x h to select the entire thing and off I go.