December 5, 2013

Juggling Projects with Projectile

Filed under: blogging,emacs — jonEbird @ 9:56 am

When I’m introducing someone to Emacs, I will often talk about how a typical Emacsen will keep it running for long periods of time and will keep many files (buffers as we call them) open. I personally have over 100 buffers open as I write this. Part of the reason I have so many open is that I’m often jumping between different collections of files as I go about my work throughout the day. When I want to switch over to work on another project, I’ll switch to the desired buffer (C-x b) and while ido makes that easier, I still need to rely a good deal on mental memory to know which files make up which project. Worse yet is that some projects have commonly named files, so you may end up with buffers “Makefile”, “Makefile<2>”, etc. Which one was it?

The Projectile project aims to help solve this problem. It’s about context switching between projects and doing so deliberately. Projectile knows which files are a part of each project. The new workflow for switching to work on another project is: 1. Switch to Project (C-c p s) at which point you’re selecting (with ido again) against a list of projects only, then 2. picking a specific file from that project that you wanted to work on first. Mentally, it is “I want to work on file XX in project YY now. Take me there, Projectile!”. You can also easily switch between other buffers exclusively within the current project you’re working in (C-c p b) or open up a new file within your project (C-c p f). It’s about understanding which files and buffer make up your project and leveraging that to maximize your efficiency.

For example, earlier today I got an email asking for help on a minor issue. I wanted to consult a Python script but it’s been a while and I forgot the full path to the script. “Well, the project has ‘util’ in the name”, I thought to myself, so I switched projects (C-c p s) and typed “util” to narrow the choices of projects to the correct one and hit ENTER. I then typed “.py” to narrow the project files to only Python scripts I’m left with two choices. Nice! I didn’t have to remember the full path to the file and saved about 5-10s of searching which adds up with each project context switch.

The concept of managing the collection of files within a project is the core functionality of Projectile. I didn’t get that when I initially perused the few articles I’ve brought it to my attention. And yet Projectile does not stop there. Some of the other features that I’ve found useful in my 2 days of using it have been:

  • Launching an ack (C-c p a) or grep (C-c p g) to search through the files in your project.
    • This will replace my M-x rgrep habit.
  • Run a multi-occur on open buffers within your project.
    • I rarely used multi-occur because I found it a hassle to select which buffers. Happy addition.
  • Need to replace, ala query-replace (M-%), across all files in your project?
    • It can do that too. (C-c p r)
  • Handful of project testing integration (Listing these here but I honestly haven’t explored these features yet)
    • Support for switching between test file and your current buffer (C-c p t)
    • Find a test file (C-c p T)
    • Run your test suite (C-c p p)
  • And more

Now let’s make your experience with Projectile better:

  • Install guide-key and add “C-c p” (the default Projectile prefix key sequence) to your guide-key/guide-key-sequence. This makes learning a completely new system, like Projectile, nearly trivial since all of the actions use the same prefix. With guide-key, you type the beginning sequence “C-c p” and then be provided with a cheat-sheet.
  • Using IDO? Then install flx-ido and consider ido-vertical-mode
  • Install ack on your machine and then the corresponding Emacs ack-in-a-half module.
  • Make sure you have Exuberant Ctags installed for generating a proper TAGS file.

I will probably look to exploit the hooks available in Projectile. In particular, I think it would be nice to interact with my shell and switch to a different screen session or something. I’m excited to continue to use it.

February 7, 2012

Upgrading WordPress to 3.3.1

Filed under: blogging,emacs,usability — jonEbird @ 8:36 pm

Upgrading WordPress tonight from version 2.6.2 to the latest version 3.3.1. About time, right? Funny enough, my motivation was when I was configuring org2blog and noticed the URLs it was trying to hit were non-existent. That was my first clue that I needed to update. For the most users, I’d suggest following the very well documented Upgrading WordPress documentation, but I thought I’d show you how I do it. (Read: This post is pretty much for my own reference for the next time)

  1. Grab the latest wordpress release
upgrade_dir=wordpress_upgrade_$(date +%Y%m%d)
mkdir ~/${upgrade_dir} && cd ~/${upgrade_dir}
tar -xzf latest.tar.gz
  1. Following along with the Upgrading WordPress documentation.
  2. Take a backup of the current install.
mysqldump -u root -p wordpress > wordpress.dump
# Using the contents of the extracted latest.tar.gz WordPress release as my tarball file list
\ls wordpress | xargs tar -C /var/www/html -czf wordpress_backup.tar.gz
  1. Now for the actual manual upgrade
WPH="/var/www/html/" # My WordPress Home (WPH)
rm -rf ${WPH}wp-{includes,admin}
rsync -av wordpress/wp-includes ${WPH}/
rsync -av wordpress/wp-admin ${WPH}/
rsync -av wordpress/wp-content ${WPH}/
(cd wordpress; for f in *; do if [ -f "${WPH}/${f}" ]; then echo "Updating $f"; cat "${f}" > "${WPH}/${f}"; fi; done)
  1. Re-log into your site and proceed with the DB upgrade.
    Since my versions were so far off, the first thing I was greeted with upon login was a button to update the DB.

All done. The good news is I can now successfully log into my blog via org2blog plugin. Here is my elisp excerpt:

;; Setting up org2blog (Installed via ELPA)
(require 'org2blog-autoloads)
(require 'netrc)
(setq blog (netrc-machine (netrc-parse "~/.netrc") "jonebird" t))
(setq org2blog/wp-blog-alist
         :url ""
         :username (netrc-get blog "login")
         :password (netrc-get blog "password")
         :tags-as-categories t)))

Now to post this, I believe I’m supposed to M-x org2blog/wp-post-subtree. Well, that worked well and posted this as a draft. I noticed a few markup problems and after fixing them, I can finalize the posting via C-u M-x org2blog/wp-post-subtree. 2012-02-07 Tue 20:36

December 29, 2011

Installing emacs v24 on Fedora

Filed under: adminstration,blogging,emacs,linux,usability — jonEbird @ 10:05 pm

I’ve been reading about other people giving the yet to be release version 24 of emacs for some time now. When I decided to upgrade my systems to use v24, I was a bit surprised to not find anything about configuring a Fedora system to use v24 of emacs. Guess I gotta do it myself…

This tutorial is part editorial and part instructional. I thought it would be helpful to include some of the techniques I used to get emacs up and running quickly without needing to pull my hair for other’s edification.

After realizing I wasn’t going to be able to just grab a pre-built binary, I went looking for the official sources. I ended up finding the pretest download location. First step first, let’s pull down the latest emacs-24 tarball and extract it.

FILENAME=$(curl -s ${PRETEST_URL} | sed -n 's/^.*a href="\(emacs-24.[0-9\.]*tar.gz\)".*$/\1/p' )
tar -xzof $FILENAME
cd ${FILENAME%.tar.gz}

If that worked, you are now sitting in the extracted directory of the latest emacs-24 pretest source. Now for some instructional information. Any significantly large project will need a decent amount of development packages installed for a successful compile and that can be a pain to identify. Earlier I claimed that I didn’t pull my hair out which means I cheated. I grabbed the latest Fedora source rpm. I didn’t actually want to install the src.rpm but rather extract the emacs.spec file which will act like a blueprint for my build. I’m going to give you the answer later but if you’d like to know how to extract the specfile, try this:
Note: You do not need to do this step. Instructional only.

# Your SRCRPM may differ depending on what you end up downloading.
mkdir tmp && cd tmp
rpm2cpio $SRCRPM | cpio -ivd
sed -n -e 's/,/ /g' -e 's/^BuildRequires: //p' emacs.spec | xargs sudo yum -y install

Note the last command in that section was a command to install the necessary development packages for our build. Since I”m not requiring you to do that above, here is the command for you:

sudo yum -y install atk-devel cairo-devel freetype-devel \
  fontconfig-devel dbus-devel giflib-devel glibc-devel gtk2-devel \
  libpng-devel libjpeg-devel libtiff-devel libX11-devel libXau-devel \
  libXdmcp-devel libXrender-devel libXt-devel libXpm-devel \
  ncurses-devel xorg-x11-proto-devel zlib-devel librsvg2-devel \
  m17n-lib-devel libotf-devel autoconf automake bzip2 cairo texinfo \
  gzip GConf2-devel alsa-lib-devel desktop-file-utils python2-devel \
  python3-devel util-linux

The other part of the specfile you’ll typically want to look at, if you’re cheating like me, is the %build section. That is where you’ll find the actual commands used to configure and build the binaries. There I found the configure switches used so I don’t have to pick out which ones I’ll need. Again, just like figuring out the development packages, figuring out configure options can also be a chore. Let’s get to configuring, building and installing it now.

./configure --prefix=/usr/local/emacs24 --with-dbus --with-gif --with-jpeg --with-png \
  --with-rsvg --with-tiff --with-xft --with-xpm --with-x-toolkit=gtk
./src/emacs --version # Look good? The INSTALL doc suggests testing: ./src/emacs -Q
sudo make install

Well, that worked for me and hopefully it worked for you too. If you noticed, I used the --prefix=/usr/local/emac24 option above on my configure line which means everything got cleanly installed down it’s own separate base directory of /usr/local/emacs24. Since you won’t want to use that path explicitly each time you launch emacs, we’ll have to inform Fedora of our new altenative.

sudo alternatives --install /usr/bin/emacs emacs /usr/local/emacs24/bin/emacs 20000
sudo alternatives --install /usr/bin/emacsclient emacsclient /usr/local/emacs24/bin/emacsclient 20000

And there, we’re done. Congratulations. You have installed emacs version 24 on your Fedora system. Let me know if you’ve had any problems or have a better recommendation.

June 24, 2009

Emacs Registers and Bookmarks

Filed under: blogging,emacs,usability — jonEbird @ 5:02 pm

Every once and a while I like to re-read manuals about pieces of software which I already feel quite comfortable in using in hopes to learn a new trick or two. Today, I was browsing the freely available GNU Emacs Manual and was breezing through it until I hit the registers section.

Have you every copied a region of text in plans to yank it back in multiple locations within your current buffer but in between this work, you realize you need to do some intermediate killing and yanking and therefore have overwritten your original region of text you had copied? Well, using registers is one way in solving that problem.

Bookmarks have a similar function as with registers within emacs. I am actually grouping them together in this weblog because their key sequences are so very similar. Bookmarks are, like the name implies, a way to keep track of position within a buffer. They can be saved and later referenced to not only re-open the particular file you were editing but take you back to the position within that file as well.

I can not seem to remember anything, from emacs commands or keystrokes to basic shell commands, without coming up with some mnemonic for memorizing it. I have just came up with one such mnemonic for using Registers and Bookmarks and I thought I’d share it with my bots (machines which read this weblog).

Register and Bookmark Mnemonics

Each key sequence starts with:
C-x r – Think “r” for register.
The general pattern is:
C-x r <key> <register> – “register” is any single digit or letter.

“key” is what I’m calling each category of register usage. Let’s explore them:

  • “<space>” – mark – Just like C-spc to set the mark, this asks that the mark be set in our register.
  • “s” – save – save the region’s text into the register.
  • “n” – number – save a particular number into the register.
  • “m” – bookmark – We’ll explore bookmarks further, but notice how you set bookmarks with what I consider the register key sequence.

Those are some of the basic storing actions, but with the same C-x r prefix you can perform other actions with the contents of the registers:

  • “+” – increment – When the register is a number, this increments it’s value. Convenient to use with macros.
  • “i” – insert – This action can be used for numbers, regions of text and even marks!
  • “j” – jump – jump to a mark. This assumes you have already stored a mark in the particular register.

We’ll shift a bit into bookmarks but stay succinct within the mnemonics section here.

  • “m” – bookmark – Repeated from above. Note that this is a interactive function which allows you to name your bookmark with an intelligent name. The default will be the basename of your current buffer’s filename. So, if you’re editing /path/to/emacs_notes.txt it will default to store the bookmark under “emacs_notes.txt” but maybe you want it to be called “emacs notes”. If so, go ahead and type that out and hit RET.
  • “l” – list bookmarks – This opens a new pseudo buffer with the list of all of your bookmarks.
  • “b” – bookmark jump – Jump to the named bookmark. This is a interactive function as well.

Bringing it Together – Examples

Save current mark to register “l”:
C-x r <space> l
Move to mark saved in register “l”:
C-x r j l
Save number in register “n”:
C-x r n n
Now, increment that number and then insert it:
C-x r + n C-x r i n

Love the emacs notes you’re editing, bookmark it:
C-x r m emacsnotes RET
Buried into multiple install READMEs for a particular product and want to return later:
C-x r m installreadme RET

Finally, the prettiest of the commands, let’s review our bookmarks:
C-x r l

Final Note on Registers and Bookmarks

There is a variable named bookmark-save-flag which when set to the value of “1″ will have the action of automatically updating your ~/.emacs.bmk file with any updates to your bookmarks. I recommend setting this in your ~/.emacs file so you don’t have to “M-x bookmark-save” periodically. Add the following to your ~/.emacs file:
(set-variable (quote bookmark-save-flag) 1 nil)

Finally, I’d be remiss if I didn’t mention how I generated that one-liner emacs-lisp line, even if it’s off topic for this weblog entry. I like to use various interactive functions and then capture their effective execution, in emacs-lisp form, via the “repeat-complex-command” function which is bound to “C-x M-:“. In this situation, I used the “set-variable” interactive function to set “bookmark-save-flag” to “1″ and then punched in “C-x M-:” and copied the one-liner for my ~/.emacs file. So, there you go, a bonus tip for those who’ve read this far.

September 18, 2008

Essential Emacs Knowledge

Filed under: blogging,emacs — jonEbird @ 9:33 pm

I am on a island at work and there is no one else to converse with other than Wilson. The island is called “Isle de Emacs” and Wilson is, I guess, psychoanalyze-pinhead. But occasionally I do get a visitor to the island and the conversation is more or less the same stuff. Okay, enough with the analogy, this short tutorial is about getting you acquainted with using the Emacs editor. The repeated conversation I was referring to was an introduction to Emacs and trying to educate my co-worker on both the essential pieces of knowledge and a bit of Emacs philosophy to help guide their learning process.

Essential Knowledge

- In case you don’t know, emacs is really just a lisp interpreter.
  Means it’s nearly infinitely extensible.
  P.S. Also, in case you don’t know, Lisp is a programming language. That means practically everything the editor is supporting, from moving the cusor word by word to saving and opening files, is actually being done via code written in Lisp.
- Files being opened and edited are referred to as buffers.
  To say you are “closing a buffer” means you are closing that file and will no longer have it open.
  Similarly, when we say “switch to buffer” means that you are changing the current screen from what you are viewing/editing and moving to the next file.
- In keyboard shortcut notation, (C-x f) means to hold the
<control> key then depress <x>, then releasing and then
pressing <f>.
  In general, a space means you should stop pressing any keys and then move onto the next key sequence.
- Emacs commands are often a two sequence routine.
  Open a file (C-x C-f), save (C-x C-s), quit the editor (C-x C-c).
  Psst. (C-x) is common starting sequence, particularly for the basic operations.
- What does the “M” character stand for in keyboard sequences?
  It is called the “Meta” key and to execute it, you can use the <Alt> key or you can alternatively hit <Esc> followed by the next key. If you use the <Esc> key, you do not depress both keys at the same time, whereas with the <Alt> you do hit both keys at the same time.
  So, to get the command prompt (technically executing execute-extended-command), the sequence is (M-x). With the <Esc> method, it’s hit Escape, then hit “x”, whereas with the <Alt> you would start by holding <Alt> and then depress “x”.
  I used to use the <Esc> key method, but forced myself to use the <Alt> method in order to keep my fingers on the home keys for speed.
- To set a keyboard shortcut, it is called a “binding”.
  The interactive emacs command to do this is called “global-set-key”.
- The equivalent to “man man” of learning shell skills is (C-h ?)
  There you will see various types of help. You can search the building commands, you can query what a keyboard sequence will execute and much more.
- Emacs can do nearly everything, but it is fairly esoteric in it’s approach to being your text editor.
  What this means is that it’s learning curve is pretty steep. If you try to climb that hill initially, you’ll probably give up.
  Try to just learn the essentials and don’t bother with anything else. Learn one thing at a time. Use the menu, use the mouse, use your arrow keys, the pgdn and pgup keys, etc. Try to use the editor just like you would use notepad, but understand that one of the goals of a advanced emacs user is to not need the menu, scroll bars, mouse, etc.
  I once attended a RedHat training course where the instructor called himself a “emacs guy” but watching him actually use the editor appeared to me like he just started using it last week. But hey, that is totally cool, and really, the correct approach for learning emacs.
- Why should I invest in a learning a editor with such a steep learning curve?
  Because it can do nearly anything and can be customized for everything. Without going into a full dissertation, this means that the effort you are investing into this editor will not be put to waste. And moreover, you will not invest efforts just to find that you’ve reached the extent of what the particular editor can do.
- When you use (M-x) to enter a command, if there is a keyboard shortcut, the mini-buffer will tell you what it is.
  Psst: Can also use (C-h b) to show a helping page about the keyboard bindings.
- You can Tab-complete nearly everywhere.
  First place you’ll notice is when opening a file, but you can use within the mini-buffer too. Can be used to complete emacs commands,
  variable names, etc.
- So, you’ve forgotten that command but you remember it’s about the keyboard?
  Search the commands, (C-h a) then just type “key” and ENTER to search for commands with “key” in the name.
- In case you get yourself stuck or just not sure what you just pressed, there are two common ways to quit out.
  (ESC ESC ESC) – Hiting Escape three times gets you out of what you’re doing. This actually works for closing out that split screen help window too.
  The other way to quit out of items is to use (C-g), but there are scenarios where ESC ESC ESC works and C-g doesn’t.
- Customization is done within your ~/.emacs file.
  You are technically executing emacs lisp here. Since you’re reading this, I’ll assume you’re a newbie and as such, just go and copy somebody else’s .emacs file. If you’re still a Emacs user after a year, perhaps you’ll finally feel adventurous enough to dive in there and generate your own customizations.
- Support for different types of files is done via different “modes”.
  Your current “mode” is displayed in the status bar, at the bottom of the editor, in parentheses.
  To change to text mode, for example, execute the command text-mode (M-x text-mode). In general, to enter any mode, it’s going to be the modename followed by “-mode”. Means, for Python, execute python-mode.
  A cheap way to see how many modes Emacs supports is to do a apropos search on “mode”. That is, (C-h a “mode”) to show each command with the word “mode” in it.
- How do I automatically execute something when entering a particular mode?
  Although not an essential piece of knowledge, to execute something when entering a mode to to “create a hook”. By convention, the hooks are stored by the variable matching the mode name and adding “-hook” to the end. So the hook for executing something when I enter “outline” mode is stored in the variable “outline-mode-hook”. I didn’t use that example by accident either. Here is my hook, used in my .emacs file:
(add-hook ‘outline-mode-hook ‘hide-body)
  Even without any Lisp knowledge, that should be fairly intuitive. It will automatically fold the body of each section within my outline document upon opening it.
  Psst: Try sticking a “-*- outline -*-” as the first line of your outline file while keeping the file extension .txt. Without that line, emacs will assume it’s plain text because of the .txt extension, but with that line, you are telling emacs to use outline-mode for this file. It’s great for keeping notes.
  I use it for keeping notes on every single work request ticket I receive at work. Such a file could become beastly in size but I don’t notice since it’s all rolled up when I re-open it.

Essential Habits.

- Keep your buffers open
  Learn to switch between buffers (C-x b TAB)
  I, myself, keep my emacs editor open at all times. I never close it. I keep buffers of my week’s work open for weeks on end. I’ll pretty much only close buffers when it becomes too cluttered when switching between buffers (C-x b TAB).
  Finally, I currently have my EDITOR variable set to “emacsclient” which means commands such as “crontab” connect to that single running emacs editor.
- Tab complete everything.
  Just like you should be doing in the shell, tab-complete commands, filenames, etc within emacs.
- Use the desktop-save feature.
  This tip suppliments the habit of keeping your buffers open. The desktop-save allows you to keep track of all buffers currently open and to reopen them upon restarting emacs. This is useful on laptops where you won’t be able to keep a copy of emacs always running.
- Pay attention to the keyboard shortcuts which are listed in the menu as you navigate to execute them.
  This goes along with the “learn one trick at a time” suggestion. Next time you get tired of executing that command from the menu, instead learn the keyboard shortcut for it.
  Psst: If you are curious what emacs lisp command is being executed in the menu, you can still use the keyboard help command (C-h k). People normally use that help command to tell them what command is being executed for the keyboard sequence but few realize that it works for menu items as well.

Essential Tricks

- (C-x ESC ESC) – Repeat complex sequence
- Want to assign a keyboard shortcut to something which you do alot?
  E.g. By default “goto-line” is not set to a shortcut. Let’s say you want to assign to (C-c g).
         Run (M-x global-set-key). It will prompt you what keyboard sequence to use and what to execute.
         Next run, (C-x ESC ESC) which is for repeat complex routine. At this point, the sequence is listed in emacs-lisp form in the mini-buffer.
         Run (C-x o) to get to the mini-buffer so you can copy that text (C-a C-space C-e M-w) and return to the normal buffer (C-x o).
- If operating in a corporate environment with a jump machine segregating your workstation and the rest of your environment, use TRAMP.
  Tramp allows you edit remote files over various protocols. I recommend using “ssh” and to that point, stick these lines into your .emacs file:

(setq tramp-default-method "ssh")

- Redefine how your backups are made.
  Emacs will automatically make backups of your files as you are editing them. By default it sticks a “~” character at the end of the file.
  Well, this is quite annoying for me since it will also keep file permissions. If I’m creating a script, now I can’t get a unique tab-completion in the shell by typing (./scr TAB) to expand to “./”. Instead it finds “” also. Move your backups to ~/.backups directory. Create that directory and then add these lines to your .emacs file:
(defun my-make-backup-file-name (file-name)
  “Create the non-numeric backup file name for `file-name’.n
   This customized version of this function is useful to keep all backups
   in one place, instead of all over the filesystem.”
  (require ‘dired)
  (message (concat “make-backup: ” file-name))
  (if (file-exists-p “~/.backups”)
      (concat (expand-file-name “~/.backups/”)
          (dired-replace-in-string “/” “|” file-name))
    (concat file-name “~”)))
(setq make-backup-file-name-function ‘my-make-backup-file-name)
- Use In fact, the last backup trick was taken from there.
  Just as I recommended above, don’t try to become an expert overnight. Learn the basics well and then pick up one trick at a time. I believe this can be a never ending process as you’re goal should be to become more and more proficient in your editor. So, go out and learn just one new trick and don’t come back until that new trick is ingrained into your finger’s memory.

Essential Memorization List

- (M-x) – Execute emacs command.
- (C-x u) – Undo.
- (M-x revert-buffer) – Oops, I just totally messed this file up and need to revert from the saved copy.
- (C-x o) – Move cursor to other split window pane.
  Nice for looking through the help buffer after it just split your screen.
- (C-x 1) – Keep current buffer and remove other split pane.
  I.e. Get that other window out of my way, please.
- (C-x 0) – Keep the _other_ buffer and remove this one.
  Just like the previous, but you’re keeping the other buffer as the sole window.
  Nice when you’re paging through an apropos section and are done. Use this to close the window without switching back first.
- (C-x C-f) – Open file
- (C-x C-s) – Save file
- (C-x C-c) – Exit

The goal of this short guide was to arm you with the necessary knowledge
in order to get you going with one of the most powerful editors around.
I didn’t necessarilly want to write a whole series of Emacs weblog entries. Instead, I just did a brain dump and I hope it wasn’t too overwhelming. Instead, I’d rather hope it helps
guide you to the island. We’ll play some volleyball, snorkel, hunt peacocks,
etc; you know, island stuff.