Emacs Utilities

Table of Contents

Top

1 Emacs Utilities

1.1 Overview

Some Emacs utilities and goodness.

Feature Entry Point Description
Help Commands    
Help for all help commads M-x help-for-help  
Emacs built-in tutorial M-x help-with-tutorial Open Emacs tutorial.
Emacs built-in localized tutorial M-x help-with-tutorial-spec-language Emacs built-in tutorial in many languages like Russian, Portuguese …
     
Package Manager    
Package Manager M-x list-packages List all packages available.
  M-x package-install Install some package
     
Info page and Man page - Unix docs    
Info page M-x info / M-x heml-info Info page reader. The helm-version is provided by heml package.
     
Manpage M-x woman / M-x helm-man-woman Manpage Reader
     
File Manager and edit remote files    
File manager M-x dired or C-x d File manager and directory editor
     
Tramp mode   Tramp mode - edit remote files, edit as super user and open
    remote directories through ssh, sftp, telnet and ftp
Programming    
Compile mode M-x compile Run any compilation command, compiler or build automation tool like make or ant.
GNU Debugger Interface M-x gdb  
Git Version Control System M-x magit-status Git user interface provided by the magit package.
     
Diff files M-x ediff File diff viwer
Diff buffers M-x ediff-buffers Buffers diff viewer like ediff
     
VIM Emulation M-x evil-mode Emulates VI/VIM editor behavior. It is provided by the evil package.
     
Shells    
Shell Command M-x shell-command Run a batch shell command like ifconfig or dmesg (Linux)
Async shell command M-x async-shell-command Run any shell command asynchronously.
Eshel M-x eshell Multi platform shell written in Elisp and highly integrated to Emacs.
Ielm M-x ielm Elisp Interactive shell to control and Debug Emacs
     
System Related Commands    
Copy file M-x copy-file Copy a file. Prompt user for a file to copy.
Delete file M-x delete-file Prompt user for a file to delete.
Make directory M-x make-directory Creates a new directory. Equivalent to shell command $ mkdir
Delete directory M-x delete-directory Delete a directory. Promps the user for a directory to delete.
Copy directory M-x copy-directory Copy directory.
     
Task manager/ Process manager M-x proced Htop like multi platform task manager.
     
Network Tools    
Network information M-x ifconfig Show iformation about network interfaces
Ping M-x ping Ping a host name or ip address
Traceroute M-x traceroute Traceroute a host name or ip address
ARP M-x arp Display the command arp output.
Routing Table M-x route Show routing table.
Whois M-x whois Run whois command to show information about route.
Telnet M-x telnet Telnet login. It is the acient version of SSH.
     
Misc    
Calculator M-x calc RPN (Reverse Polish Notation) calculator like HP-48g engineering calculator.
Color Pallete M-x list-colors-display Display a collor pallete.
     

Note:

  • M-x ifconfig - runs ipconfig on Windows and ifconfig or ip on Linux. It is platform agnostic.
  • To Emacs' tutorial (help-with-tutorial) work the cua-mode must be disabled (M-x cua-mode to disable it).

Fun

Command Description
M-x doctor If you fell bad, your country is in a endless recession …
M-x life Run Conway's Life simulation.
M-x dunnet Text based adventure game
M-x tetris Play Tetris.
M-x hanoi Towers of Hanoi.
M-x zone Make buffers go crazy!!
M-x zone-leave-me-alone Stop zoning out.

1.2 Shells

Emacs have built-in commands to run shells.

Name Command: M-x <command> Description
shell M-x shell Runs an inferior shell app. It can run bash, zsh and etc.
     
term M-x term Emacs' terminal emulator that can run apps like top, vim, bash and etc.
    It alows command line line completion inside Emacs in apps like bash, zsh and python .
     
eshell M-x eshell Emacs' built-in shell written in Elisp. it has elisp implementation of cd, ls, pwd
    and other standard Unix commands and apps. Works on all operating systems. Eshell
    also can run Elisp functions. It is a good alternative to Microsoft Windows
    terminal emulator cmd.exe.
     
ielm M-x ielm Emacs' elisp shell that can be used to control, test and interact with Emacs.
     

Emacs also have commands to run programming languages shells:

Language Command Package
Python M-x run-python -
Octave M-x run-octave -
Ruby M-x run-ruby inf-ruby
Elisp (Emacs Lisp) M-x ielm -
Scheme M-x run-scheme -
Lisp M-x run-lisp -
     

1.3 Async Shell Command

It can run any repls or interactive shell applications, except ncurses-based apps like htop.

Example: M-x async-shell-command "java -jar clojure.jar".

It is useful to run programming languages shells even if there is no mode or package installed for it. It is possible to rename the shell buffer with M-x rename-buffer in order to make easier to identify the buffer.

Examples:

Run sqlite3:

  • M-x async-shell-command sqlite3

Run scala repl:

  • M-x async-shell-command scala and then M-x rename-buffer *scala*

to change the default buffer name to a more readable name *scala*.

Run mono's csharp repl:

  • M-x async-shell-command csharp

Run Windows powershell:

  • M-x async-shell-command powershell -Command -

Run Java bean shell (Java Interpreter):

  • M-x async-shell-command "java -cp ~/opt/bsh-2.0b4.jar bsh.Interpreter"

or programatically:

(async-shell-command "java -cp ~/opt/bsh-2.0b4.jar bsh.Interpreter")

Defining a command:

(defun run-bsh ()
    "Run Java bean shell."
    (interactive)
    (async-shell-command "java -cp ~/opt/bsh-2.0b4.jar bsh.Interpreter"))

Example: Running Haskell REPL.

  • M-x async-shell-command stack ghci

I90yeoX.gif

Example: Install a Haskell package

  • M-x async-shell-command stack instal turtle

btfaK3k.gif

Example: Running Fsharp REPL

  • M-x async-shell-command fsi

tRKPfXG.gif

Example: Ping a server to test connection 8.8.8.8 (Google's DNS)

  • M-x async-shell-command ping 8.8.8.8

3OeuGNj.gif

1.4 Compiling in Emacs - M-x compile

1.4.1 Compilation Commands

To compile any code use:

  • To compile: M-x compile <compilation command>
  • To recompile: M-x recompile or type g at the compilation buffer.

Compilation Commands

Command Key binding Description
M-x compile   Run a compilation command
M-x recompile g (compilation buffer) Run a compilation command again.
M-x kill-compilation C-c C-k (compilation bufffer) Stop compilation process.
M-x compilation-next-error M-n Go to next error
M-x compilation-previous-error M-p Go to previous error
     
     

Compilation Buffer Key bindings

Command Key binding Description
M-x recompile g Compile again running same compilation command.
M-x quit-window q Quit compilation buffer
M-x kill-compilation C-c C-k Stop compilation process
  < Go to beginning of compilation buffer.
  > Go to end of compilation buffer.
M-x describe-mode h  
  RET Typing Return at compilation error goes to error location.
     
     
M-x rename-buffer   To use multiple compilation buffers, rename it with this command.
     
  • Note: The command M-x compile provides completion for Makefile targets by typing M-x compile make <tab>.

1.4.2 Setting compilation key bindings

The code block bellow will set the key bindings as shown in the following tables:

Key bindings for outside compilation buffer.

Key binding Description
f9 Run M-x compile
Windows-Key/Super-f9 Run M-x
M-[ Go to next compilation error
M-] Go to previous compilation erro
   
   

Key bindings for compilation buffer

Key binding Description
q Quit compilation buffer (default)
g Run M-x recompile (default)
c Run a new compilation command
k Kill compilation
mm Run make - M-x compile 'make'
mc Run make clean - M-x compile 'make clean'
mp Run make clean and make again.
   
(global-set-key [f9]    #'compile)

;; Not s-f9 means Windows Key/f9
(global-set-key [s-f9]  #'recompile)

(global-set-key (kbd "M-[") #'compilation-previous-error)
(global-set-key (kbd "M-]") #'compilation-next-error)

(define-key compilation-mode-map "c" #'compile)
(define-key compilation-mode-map "k" #'kill-compilation)

(define-key compilation-mode-map "mm" (lambda () (interactive)
                                        (compile "make")))

(define-key compilation-mode-map "mc" (lambda () (interactive)
                                        (compile "make clean")))

(define-key compilation-mode-map "mp" (lambda () (interactive)
                                        (compile "make clean && make")))

1.4.3 Creating custom compilation commands

  1. Compile Haskell

    Compile single-file haskell apps with stack:

    (defun compile-haskell ()
      "Compile a haskell .hs file."
      (interactive)
      (save-buffer)
      (compile (format "stack exec -- ghc %s -o %s.bin"
                       (buffer-file-name)
                       (file-name-base (buffer-file-name))
                       )))
    
    
    (defun compile-haskell-run ()
      "Compile a haskell .hs file and runs the executable."
      (interactive)
      (save-buffer)
      (compile (format "stack exec -- ghc %s -o %s.bin && ./%s.bin"
                       (buffer-file-name)
                       (file-name-base (buffer-file-name))
                       (file-name-base (buffer-file-name))
                       )))
    

    The command M-x compile-haskell can be used to compile this code bellow that will produce the executable myApp.bin.

    The command M-x compile-haskell-run compiles the code and runs the app.

    File: codes/myApp.hs

    import qualified System.Directory as D
    
    main :: IO ()
    main = do 
      putStrLn "Hello World Haskell!"
      D.getDirectoryContents "/" >>= mapM_ putStrLn
    
  2. Compile F# (Fsharp)

    The command M-x compile-fsharp-run compiles a F# app .fsx to .exe and runs it with mono.

    (defun compile-fsharp-run ()
      "Compile a Fhsarp .fsx file to .exe .NET executable."
      (interactive)
      (save-buffer)
      (compile (format "fsc --target:winexe --platform:anycpu --standalone %s --out:%s.exe && mono %s.exe"
                       (buffer-file-name)
                       (file-name-base (buffer-file-name))
                       (file-name-base (buffer-file-name))                   
                       )))
    

    This command can be used to compile the file helloWinform.fsx to helloWinform.exe and run it.

    File: codes/helloWinform.fsx

    open System.Windows.Forms
    open System.Drawing 
    
    let f = new Form () 
    f.Text <- "Hello world"
    f.Name <- "Form1"
    f.BackColor <- Color.Blue
    
    let btn = new Button (Text= "Click Me", BackColor = Color.Green)
    f.Controls.Add(btn)
    btn.Click.Subscribe(fun _ -> printfn "Click me again")
    
    Application.Run(f)
    

1.4.4 Example

Example: Running a F# script. To see the image fullscreen open: http://imgur.com/a/7Hvrz and click at the image.

mBMJmqp.gif

In this case was used the command M-x compile fsi helloWinform.fsx. That runs a F# script performing and all syntax errors and value errors will be shown at the compilation buffer.

Note: This code also works in Linux or OSX. It was recorded in Windows because there was no good GIF recording apps available for Linux at the time this was recorded.

Code used in this example: File - helloWinform.fsx

open System.Windows.Forms
open System.Drawing 

let f = new Form () 
f.Text <- "Hello world"
f.Name <- "Form1"
f.BackColor <- Color.Blue

let btn = new Button (Text= "Click Me", BackColor = Color.Green)
f.Controls.Add(btn)
btn.Click.Subscribe(fun _ -> printfn "Click me again")

Application.Run(f)

1.5 Emacs in Terminal

Running Emacs in terminal:

  • emacs -nw

Running Emacs fast without read ~/emacs.d/init.el or ~/.emacs startup file:

  • emacs -nw -Q
  • Start Emacs in terminal running Eshell:

Running Emacs in terminal or executing some Elisp command or app:

$ emacs -nw -q --eval '(eshell)'
  • Start Emacs in terminal running python:
$ emacs -nw -q --eval '(progn (run-python "python3") (switch-to-buffer "*Python*"))'

See also:

1.6 Manpage Reader

To read manpages enter M-x woman.

Example: Rsync manpage.

utilities_manpage.png

1.7 Info Page Reader

  • To Browser info pages and also the Emacs documentation. M-x info
  • Search Info pages. M-x info-apropos

Example: Top info page.

utilities_infopage_root.png

utilities_infopage_tramp.png

1.8 Calculator - M-x calc

Overview:

  • RPN Calculation (Like HP48g series)
  • Good for small calculation and verify expected values.
  • Scientific calculator
  • Bitwise operation: Useful for low level programming like: C-programming, embedded systems, network and machine instructions
  • Units - Convert units like meters, inches, feets, min, seconds, bits, bytes …
  • Functions (cos, sin, exp bound to keybindings)
  • Date calculations
  • Symbolic Math

Documentation

   
Describe keybindings C-h m
Calc Info manual M-x calc-info
   

Stack Commands

     
Dup RET Duplicate last number of stack.
Swap Tab Swap the last two numbers of stack.
     
     

Scientific Functions

Function Key
set precision p
change sign n
reciprocal 1/n &
square root Q
   
ln L
exp (ex) E
10x H E
   
sin S
cos C
tan T
   
radian mode m r
degree mode m d
   
pi π (3.415…) P
   
factorial !, k d
Prime factorization k f
GCD k g
LCM k l
Error function - erf f e
Error fuinction - erfc I f e
   

Financial Functions

   
Enter percentage M-%
Convert to percentage c %
Percentage change b %
   
present value b P
future value b F
rate of return b T
number of payments b #
size of payments b M
net present value, int rate of return b N, b I
   

Formula transformation

Description Key binding Output
Enter a formula ' (-b + sqrt(b2 - 4*a*c) / (2 * a))  
Print the formula in Latex mode dL \frac{\sqrt{b^2 - 4 a c} - b}{2 a}
Creates a C-lanaguage code dC (sqrt(pow(b, 2) - 4*(a*c)) - b) / (2*a)
Normal Language dN _' (-b + sqrt(b^2 - 4*a*c) / (2 * a))_
Show formula in big display mode dB  

Unit Conversions

Description M-x command
Enter number with unites ' 100 ft/hr
Convert to new unit, base unit u c, u b
Convert temperature units u t
Simplify unit expression u s
View unit table u v

Numbers with different base

Description Emacs Calc Base 10
Binary 2#11110101 245
Octal 8#27 23
Hexadecimal 16#FFFF 65535

Bitwise Operations

Keybinding M-x command Description
    Number Representation
d 0   Decimal (Standard )
d 2   Number representation to binary 2#00100111
0 d 2   Two-complement binary
d 6   Hexadecimal
     
    Word Size
b w   Set word size (8 bits, 16 bits, 32 bits …)
b c calc-clip Clip the current number to current word size.
     
    Bitwise operation
b a calc-and Bitwise AND
b o calc-or Bitwise OR
b x calc-xor Bitwise XOR
b n calc-not Bitwise NOT
b d calc-diff Bitwise difference
b r calc-rshift-binary Bitwise right shift by 1 bit
b l calc-lshift-binary Bitwise left shift by 1 bit

See also

1.9 Magit - Emacs Interface to GIT

See:

Magit Remote Branches

Magit Reference Card:

Another Reference Card

Command M-x Key bind GIT  
  magit-status    
magit-status   git status Magit status is the main entry point to Magit
magit-init   git init Init a git repository
magit-log   git log  
magit-process-bufffer $   Displays the git command and its output
  g   Update magit status buffer
  l   History
  L   Verbose history
  !! git <command> Run a git command
  !s   Magit shell command
       

Remote Branches

Command M-x  
magit-add-remote Add a remote branch to magit
magit-remote-add M-a add remote repository
magit-remote-remove M-d remove remote repository
   

Untracked files commands:

     
s Add untracked file to staging area  
C-u S Stage all untracked and tracked files  
i Add filename to .gitignore  
k Delete untracked file, forever  

Staging and Commiting

   
u Unstage
C-u s Prompt file for stagin
U Unstage everything that has been staged
c Pop buffer for commit message
C-c C-c Amend commit message
C-c C-k Erage magit-log-edit buffer and bury it
   

Tagging

   
t t Create a lightweight tag
t a Create annotated tag

Stashing

   
z z Create new stash
a Apply stash
A Pop a stash
k Drop a stash

Branch

   
   
b v Branch manager
b b Switch to a different branch
b n Create a new branch
k Delete current branch
C-u k Delte branch event if it hasn't been merged
m m Merge the branch in current line to specified
g Refresh branch list
q Buries the branch list and deletes the window
   

1.10 Helm utilities

Command Description
Elisp Programming  
M-x helm-M-x Show all M-x commands
M-x helm-apropos Apropos with helm interface.
   
Search Documentation  
M-x helm-info Search Info pages.
M-x helm-man-woman Search man pages.
   
Files and buffer  
M-x helm-buffers-list Switch buffers
M-x helm-find-files Helm equivalent of M-x find-files
   
Search  
M-x helm-occur Occur-like command with helm interface.
   
Org-mode  
M-x helm-org-in-buffer-headings Filter org-mode files by headlines.
   
Code Navigation  
M-x helm-imenu Code navigation with Imenu
   
Process Manager  
M-x helm-top Task/Process manager like M-x proced
   
Color Pallete  
M-x helm-colors Select a color and copy to clipboard
   

1.11 Spell Checker

Emacs uses external programs to check the spell. It supports ispell, aspell and hunspell. The variable ispell-program-name sets the spell check software used by Emacs. The default dictionary used by Emacs is US English, but it can be changed with the command M-x ispell-change-dicitonary.

Spell check interface commands:

Command Default Key Bind Description
M-x ispell-buffer   Check the spelling of all buffer.
M-x ispell-word M-$ Check the spell of word at cursor position.
M-x ispell-region   Check the spell of selected text.
M-x ispell-change-dictionary   Change the language of dictionary used by the spell check.
M-x flyspell-mode   Toggle flyspell mode. When active, it will highlight all misspelled .
M-x flyspell-buffer   Check the hole buffer in flyspell minor mode.
M-x ispell-kill-ispell   Kill the Ispell subprocess.

Set the default Spell checker:

(setq ispell-program-name "aspell")

;; or
(setq ispell-program-name "ispell")

;;
(setq ispell-program-name "hunspell")

The dictionary can be changed with the Elisp code:

(ispell-chage-dictionary "en")
(ispell-chage-dictionary "castellano")
(ispell-chage-dictionary "german")


;; M-x ispell-spanish
;;
(defun ispell-spanish ()
 (interactive)
 (ispell-change-diciontary "castellano"))

;; M-x ispell-en
;;
(defun ispell-en ()
  (interactive)
   (ispell-change-diciontary "en"))

To right click in the word and get suggestions use:

(eval-after-load "flyspell"
  '(progn
     (define-key flyspell-mouse-map [down-mouse-3] #'flyspell-correct-word)
     (define-key flyspell-mouse-map [mouse-3] #'undefined)))

The ispell doesn't check the spell of comments and strings. This behavior can be changed with the code below that allows it to check the spell in all programming modes like python-mode, haskell-mode and so on.

(add-hook 'prog-mode-hook 'flyspell-prog-mode)

See also:

Created: 2018-07-26 Thu 07:42

Emacs 25.3.1 (Org mode 8.2.10)

Validate