SLIDE 1 AOS Linux Tutorial
Editors: Emacs and Vim Michael Havas
- Dept. of Atmospheric and Oceanic Sciences
McGill University October 5, 2010
SLIDE 2
Outline
1 Text Editors 2 VIM 3 EMACS 4 Next Time
SLIDE 3
Text Editors
Why text?
Everything is text!
Code. Email. The web. Configuration files. Papers.
SLIDE 4
Text Editors
A Full Hour?
Only an hour? Fancy text editors:
Move code around. Help you write code faster. Reduce errors. Integrate with other tools. Have a steep learning curve with great benefits.
SLIDE 5
Text Editors
Plan Text or Word Processors
A plain text file is represented and edited by showing all the characters as they are present in the file. Documents created by a word processor generally contain fileformat-specific control characters. Don’t use MS Word or OO Writer to edit plain text files.
SLIDE 6
Text Editors
Features
Search and replace. Cut, copy and paste. Undo and redo. Syntax highlighting. Clever automatic indentation. Jump to definitions of functions. Code folding.
SLIDE 7
Text Editors
Tips
Use search tool instead of moving the cursor. Don’t use the mouse. Be one with the editor. Learn the basics of regular expressions for search and replace. Integrate your editor with your tools. Find a plugin for your specific language.
SLIDE 8
Outline
1 Text Editors 2 VIM 3 EMACS 4 Next Time
SLIDE 9
VIM
What’s a vim?
An extension to Bill Joy’s VI text editor written by Bram Moolenaar in 1991.
Benefits?
KISS. Small in size. Fast. Runs everywhere. Is everywhere.
SLIDE 10
VIM
Modes
Vim has multiple modes: Normal-Mode For entering keyboard-shortcuts to manipulate text. Insert-Mode For typing text into the text file. Command-Line-Mode For issuing a command via the command-line. Prefixed by :. Always be in Normal-Mode. Esc goes to Normal-Mode
SLIDE 11
VIM
Quitting
Quitting :q Quit. :q! Quit and don’t warn me about saving. :wq Save and quit.
SLIDE 12
VIM
Moving the Cursor
Character-by-Character h Left. j Down. k Up. l Right. By more than one character e End of word. b Beginning of word. w Next word. :n Go to line n.
SLIDE 13
VIM
Inserting Text
Steps Move your cursor. Press i to enter insert-mode. Type text. Press Esc to go back into normal-mode. More advanced Replacing i with the following leads to more advanced techniques: Press a to insert text after the cursor (append). Press I to insert at the beginning of a line. Press A to append at the end of a line.
SLIDE 14
VIM
Deleting Text
Deleting Text x Delete character. dw Delete word. dd Delete the whole line. d$ Delete until the end of the line. ˆ Delete until the beginning of the line. dap Delete a paragraph. dtX Deletes to the character ’X’. di” Deletes content inside quotes. di) Deletes content inside parentheses.
SLIDE 15
VIM
Operators, motions and counts
Most operations can apply on motions. Ex: d (delete) can apply on motion: w Word. $ End of line. ˆ Beginning of line. ap A paragraph. tX to character X. i) In parentheses. i> In angled brackets. i” In quotes. Operators can also take a numerical argument. Ex: d5w deletes five words.
SLIDE 16
VIM
Undo, Redo and Repeat
Undo and Redo u Undo U Undo changes on whole line. Ctrl-R Redo. :earlier 10m Go back in time 10 minutes. :later 10m Go forward in time 10 minutes. Repeat . Repeats the last command. Really useful!
SLIDE 17
VIM
Visual-Mode
Visual mode v Enter visual mode. V Enter visual mode selecting rows. Ctrl-v Enter visual mode selecting columns. Operators can act on selected text
SLIDE 18
VIM
Cut, Copy, Paste
y Yank (Copy). d Delete (Cut). p Put (Paste). P Put above.
SLIDE 19
VIM
Changing Text
Command: c Deletes motion and puts vim in insert mode.
SLIDE 20
VIM
Search
Searching / Search forward. ? Search backward. * Search for word under cursor. n Repeat search. Use regular expressions. Examples of regular expressions Some examples: ˆabc Word “abc” at the beginning of the line. abc$ Word “abc” at the end of a line. abc.* Word “abc” followed by any number of characters.
SLIDE 21
VIM
Search
Searching / Search forward. ? Search backward. * Search for word under cursor. n Repeat search. Use regular expressions. Examples of regular expressions Some examples: ˆabc Word “abc” at the beginning of the line. abc$ Word “abc” at the end of a line. abc.* Word “abc” followed by any number of characters.
SLIDE 22
VIM
Substitution
Substitution :s/blah/foo Substitute first occurrence of blah for foo on the line. :s/blah/foo/g Substitute every occurrence of blah for foo on the line. :%s/blah/foo/ Substitute ever occurrence of blah for foo in the file. Useful with visual-mode. Use regular expressions.
SLIDE 23
VIM
Getting Help
:help Open the help window. :help command Get help on command.
SLIDE 24 VIM
Configuration
Configuration file: ∼/.vimrc:
""" General """ s e t nocompatible "get out of vi-compatible mode filetype on " detect the type
f i l e s e t h i s t o r y =1000 " lines of history to remember filetype plugin on " load f i l e t y p e p l u g i n s s e t showmatch " Show matching parentheses.
Configuration directory: ∼/.vim/:
Setup backup directories here. Install your vim plugins here.
SLIDE 25
VIM
Useful Plugins
vim-latex Makes editing L
AT
EX a breeze. http://vim-latex.sourceforge.net/ Surround Provides mappings to easily delete, change and add such surroundings in pairs. http://www.vim.org/scripts/script.php? script_id=1697 Snipmate Provides text-mate like snippets to vim. http://www.vim.org/scripts/script.php? script_id=2540
SLIDE 26
Outline
1 Text Editors 2 VIM 3 EMACS 4 Next Time
SLIDE 27
EMACS
What’s an EMACS? A text editor which began development in the mid-70s whose development continues today originally as Editor MACros. What are its benefits? Everything at reach. Larger set of features than vi. Extensible and customizable. Can emulate vi.
SLIDE 28
EMACS
Preliminaries
Emacs does not have vim-like modes. Emacs has lots of keybindings: Control (C) The control key. Meta (M) The alt key.
SLIDE 29
EMACS
Moving Around
Character-by-Character C-f Forward. C-b Back. C-p Previous line. C-n Next line. Word-by-Word M-f Forward one word. M-b Back one word.
SLIDE 30
EMACS
Moving Around
Line-by-Line C-a Beginning of line. C-e End of line. Sentence-by-Sentence M-a Beginning of a sentence. M-e End of a sentence. Page-by-Page C-v Page forward. M-v Page backward.
SLIDE 31
EMACS
Numeric/Prefix Arguments
Most Emacs commands can take alternate or numeric arguments. For example: C-u 4 * Insert 4 “*”s. C-u 4 M-f Move forward 4 words. C-4 M-f Shortcut: Move forward 4 words. C-u M-f Shortcut: Move forward 4 words.
SLIDE 32
EMACS
Terminology
Frame Similar to the concept of a Window in regular graphical user interfaces. Window Sections of the frame that display buffers. Buffer Contains text normally representing a file. Mini-Buffer The last line of emacs designated for entering commands.
SLIDE 33
EMACS
File Operations
File Operations C-x C-s Save file. C-x s Prompt to save each buffer. C-x C-f Find/Open file. C-x C-c Quit.
SLIDE 34
EMACS
Window Operations
Window Operations C-x 3 Split the current window vertically. C-x 2 Split the current window horizontally. C-x 1 Close all windows but current. C-x o Go to next window. C-M-v Move down a page in the other window. C-M-V Move up a page in the other window.
SLIDE 35
EMACS
When Things Go Bad
If emacs becomes unresponsive C-g C-g C-g Or, if you are in recursive-editing mode Esc Esc Esc
SLIDE 36
EMACS
Deleting and Killing
Deleting does not copy text to kill-ring C-d Delete character. M-d Delete word. M-backspace Delete word backwards. Kill is like cut C-k Kill from point to end of line. M-k Kill from point to end of sentence. Highlight copy/cut Set mark by C-spc. Move point. To cut: C-w To copy: M-w
SLIDE 37
EMACS
Yanking Text and Undo
Yanking text All killed text is placed in the kill-ring. To yank/paste back most recent kill: C-y To yank back earlier kill: M-y Undo C- is undo.
SLIDE 38
EMACS
Buffer Management
A window can switch the buffer it is displaying. C-b Switch buffer. C-x b List buffers. C-x k Close buffer.
SLIDE 39
EMACS
Search and Replace
Search C-s Search forward. C-r Search in reverse. Replace M-% Query-Replace String. Recursive Editing While query-replacing string, C-r to enter a recursive edit. Do work. Leave the recursive edit using C-M-c.
SLIDE 40
EMACS
Modes
Modes affect the operation of emacs. major-mode Only one major-mode can be active at a time. minor-mode Many minor-modes may be active at once. Can change modes using M-x mode.
SLIDE 41 EMACS
Useful Modes
Minor Modes: flyspell Check spelling on-the-fly auto-fill Automatic indenting. Major Modes:
- rg-mode An organizational mode (AMAZING!)
latex-mode A mode for writing L
AT
EX. http://www.gnu.org/software/auctex/ dired A mode for file management. gnus An email/news/rss client. fortran-mode A mode for fortran. To find out more information about a mode, switch to the mode and hit M-x describe-mode.
SLIDE 42
EMACS
Getting Help
Help! C-h Main help. Use ? for all options. C-h c Quick info on a shortcut key. Displays in minibuffer. C-h k In depth info on a shortcut key. C-h f In depth info on a function. C-h a Looks up all command names that contain keyword. Online! http://www.emacswiki.org/
SLIDE 43
EMACS
Configuration
Configuration is done in elisp programming language.
; ; Display the menu bar f o r now (menu−bar−mode t ) ; ; Disable the v i s u a l and a u d i b l e b e l l ( setq ring−bell−function ’ i g n o r e ) ; ; Choose my c o l o r theme ( color−theme−blackboard ) ; ; CUA−mode only f o r r e c t a n g l e s ( setq cua−enable−cua−keys n i l ) (cua−mode)
Sensible configuration: http://github.com/technomancy/emacs-starter-kit
g i t p u l l g i t :// github . com/technomancy/emacs−s t a r t e r −k
SLIDE 44
EMACS
EMACS as an OS
M-x calendar M-x calc FTP and SSH using tramp and dired M-x list-colors-display eshell. elisp evaluation. IRC using M-x erc. Mail and News using M-x gnus M-x tetris M-x snake M-x butterfly M-x pong M-x morse-region: Convert region to morse-code. M-x zone A physchoanalyst: M-x doctor
SLIDE 45
EMACS
EMACS as an OS
SLIDE 46
Outline
1 Text Editors 2 VIM 3 EMACS 4 Next Time
SLIDE 47
Next Time
L
AT
EX BibTex