In my last post, I talked about setting up Emacs with Prelude. With Prelude you get most of the features many professionals spend days setting up their emacs. I believe it is right set of features to start with.
In this post, I will expand on that and talk about how to leverage the features for actual work. I will talk about features that I use all the time.
Everyone’s work style and use case is different. Emacs does not come with all the features one might need. It is not uncommon to switch between emacs and terminal window to seperate editing and execution/processing of documents. However, if you repeat that process for all your work, it may be beneficial to use lisp functions to ease your work. I will talk that in later half of this post.
1. Recent Files
My typical emacs workflow starts with starting emacs (obviously) and pressing C-c f. It is shortcut for listing recent files I worked on. This feature is provided by helm package. Prelude comes with helm package installed, however it is not enabled by default. To enable helm go to “~/.emacs.d/prelude-modules.el” and un-comment prelude-helm and prelude-helm-everywhere packages. If you don’t need, don’t enable too many other packages. In my case, I only enabled the two helm packages.
Go to ~/.emacs.d/personal/custom.el and add following two lines at the end of the file.
(require 'recentf) (recentf-mode 1) (setq recentf-max-menu-items 50)
You can adjust recentf-max-menu-items size according to your preference. However, with helm you can search file names with any string that matches in a fuzzy way. It is better to have a bigger recent file list than a smaller one.
After you restart emacs, starting from next edit, recent file list should add new files to the list as they are being edited.
2. Working with files
To work with files you need to create it. With prelude it is easy to create files in new directory, without the hassle of typing several commands. Once you visit/create a file with C-x C-f command, if the file and the directory is new emacs will ask if you want to create those file and directory, just press “y” where appropriate.
As a developer it is a good idea to use some kind of software configuration management (SCM) system. SCM helps you properly track changes in software. Git is a popular choice for version control.
Some of my personal projects are hosted in github.com. Once I create a project using github’s website, I clone the project to my local working directory. Cloning a project downloads metadata (stored in .git directory) necessary to track file versions (changes). You also can create a local git repository and add github’s project URL as remote project location. Basic idea is, you want to upload your files to a website, so that you can access it later in another computer, share with others or work collaboratively.
Once you have a project tracked with git, emacs’ magit package can be used to manage files with git.
After you edit a file and want to commit(add) the changes, follow following commands.
magit-status or C-x g – See status of un-tracked files. (You can press Alt-x and type magit-status or press the shortcut key. With helm it is easy to type magit-status. You can type something like “ma sta“).
It will list new files under Untracked files list.
s – Highlight Untracked files header or the files under it and press s. It will stage the files to be committed. Staging area is a temporary location, where files are stored before moving to commit area. Once files are committed tracking of the files start. However, git best practices suggest “Commit often, perfect later, publish once“. That is what you should do as well. It is best to commit work than to regret losing it.
c c – Highlight the staged list and press c twice. This will bring you to commit message editor. Write a descriptive message for your commit, so that it is clear to yourself (or others) what work you did in that commit. Save the file with C-x C-s.
C-c C-c – Press C-c C-c twice to bring up committed list. This window will show Unmerged list. Committing changes committed the files to a local git repository. However, only after we push/publish/merge our local commits to remote repository, it will be available for others to work on. If you are the only one working on the project, you don’t have to worry too much about merge conflicts. However, if magit warns about remote and local not being in sync, execute “magit-pull” before publishing.
P p – Highlight Unmerged commits list and press P (Shift-p). This will bring another list, suggesting list of command options, press p. This should remove the Unmerged commits section and the comment in the Head section should reflect your last commit message. If you see that, your file is uploaded to github.
If you go to github, you should see your new changes.
4. Custom Functions
Emacs with Prelude is loaded with popular emacs repositories. You can list packages with package-list-packages and install with package-install command. This way you can extend emacs to adapt for a particular need. You can even install packages by directly downloading them from websites to proper folder inside ~/.emacs.d directory.
However as I said in the beginning of this post, your use case might be completely different than that of others. Sometimes you might want to do several processing using emacs’ commands or execute external commands. You may even want to group them in a single command. In emacs you can do that by creating your own script.
In prelude you can add custom emacs lisp files (.el) inside ~/.emacs.d/personal/ directory. I created a simple script to copy text and compile a source code. In this case, I know name of the files that I will be working on, but if you want to work with current file you can use current-buffer instead.
(defun solution-java-insert () (interactive) (insert-file-contents "~/Programming/Templates/Solution.java" nil) ) (global-set-key (kbd "C-x j") 'solution-java-insert) (defun compile-java () (interactive) (shell-command "javac Solution.java") (shell-command "java Solution") ) (global-set-key (kbd "C-x c") 'compile-java)
First function defined with defun inserts contents of Solution.java to current file. This way I don’t have to type the boilerplate code every time I want to work with such file. The interactive command will let you type the command using M-x (Alt-x) in emacs. Having this capability is useful, if you tend to forget shortcut keys for your commands. Next function also does similar thing. It compiles and executes specified file using the terminal. Scripts in ~/.emacs.d/personal/ directory is automatically read by emacs when it starts. However, if you want to test your changes immediately you can use load-file command instead.
To lookup what commands are available and what they do, type C-h f or C-h k.
Hope you liked this post.