Search This Blog

Tuesday, July 5, 2016

Tips for Emacs Beginners

Emacs outshines all other editing software in approximately the same way that the noonday sun does the stars. It is not just bigger and brighter; it simply makes everything else vanish. — Neal Stephenson
Q: Why should I learn emacs? Ive heard 'real programmers' use emacs…
A: Real programmers use their brain
Real programmers program their brain
Often, real programmers' brain-programming program is emacs. — Thien-Thi Nguyen
Well that's the advertisement…
Lets get down to some details – how to start using emacs

Baby Steps 0

You can start the tutorial with C-h t
Which unforunately wastes about 200 lines saying that C-f C-b C-p C-n will do the work of ↑ ↓ ← →
I suggest you ignore this archaism

Funny Terminology

M-x means Alt-x but then that occasionally doesn't work.
Then you can use ESC instead of M-.
Remember ESC is a prefix Alt is a chord.

Baby Steps 1

Beginning and end of line C-a and C-e
[Home and End also are fine]
Then learn larger scrolling C-v M-v
Page Up and Page Down will also work fine; just that their names are backwards! Go figure!
Learn to stop emacs
  • from Menu→File→Quit
  • C-x C-c
[And dont pay attention to people who tell you menus are for sissies! Keyboard shortcuts are for he-men etc. The menus often tell the shortcuts which you can pick up in due course]

Cut Paste

Emacs of course has cut-paste; its there on the edit-menu as expected; but they're bound to weird keys!
Emacs also allows more usual keys (called cua-mode) if you do M-x cua-mode It would be nice if this worked but it is not reliable.
My suggestion: Try out cua-mode. Then Avoid!
Instead just learn this table and get on
keys Usual (cua) Emacs
Copy C-c M-w
Cut C-x C-w
Paste C-v C-y
Undo C-z C-_ (ie C-Shift-_)
or C-/ or C-x u
Select all C-a C-x h
All these are on the edit-menu as expected
Many more here

Windows

Funny Terminology
Rest of World Emacs
Window Frame
Pane Window
Play around with windows (in emacs' sense!) with
File → New Window Below [Horizontal cut]
File → New Window Right [Vertical Cut]
File → Remove other windows [Expand this window to fill whole frame]

Do the same with the keys C-x 2 C-x 3 C-x 1
[Also C-x 0 which is not on any menu to remove a window]

Frames

Similar to above – try new frame and delete frame
Also try open file in other window/frame C-x 4 f and C-x 5 f
[Not on any menu by default]

Buffers

With either windows or frames above notice that after splitting a window into 2, their contents are SAME. Thats called a bufferan important emacs concept
Play around with commands in menu→buffers
Important to understand that emacs doesn't so much edit files like other editors as copy files into buffers (C-x C-f) and copy buffers into files (C-x C-s)

Does it make a difference??

Not really with files But emacs buffer concept is much more general than files
Notice that if you start emacs without any file you will already see a Messages and a scratch buffer.
Switch to scratch buffer and type (message "Hello World") Now type M-C-x (ie either the chord Alt-C-x or the sequence ESC C-x)
You should see "Hello World" at the bottom This means that the scratch buffer is really a Lisp interpreter!
A more convenient Lisp interpreter than scratch is M-x ielm (interactive elisp mode). You can try that after you are a little more conversant with basics
See demo here

For "Buffer → List all buffers" (C-x C-b) there is better version You can bind it on C-x C-b by putting this line in your init file
(global-set-key (kbd "C-x C-b") 'electric-buffer-list)

Search, Replace

Emacs' search is on C-s Replace is on M-% (ie Alt-Shift-%)
Try it – feels strange at first – but is more powerful once you are used to it. And its in the Edit menu (in search). So no need to
For searching with regexp use C-M-s (That is ESC ctrl-s)
Search tip: Use C-w while searching to search for the word where point is. [Point is emacsspeak for 'where-the-cursor-is']

Introspection

Emacs is very friendly in telling you about itself.
eg

Keys

What does a key do?
eg To find out what is C-x C-f do Type C-h c
It will ask you Describe key
Type C-x C-f
It will (should!) say find-file
This means that when you press C-x C-f emacs actually calls the interactive command find-file
Can we find out more about find-file? Yes…

Commands (Functions)

C-h f find-file will tell you more (maybe too much!) about find-file

Two in One

Do the above two in one keystroke with C-h k followed by C-x C-f

Prefix Keys

Some keys eg C-x is a prefix for many others eg C-x C-f
Want to find out what ALL keys start with C-x?
Do C-x C-h
All these (and more) can be found in Menu → Help → Describe

Variables

What is the meaning/use of say inhibit-startup-message ?
C-h v Then type inhibit-startup-message (tab expansion works! Get used to using it!!)

Docs in General

You can study more about emacs with info C-h i
After that give emacs or elisp
Though it may help to spend 10 minutes studying info itself with M-: (info "(info)Help")
ie type or copy-paste the text «(info "(info)Help")» after typing M-:

Something Funny Happened…

What did I type that made that happen?
Figure out with C-h l

Appearance

There are different thing people like to do to improve emacs' appearance startup/flash message I put the following to make emacs more bare and less noisy on startup
(scroll-bar-mode 0)
(tool-bar-mode 0)
(setq inhibit-startup-message t)
(setq initial-scratch-message nil)
(fringe-mode 3)
Earlier there were themes – install package color-theme; do M-x load-theme and select. Ive not had so much luck with them. Nowadays customthemes seem to be more popular.
More lightweight is to try start emacs with
 $ emacs --reverse-video

Shell Replacements

Emacs is my OS and Linux is its bootloader
[Some ppl replace bootloader by device driver ]
There are many things that can be done from the shell that can be done from emacs. Emacs fanatics will say they are all better from inside emacs – don't mind them – but still good to know some of these functionalities. I'll list a few below with my opinion about whether its so-so ok good

man

Man pages can be called with M-x man – or M-x woman (!!)
good

info

Like man
good

grep

so-so – too many alternatives – badly documented
See this youtube tut

compile

M-x compile then back up over the make -k that appears and type gcc yourfile. For larger projects of course make is more normal.
good – gives IDE like tightness for errors (for C/C++ at least)

dired

Functionality
Like combo of ls/cp/mv/chmod etc
Powerful
but hard to learn
Start
M-x dired
Stop
q
Cute
wdired mode That tut does not say that in dired Menu → Immediate → Edit File names will switch to wdired mode

Sunrise Commander

More powerful than dired and harder :-)

magit

When using git+emacs people love magit.
However basic git stuff can be also activated through C-x v v
Suggestion 1
You become a better programmer if you get used to using something like git early
Suggestion 2
Learning too many things together can slow you down. So start off with git from command-line + emacs if you are new to both. Switch to magit when you become comfortable.

Calendar

Start
M-x calendar
Stop
q

Calculator

Start
M-x calculator
Stop
q
Help
C-h f calculator-mode

Programming Language Support

Generally programming scale is divided three levels:
  1. Programming in the small
  2. Programming in the medium
  3. Programming in the large
Of these the third is usually called software engineering and is more a managerial than technical problem. [In any case neither is emacs much good at it; nor I know much about it! And if it seriously interests you I suggest you concentrate on an MBA not a CS education since most million+ line projects are managed by managers who can’t code!]
OTOH it is very useful to split 1. into tiny and small. This gives us: Programming in the tiny; in the small; and in the medium.

In the small

Navigation
Chars, Lines, Scrolling, Navigating parentheses Sexp, Beg/End-of-buffer
Indentation
Basics, C
Snippets
Most popular Yasnippet Many Others

In the medium

Tagging + Browsing

Folding

Debugging

  1. gdb runs under emacs: M-x gdb
  2. It also runs IDE-like ie gdb-many-windows
  3. Plain ol (shell) gdb
I recommend you try them in the order 3→1→2

Warning

Setting up man of the above can make you WEEP; Practically may be wiser to stay with eclipse/pycharm etc.
But before that remember…

Programming in the tiny

A thousand line program is made up of one thousand one-lines !
How were they written??
People regard one-liners as unimportant at best; or terrible. They are in the same category as people who think that children should not play with toys because it spoils a child's education!
Programming in the tiny is about

One-liners

Most famous in APL; also shell, perl

Playing in the Repl

Using introspection for (self)discovery

In python for example this means: dir, type, help etc on some random object

Particular languages

C/C++

Start with M-: (info "(ccmode) Commands")
Then look around more broadly M-: (info "(ccmode) Top")

Interpreted languages

Python, Haskell, Gofer, Sml, Scheme, Clojure etc and elisp (of course) have excellent support.
This support is one of the BIG reasons for using emacs.
For example:
Python
M-x run-python Then try from this
Earlier I used to recommend to not use the python mode that comes with emacs but to use the one by the python-devs. Nowadays the native/builtin one seems to be mostly fine.
Haskell
M-x run-haskell
Elisp
M-x ielm

Java Support?

Bad!
Dont bother wasting your time!
OTOH Ive written C# code on linux! Usefully!

For Vim users

A table I made of common patterns that vi users expect
Also look at evil

Ergonomics

Steve Yegge's page on effective emacs (below) is old but worth reading.
Some of his suggestions – eg to use Xmodmap to change keyboard settings are obsolete. If you really want to do this look at:

Programming

Emacs is famous for being programmable. There are many aspects to this:
  • You can change a key to do something else with global-set-key
  • You can write lisp – powerful but a bit hard
  • Between programming lisp and just living with whatever are Macros
  • You can also configure modes and packages – Next section

Packages and Configuration

Of late emacs has started having packages somewhat like ubuntu's apt. Its still immature and doesn't quite always work. [And I dont know it very well 😆]
Ideally, like debian/ubuntu we should have an apt-get that manages all package-stuff. The closest we have to that is req-package. But Ive not managed to get that to work satisfactorily. So next best is the less ambitious but more (currently) widely used Use Package
Also packages need to be customized. This is what Customization is for:
  • (info "(emacs) Customization")
  • (info "(elisp) Customization")
Problem is that most experienced emacs users accept that emacs' customize facility is a disaster.
So the basic questions are
  1. How do we manage to keep customize out of our way?
  2. How do we benefit from use-package

Taming Customize

Firstly what is customize good for?
  1. Fonts faces etc are better customized than setq-ed
  2. For large packages like org browsing the options with customize is educative – as long as you browse and not actually customize
Towards keeping it out of our hair Put
(setq custom-file "~/.emacs.d/custom-file.el")
(load custom-file)
in your init.el file, then emacs will put customize output into ~/.emacs.d/custom-file.el and leave your handwritten options in ~/.emacs.d/init.el
Note if you have no customize settings just make an empty ~/.emacs.d/custom-file.el else emacs barfs on the load above.

Use-package

Our conundrum is that use-package is what want to use to manage packages.
However it is itself an (uninstalled by default) package!!
So how to bootstrap the system? Start your init with:
(setq custom-file "~/.emacs.d/setups/custom-file.el")
(load custom-file)

;; Bootstrap package system!
(package-initialize)

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(setq load-prefer-newer t)
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(require 'use-package)
After this use-package is available and customize is (somewhat) out of the way.
Whats the setups dir for? Well I like to have more elisp setup files than just one ~/.emacs.d/init.el. And emacs minds –dont ask why!– having these directly in .emacs.d but is ok with having them in a subdirectory.

Desiderata

What is good about use-package? Of course firstly everything about a package is in one place.
In addition the following are considered undesirable in an emacs config
  1. (require 'something) Equivalently (load "somefile") etc because these slow down startup when there are too many. This is cured by
  2. autoload – but getting autoloads right is very hard
  3. Also eval-after-load is a sign of a poor setup
  4. Need to put setup code into hooks
Use-package helps to do away with all these

Usage

When someone says use (require 'foobar) replace it with (use-package foobar)
If then they say you need to customize the variable (setq foo-var 42) before the (require ...) , add this to your init:
(use-package foobar
  :init
  (setq foo-var 42))
If the instruction is to add it after, do:
(use-package foobar
  :config
  (setq foo-var 42))
In short code that is to be done before the require/load goes in the :init block. Code that goes after goes in the :config block
More on the Use Package docs Also Sacha Chua is a famous emacser and has some good demo uses of use-package in her init

packages to look at

Helm

Getting more and more popular nowadays. I dont like it so much

Projectile

undo-tree

Links/Tuts

Some older collections of suggestions of mine

Org Mode

Org Mode's author's presentation at google

C programming

shows autocomplete and yasnippet

Lifetime Editor

someone who'se spent his last 30 years inside emacs [mostly advertisement]

steve yegge's effective emacs

Some stuff I had written for students

On emacs
I particularly recommend the emacs as default suggestions for people who are used to open-file-by-clicking (ie Windows) mode

Part of other (non-emacs) suggestions

No comments:

Post a Comment