Just A Couple Of Emacs Keybindings

Every now and then I’ll type something in Emacs with a certain goal, just to find that I get something completely different from what I’ve intended.

When in org file, I tried to convert a list item to a sub-header. The keybinding to make this conversion is C-c *. But when I (thought I) typed it, instead of getting a sub-header, a new buffer opened at the bottom of the frame – a calculator:

emacs-calc-mode.png

Figure 1: Calc mode

view-lossage

I had no idea how did that happen, and luckily recalled a tip I got, on how to move back in time using the view-lossage command, which display last 300 input keystrokes. Doing so, I found that instead of C-c *, I typed C-x *.

So now I know (and hopefully remember) that:

  1. There’s a calculator1 in Emacs, bound to C-x *
  2. C-h l is a useful way to track back clumsy keystrokes

Footnotes:

1

Not that I had any doubts there is, just didn’t think to look for it just yet. There are so┬ámany other “to-learn” things on my list…

No Exuses To Blogging

No-Excuses-Guide-to-Blogging-Sacha_Chua-v2.jpg

Awhile back I wrote about my experience learning to touch type. While writing it, I found this related article by Sacha Chua, which helped me rationalize to myself why it is such an important skill to learn. I ended up spending almost an hour browsing Sacha’s site. The amount of resources, posts, tips and tutorials that she shares is humongous!

Continue reading No Exuses To Blogging

Get The Current File’s Path in Emacs

Here’s a small function I borrowed from this question on stack-overflow. It returns the full path of the file I currently edit in the buffer:

(defun show-file-name ()
  "Show the full path file name in the minibuffer."
  (interactive)
  (message (buffer-file-name))
  (kill-new (file-truename buffer-file-name))
)
(global-set-key "\C-cz" 'show-file-name)

You’ll note that this function is bind to C-c z. So when typing it, you should see the path showing in the minibuffer. As a bonus, it stores the path in the kill ring, so C-y (CMD-v works as well on my mac) will paste the value.

I Can Live Without A Mac

Couple of months ago I listened to an episode of the technical difficulties podcast. In that episode Gabe Weatherhead (macdrifter.com) and Erik Hess hosted Dr. Drang and talked about the history of computing.

At some point 1, Gabe asked Dr. Drang what will he do if he can’t use Mac anymore. I don’t remember exactly what Dr. Drang’s answer was, but for awhile, the question kept echoing in my head. The thought about Mac not being around at some point was terrifying. I can’t go back to Windows. Never. And Linux never seen as an adventure I would have like taking.

Since then, though, much have changed.

At first, I was reintroduced to old fashion mechanical keyboards2. I bought a couple of them; one for home and one for my office. Suddenly, typing using my laptop’s keyboards 3 feels awkward and unsatisfying, and I now do it as little as possible.

Then, I took a step into the rabbit hole of Emacs, the timeless editor which is backed by a strong and lively community. It’s built around the keyboard, so I stopped using the Apple’s “magic” mouse as much as I used to.

Lastly, since Emacs requires tinkering with Mac’s internals, I got more comfortable and familiar with the terminal application, and with “UI-less” experiences.

Slowly, I’ve learned that my Mac is nothing but a strong processing device with a Retina display. With that realization, the idea of using a different platform, Windows is still not an option, stopped scaring me. To the contrary, that thought sparks curiosity in me…

While switching away is still not an option, I feel as if I was cured from Apple’s spell. I regained my ability to think critically on its products, and look more objectively on their alternatives.

It’s probably only me, though, cuddling with the past. Most users are probably welcoming the intersecting paths of Mac and iPad into a comp-let mongrel that will lock developers out of its internals. But now, at least, I’m less anxious about that trajectory. I will be able to find my way elsewhere…

Footnotes:

1

Here’s a link to this section in the podcast.

2

Typing on mechanical keyboard makes me not wanting to stop writing… I learned about them in yet another podcast – Cortex

3

Except for the Apple Extended Keyboard. I actually bought one, and am loving it.

Bunch of Emacs Tweaks

Comment\Uncomment a Line

Few useful commands for commenting\uncommenting lines in emacs. Taken from the Emacs tutorial. Sure, I can go back to the manual, but I want to ducument and keep them here, for quicker referrence.

M-; Insert or realign comment on current line; if the region is active, comment or uncomment the region instead (comment-dwim).

C-u M-; Kill comment on current line (comment-kill).

C-x ; Set comment column (comment-set-column).

C-M-j M-j Like RET followed by inserting and aligning a comment (comment-indent-new-line). See Multi-Line Comments.

M-x comment-region

Mastering this command takes me one step further into Emacs, as it used to be one of those funcionalities that keeped drawing me back to Sublime Text.

Quick reload of init.el file

I’m constantly customizing my emacs. I have an init.el file, but most of the configuration in a more literal way, in an org config file.

When I make changes to Emacs settings, I need to reload the init file activate the changes. So far, I typed C-x C-f to find the init file and then M-x [RET] eval-buffer to reload it. Repeating this flow hundreds of times became annoying.

A quick inquery in IRC, and now I know that I can call load-file and give it the name of the file I would like to load. Having a function to load a file, means that I can wrap it with my own function, and reload my init file with a customized keybind.

And with the help of this answer at stack-overflow, I came up with the following shortcut to reload my Emacs configuration:

(global-set-key (kbd "<f6>") (lambda() (interactive)(load-file "~/.emacs.d/init.el")))

New line bellow

I wondered if there’s a command to creat a new line bellow the line my point is on. Here’s what I found in superuser:

C-e C-m – go to the end of the line, create a new line and move the point to that line.

or

C-e C-j – same as the command above, only that the point will indent if neccessery.

There is also a keybind for creating a new line above the current line, and move the point to that line – C-a C-o.

Quick Open a specific file

Now days I start most of my writing in my draft file. I need a quick way to access this file, whether I’m in Emacs or any other application. I know Emacs has the concept of registers, which are special memory slots, that can be accessed with a command. Those registers can store any type of data, such as strings, integers, files and paths.

It’s time to learn how to work with them. When thinking about it, there are other files that I would have liked to access quicker, such as the init.el or config.org.

Google’s first search result was EmacsWiki. Again, it proved to be a great source of information, had I wanted to confuse myself. So I passed. The second result was from Emacs tutorial, which again proved to be clear, concise and informative.

Here are the commands for storing a filename in and loading it from a register:

(set-register r '(file . name))

For example,

(set-register ?r '(file . "~/Dropbox/Notes/posts/pages/posts_drafts.org"))

To load this file, I should type C-x r j r

In the code examples above, r is the name of the register. It can be replaced with any character.

And to see what’s stored in a specific register:

M-x view-register RET r

Again, r is the register I’m querying.

Change cases

Keybinding Action
M-l Convert following word to lower case (downcase-word).
M-u Convert following word to upper case (upcase-word).
M-c Capitalize the following word (capitalize-word).
C-x C-l Convert region to lower case (downcase-region).
C-x C-u Convert region to upper case (upcase-region).

Changing the default font in Emacs

Josh Stella wrote a delightful post about how he uses Emacs, not necessarily for development work. I found quite a few configuration tips, and already implemented few of them. One of those tweaks is using the Input font family. Visiting fontbureau made me want this font too!

I thought it will be as simple as copy-paste (I’m still not used to the appropriate kill-yank terminology) Josh’s configuration. It wasn’t – after reloading my init, the font didn’t pick up.

Few experimentations later, though, and it did work. First, I had to download and install the font in my mac, dahhh… Then, I had to modify the name of the font (Josh used InputSerif; I had to change it to Input). Here’s my configuration:

;; set up fonts for different OSes. OSX toggles to full screen.
(setq myfont "Input")
(cond
((string-equal system-name "ygilad.local")
 (set-face-attribute 'default nil :font myfont :height 144)
 (toggle-frame-fullscreen)))

Indeed, it looks beautiful. Here’s a screen grab of this post in Input: emacs_with_input_font.png

There’s still one problem – this modification to my config broke the org-reader plugin, and I can’t export my org files to Pelican. Sadly, I’ll have to resort to the default font (Menlo), until I figure out a fix.

Developer for a day

Earlier today I demoed a simple search page that I developed as part of a 24 hour hackathon here at Outbrain. What makes this search page unique is that it uses the Sphere platform1 to find content that isn’t only relevant to the search query, but also caters to the user’s interests. So, for example, if I search for “python”, I’ll get articles about python, the programming language. If my mom, on the other hand, do the same search, she’ll get articles like this one.

But what I developed isn’t the subject of this article (I might write about it in a separate post). What I do want to share is my experience at putting a developer hat for a day. As a product manager, who works very closely with engineers, this turned out to be an invaluable lesson.

I had no intention of building anything in that hackathon. My plan was to help and support the hacking teams2. A few minutes after the hackathon began, though, I thought it will be cool to build this search page. Unfortunately, at that point there were no developers around to whom I could pitch my idea. All of them were already assigned to other teams. But I got this impulse to build something, so I decided to challenge my lack of development skills, and form a team of one.

Well, saying that I’m not a developer isn’t entirely true. Afterall, I graduated as a software engineer, I understand technology, I can speak intelligently about software architecture and design patterns, and I was intimately involved in designing and building the Sphere platform. I even do some coding in my spare time3. But, I’ve never coded with a mission or under a strict deadline. So I thought, this would be an opportunity to get serious about coding. Indeed, serious I became, spending the next 24 hours (minus 6 hours of sleep) hacking my way toward something I’ll be proud to present.

Here’s how my next 24 hours have looked like:

10:20am – 20 minutes into the hackathon I’m having this idea, and yada yada yada I decide to code.

10:40am – I can see what the architecture of the solution should look like, and what APIs I’ll have to use. It’s going to be easier than I thought…

11:30am – Hitting a dead-end. My initial approach won’t work, because I don’t have access, from the environment I’m using, to the API I rely on. Need to think of a new direction.

12:00pm – Found a new direction. I’m not sure it’s the right one, and I’ll have to learn a framework that I didn’t use before (well, I didn’t use any framework before…), but I’m running out of options, so I’m taking the chance. I find a tutorial, and hope to learn everything I need to know before the hackathon is over.

1:30pm – Urr… this is the longest tutorial ever, and it’s not even related to the use-case I’m trying to solve.

2:15pm – OK, good news and bad news. The good – I finally finished the tutorial. The bad – I still don’t have a clue how to work with this framework to build what I have in mind.

3:00pm – 5 hours have passed, and I still have nothing. What’s worse – people around me have high expectations of me. I have no idea why, but I know I’m going to disappoint them. I’m loosing my patience, and even the quietest chatter in the room distracts me. I’m agitated, and my fuse shortens by the minute. I hope no one will talk to me. I need silence. Maybe I should put headphones on, or go to a secluded room…

4:10pm – Half a day went by, and I’m farther away from my initial idea than I was 6 hours ago. Maybe some coffee and fresh air will help me regain energy and spirit.

4:20 – I don’t know if it’s the coffee or the time off the computer, but I’m thinking more clearly now. In fact, I have an idea. I need to run back to the office.

5:30pm – I have a working solution! It looks awful, but works…. all I have to do now is take care of the front-end. Easy breezy.

9:15pmI hate CSS. I’m about to loose my mind. I must eat.

10:00pm – I need to have some sleep. I’m sure things will look easier tomorrow.

8:10am – CSS is still CSS.

9:55am – 5 minutes to my demo. With some help, hand-holding and a lot of duct-tape my work is somewhat presentable. I hope people see the potential, and won’t get caught-up with the UI.

10:10am – I’ve just presented my thing. I feel awesome. I built something and got people’s applause.

Now, what did I learn from this schizophrenic experience:

I don’t want to be a developer.

Yeah, as simple as that. I’ll keep doing it as a hobby, but I’ll never do it professionally. I mean, I love the problem solving, and creating something with my own hands is amazing. But, getting sucked into the smallest of details, spending hours trying to figure out what I did wrong, only to find a missing ;, and wasting tons of time on configuration before I can do anything, make me go nuts…

OK, so as this door is now closed, what did I learn about coding that will help me understand engineers better?

Coding requires focus

Soooo much of it. Even the slightest distraction can throw your thought process miles away. It then takes a lot of time to regain your thoughts, and get back to where you were before. I now understand even better Joel’s “Human Task Switches Considered Harmful” post.

Tools are important

Don’t ask why, but I’ve started to learn Emacs recently, and so far I love it. But, Emacs isn’t the most dummy proof app out there. Here’s a funny chart, that actually stops being that funny when you start learning Emacs:

3251176498_c3485a55fb.jpg

So, since I’m somewhere at the bottom of the learning curve still, I thought this hackathon would be an opportunity to learn the tool better and faster. But as soon as time started to press on me, and at the first instance when I didn’t know how to do something in Emacs that was trivial in other tools, I closed it and opened the other tools I feel comfortable with (Sublime and CodeRunner).

Knowing what’s the expected outcome is key

Having a clear idea of what my end product should do, and to some extent – how it should look like, was crucial. I had so much to learn in a very short time, but knowing what my end goal was kept me focused. It also helped me stay on course and learn only what was relevant to getting my project done (otherwise I have the tendency to drift away quickly).

And lastly, I experienced first hand how deadlines and quality [don’t] play [well] together:

Code becomes crappier as deadline approaches

You might think that my experience isn’t a good enough example, and I might agree. I’m just saying that now I can better relate to this #NoDeadlines trend.

If you’re involved, at any capacity, in product development, you already know this lesson, because crappy code keeps bouncing back at you and eats the time and resources you need in order to build new stuff. Don’t be fooled by fancy terms, such as “tech-debt” and “refactoring” – these are politically correct ways to refer to crappy code. And the stricter the deadlines, the more of it you’ll get4.

So with that, I’ll put my developer hat down. It’s too big for me…

Footnotes:

1

Check out the platform here, and leave a comment if you’re interested to learn more.

2

There were 19 internal and external teams hacking.

3

In case you’re interested, here’s my GitHub account.

4

No, it doesn’t mean I won’t ask for time-lines and set deadlines in the future…

Swapping Buffers in Emacs

It took me awhile to find a way to swap the position of two buffers in emacs. Yes, there is a description in emacs wiki, and the code bellow is actually taken from there, but it’s not that easy to find through the tons of irrelevant information arround it.

So if you’re looking to simply get the right buffer show on the right, and vice versa, here’s what you should add to your init file:

(defun transpose-buffers (arg)
      "Transpose the buffers shown in two windows."
      (interactive "p")
      (let ((selector (if (>= arg 0) 'next-window 'previous-window)))

          (let ((this-win (window-buffer))
                (next-win (window-buffer (funcall selector))))
            (set-window-buffer (selected-window) next-win)
            (set-window-buffer (funcall selector) this-win)
            (select-window (funcall selector)))
          (setq arg (if (plusp arg) (1- arg) (1+ arg))))))

I have no idea what this code means1, but it does what I expected it to do. I also didn’t create a keybinding for it, but you can if you would like to. Here’s how to bind it to, say, f8:

(global-set-key [f8] 'transpose-buffers)

Footnotes:

1

Learning elisp is on my todo list…