Emacs Tips and Tricks

Emacs configuration

~/.emacs

(setq backup-directory-alist
          `((".*" . ,temporary-file-directory)))
    (setq auto-save-file-name-transforms
          `((".*" ,temporary-file-directory t)))

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)

(transient-mark-mode 1)

(add-hook 'before-save-hook 'delete-trailing-whitespace)
  • transient-mark-mode 1 enables highlighting on mac.
  • add-hook 'before-save-hook 'delete-trailing-whitespace remove trailing whitespace when document saved.

Jump to first non-whitespace character in line

M-m or Esc-m on Mac.

While writing code in Emacs, I have to go to the the first character in the line.

var parseTrailbyEvent = function(){
    async.each(eipEventNames, getTrailForEvent,
               function(err, result){
                   if (err) {
                       console.log("Error occurred");
                   } else {
                       console.log(trailEvents);
                   }
               })
};

Since there are many indents on the lines, it is a painful to go to the beginning of the line and then move until the first character.

Add tab to multiple lines

Select multiply lines, then type C-u 4 C-x Tab, it will indent the region by 4 spaces.

Search and replace

M-x replace-string Enter <current string> Enter <new string>

On mac M-x is Esc-x.

comments powered by Disqus