Category Archives: emacs

Using emmet-mode in Emacs for HTML Editing

When in Windows, I have a bad habit of using Sublime Text 2 instead of Emacs — I know, I’m ashamed of myself. It has some very useful features that I’ve really gotten used to using. Among them is the HTML dom shorthand… an example, if I type:

hr.margin-none <TAB>

Then it automatically expands to:

<hr class="margin-none"></hr>

Very useful when typing the code out yourself and not using a GUI editor… so I wanted to see if this functionality was available in Emacs. The good folks over at StackOverflow were quick to answer my question, and now I’ve been introduced to emmet-mode. It works the same, except by default the key binding to cause the expand is C-j, instead of TAB in Sublime Text. I think making a local emmet-mode keybinding may take care of this, but I’ve not played around with it enough yet. You can get emmet-mode from marmalade or MELPA repositories.

 

Using VI Inside of Emacs with ansi-term

As I’m still in the transition to use Emacs more often, I find myself often wanting to do a quick-and-dirty edit in vi. Rather than open another terminal (which can be disruptive if I have a tiling window manager with a particular layout configured), I like to have an ansi-term shell open in another Emacs frame. I usually use C-x 2 to split the window vertically, then C-x o to switch to the second frame, and M-x ansi-term RET to start an ansi-term shell. It will prompt for the shell to use, defaulting to the current shell of that user, in my case it will be /bin/bash or /bin/zsh depending on which system I’m using.

One great thing about ansi-term is that aside from the control/meta keystrokes, it’s a true ANSI-compliant terminal shell. You can still use C-x C-f and other key sequences (which in that case will open a new file in a new buffer) but you can also use C-c to cancel the current command, interrupt the running process, etc., except you have to hit it twice to tell ansi-term that you wanted to pass that control-c into the shell, not for Emacs itself to process. One of the only differences I’ve found, and it suits my needs just fine.

Now, for the quick-and-dirty edits — using vi or vim works perfectly fine within the Emacs ansi-term. The only issue I’ve found is that if I’m inside the file long enough, I often forget that I’m in VI and I will try to save the file with C-x C-s, and VI doesn’t really like that very much. Another thing of note — often I’m sudo’ed to root in the ansi-term window for various reasons, and vi’ing a file then will edit it as root, but using C-x C-f to open the file inside a new Emacs buffer will open it as the user ID that initiated the Emacs client (as I run Emacs in daemon mode almost exclusively), so I won’t have permissions to edit system files natively. I could use tramp/sudo, but sometimes it’s just faster to edit with VI. I’ve found this handy for multiple occasions so far.

One thing to note — using VI inside another Emacs shell (shell, term, iterm) can have strange results when attempting to save, enter/exit command mode, etc., so use at your own risk!

Who uses Caps Lock anymore? More Control!

When I started using emacs again some time ago, I noticed quickly that it was very uncomfortable to move my little finger down to the left control key in order to do the many emacs control sequences. I wanted something much easier to use, or else emacs likely wouldn’t last. I recalled a while back remapping my caps lock key to another control key, so I looked into it. Not only is this good in Linux, but I was able to do it in Windows 7 as well, with a registry entry.

Here is a link to my github page with the required Windows registry update. Note that this has been tested on Windows 7 only. If you try it out with other versions, let me know if it works so I can update the post!

For Linux, three lines added to ~/.bash_profile or ~/.bashrc would do the trick:

xmodmap -e 'keycode 66 = Control_L'
xmodmap -e 'clear Lock'
xmodmap -e 'add Control = Control_L’

Once that file was sourced or you relog, it takes effect and you have a much more comfortable control key on your hands!

Update 10/15/2014:

You can also achieve the same results with a single command:

setxkbmap -option ctrl:nocaps

This of course uses a different command, so you have another option if you prefer to not use xmodmap, or if it’s not available for some reason.

Emacs tip: Find out what that keystroke does!

I’ve been taking quite a bit of my free time to learn more about Emacs, recently to customize the color theme the way I like, and I’ve been learning quite a bit. One post I found on Stack Overflow showed one of the users who made a function to cycle between a particular set of themes that he liked, depending on whether he wanted a light or dark theme. (Follow the above link for the answer by Simon Shine.)

One thing I noticed though was that he used C-t (Control T) as the key-binding, and I was pretty sure that it was used elsewhere. I used that keystroke in the scratch buffer, but didn’t see anything really happen. I knew it was bound because I didn’t get the “is undefined” message.

Enter “M-x describe-key“. With that little gem, you can enter a keystroke, and it will pop up a buffer explaining the function, where it’s source is, and how to use it. Very useful in learning Emacs!

Another thing to note, the describe-key has a little brother, “M-x describe-key-briefly“, which does the exact same thing, only displays a very brief answer in the minibuffer without opening a new buffer. Useful if you don’t want your existing window changed.

Errors with gpg –gen-key over SSH

This issue plagued me for some time… I was attempting to create a gpg key, add it to my keychain, and then gpg encrypt a netrc file to use in Emacs. It kept failing just before it would create the actual key, and was giving strange X-related errors. I should note that the “over SSH” part of this title means that I was connected via ssh on the server I was attempting to create the gpg key on.

One of the many error messages was that I did not have the “pinentry” package installed, but I verified multiple times that it was there, and even reinstalled it several times.It finally dawned on me that the reason it was looking for pinentry-gtk or pinentry-qt (both X-windows libraries) was that it thought I was locally logged into that server, and could see the desktop. This made me realize that it was trying to display the graphical interface for the passphrase, which could not be displayed because I was connecting via ssh. A quick echo $DISPLAY showed me that my ssh was attempting to forward the X connection to my local host, however the DISPLAY was not working because my local system did not have those packages installed. Once I issued unset DISPLAY my key was able to generate just fine, because it used the non-X version of the routine that asked me for my passphrase.

Another seemingly difficult problem solved by thinking things through!

Offlineimap & mu4e with Google Two-Factor Authentication

Like many people, I enabled Two-Factor Authentication to my Google account. I have so much data and information there, I thought it was appropriate to protect it with this added security. I have the Google Authenticator (which I also use for Evernote and Amazon Web Services) app on my phone, so it’s usually within reach and not too much of a pain to handle.

With this, I realized that my offlineimap stopped working (which is how I update my mailbox for mu4e (Mail Utility for Emacs) because of the lack of this added security. Knowing that removing the Two Factor Authentication was not an option, I delayed using mu4e/offlineimap for a while until I could figure it out. Tonight I finally was able to check that TODO item off my list — the solution? Application specific passwords for Google accounts.

Google has a nice page explaining the process, and it was extremely simple to configure. From within Gmail, you can click on Settings/Accounts and Import, then “Other Google Account Settings”. From there just select “app passwords” from the “Security” tab, and add a new application. A password will be generated, and just copy that password into the appropriate field in your ~/.offlineimaprc file, and you’re off and running again. Emacs mail *and* added security!

Back into Emacs

I’m a huge Emacs nerd. I first used it in university computer science courses, where they would provide commands and structures for everything you needed (at first). Later on I found that it was a decent text editor, and used it for such.  Throughout my time there, that was pretty much all I had used.

Fast forward about 5 years. I got my first real IT job at a large enterprise company supporting Solaris servers. First week on the job, I logged into a server I had to make a simple change on, and ’emacs /etc/hosts”.  I received “emacs: command not found”. Oh right, the normal community doesn’t use Emacs, it’s considered pretentious, bloated, and completely unnecessary for everyday sysadmin work. So with that, I had to use vi. (At that time, vim was not even installed on Solaris by default — and might still not be.) I had never used vi in my life, so I pumped out some quick Google-fu and found the basics, insert mode, command mode, and “wq” to write (save) and quit. Got it. Now I was a vi/vim user.

Fast forward a few more years. Damian Conway, noted Perl expert/lecturer, came to my workplace to give a perl workshop. The last day of the workshop, however, he ran out of material for perl, so he proceeded to give us a “one day vim crash course” and showed us how entirely awesome vim could be. I was astounded. I wanted to increase my skills and become more efficient. Over the next couple years I added a few tweaks here and there, nothing groundbreaking, but just a couple things.

Then I met up with a couple former co-workers who were heavy Emacs users, and I started to realize what I was missing out on during all those vi/vim years. Little bit little they would discuss things like tramp, jedi, el-get, various major modes, and org-mode. I quickly got back into it. I found Sacha Chua’s Emacs blog, another heavy Emacs  user, and started following her as well, learning quite a bit more.

Today I’ve successfully set up el-get, and even handier tool, allowing easy installations of various “recipes”, including github projects, and many others. The wonder keeps growing and growing.

Not too long ago I remember thinking that there is no way I will remember all of the Emacs commands, but so far, I’ve been surprising myself over and over again. I’m glad I took the step, and that I know I’m only scratching the surface of what Emacs has to offer.