AOS Linux Tutorial Editors: Emacs and Vim Michael Havas Dept. of Atmospheric and Oceanic Sciences McGill University October 5, 2010
Outline 1 Text Editors 2 VIM 3 EMACS 4 Next Time
Text Editors Why text? Everything is text! Code. Email. The web. Configuration files. Papers.
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.
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.
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.
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.
Outline 1 Text Editors 2 VIM 3 EMACS 4 Next Time
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.
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
VIM Quitting Quitting :q Quit. :q! Quit and don’t warn me about saving. :wq Save and quit.
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 .
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.
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.
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.
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!
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
VIM Cut, Copy, Paste y Yank (Copy). d Delete (Cut). p Put (Paste). P Put above.
VIM Changing Text Command: c Deletes motion and puts vim in insert mode.
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.
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.
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.
VIM Getting Help :help Open the help window. :help command Get help on command .
VIM Configuration Configuration file: ∼ /.vimrc : """ General """ s e t nocompatible "get out of vi-compatible mode filetype on " detect the type of 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.
VIM Useful Plugins vim-latex Makes editing L A T 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
Outline 1 Text Editors 2 VIM 3 EMACS 4 Next Time
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.
EMACS Preliminaries Emacs does not have vim-like modes. Emacs has lots of keybindings: Control (C) The control key. Meta (M) The alt key.
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.
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.
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.
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.
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.
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.
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
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
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.
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.
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 .
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 .
EMACS Useful Modes Minor Modes: flyspell Check spelling on-the-fly auto-fill Automatic indenting. Major Modes: org-mode An organizational mode (AMAZING!) latex-mode A mode for writing L A T 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 .
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/
Recommend
More recommend