Anindya Sen - IIT Bombayanindya/tutorials/vim_tut.pdf · 2014. 9. 22. · Vim Anindya Sen Autumn...

Post on 14-Mar-2021

9 views 0 download

Transcript of Anindya Sen - IIT Bombayanindya/tutorials/vim_tut.pdf · 2014. 9. 22. · Vim Anindya Sen Autumn...

Vim

Anindya Sen

Autumn 2011

October 19, 2011

Anindya (IITB) Vim October 19, 2011 1 / 19

Why text editor? Motivation!!

Information access in a hurry

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modification

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

1 pdf 137 Kb (129 Kb zipped)

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

1 pdf 137 Kb (129 Kb zipped)2 doc 96 Kb (21 Kb zipped)

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

1 pdf 137 Kb (129 Kb zipped)2 doc 96 Kb (21 Kb zipped)3 txt 65 Kb (17 Kb zipped)

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

1 pdf 137 Kb (129 Kb zipped)2 doc 96 Kb (21 Kb zipped)3 txt 65 Kb (17 Kb zipped)

Ease of indexing

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

1 pdf 137 Kb (129 Kb zipped)2 doc 96 Kb (21 Kb zipped)3 txt 65 Kb (17 Kb zipped)

Ease of indexing

Syntax highlighting

Anindya (IITB) Vim October 19, 2011 2 / 19

Why text editor? Motivation!!

Information access in a hurry

Ease of concatenation and splitting

True Read/Write Portability

Searchability

Ease of modificationEfficient storage

1 pdf 137 Kb (129 Kb zipped)2 doc 96 Kb (21 Kb zipped)3 txt 65 Kb (17 Kb zipped)

Ease of indexing

Syntax highlighting

Tool of last resort

Anindya (IITB) Vim October 19, 2011 2 / 19

Editing text

You edit lots of text

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source code

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentation

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentationE-mail

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentationE-mailetc.

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentationE-mailetc.

But you don’t have enough time!

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentationE-mailetc.

But you don’t have enough time!

You want to get more work done in less time :-)

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentationE-mailetc.

But you don’t have enough time!

You want to get more work done in less time :-)

Next hour or so will show you how

Anindya (IITB) Vim October 19, 2011 3 / 19

Editing text

You edit lots of text

Program source codeDocumentationE-mailetc.

But you don’t have enough time!

You want to get more work done in less time :-)

Next hour or so will show you how

Selecting a good editor (e.g. vim, emacs) is the first step. In thistalk, we will be focussing on vim!

Anindya (IITB) Vim October 19, 2011 3 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command mode

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of text

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of textDefault mode in which vim starts

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of textDefault mode in which vim startsGet back using the ‘Esc’ key

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of textDefault mode in which vim startsGet back using the ‘Esc’ key

Input/Edit mode

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of textDefault mode in which vim startsGet back using the ‘Esc’ key

Input/Edit modeFor editing text!

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of textDefault mode in which vim startsGet back using the ‘Esc’ key

Input/Edit modeFor editing text!Multiple keys for different tasks e.g. insert (i/I), open(o/O), append(a/A), change(c/C), replace (r/R)

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Modal” editor

Vim is a finite automaton, with states

Command modeFor navigation and manipulation of textDefault mode in which vim startsGet back using the ‘Esc’ key

Input/Edit modeFor editing text!Multiple keys for different tasks e.g. insert (i/I), open(o/O), append(a/A), change(c/C), replace (r/R)Works just like a “normal” text editor :-)

Anindya (IITB) Vim October 19, 2011 4 / 19

A “Moded” editor (Cont . . . )

Command-line mode

Anindya (IITB) Vim October 19, 2011 5 / 19

A “Moded” editor (Cont . . . )

Command-line modeFor entering editor commands, reading/writing files, exiting vim

Anindya (IITB) Vim October 19, 2011 5 / 19

A “Moded” editor (Cont . . . )

Command-line modeFor entering editor commands, reading/writing files, exiting vimPress ‘:’ to enter

Anindya (IITB) Vim October 19, 2011 5 / 19

A “Moded” editor (Cont . . . )

Command-line modeFor entering editor commands, reading/writing files, exiting vimPress ‘:’ to enter

Visual mode

Anindya (IITB) Vim October 19, 2011 5 / 19

A “Moded” editor (Cont . . . )

Command-line modeFor entering editor commands, reading/writing files, exiting vimPress ‘:’ to enter

Visual modeFor navigation and manipulation of text selections

Anindya (IITB) Vim October 19, 2011 5 / 19

A “Moded” editor (Cont . . . )

Command-line modeFor entering editor commands, reading/writing files, exiting vimPress ‘:’ to enter

Visual modeFor navigation and manipulation of text selectionsUsually enter from command mode by pressing “v”, “Ctrl-V” or“Shift-V”

Anindya (IITB) Vim October 19, 2011 5 / 19

A “Moded” editor (Cont . . . )

Command-line modeFor entering editor commands, reading/writing files, exiting vimPress ‘:’ to enter

Visual modeFor navigation and manipulation of text selectionsUsually enter from command mode by pressing “v”, “Ctrl-V” or“Shift-V”Possible to select columns (block-visual mode)!

Anindya (IITB) Vim October 19, 2011 5 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

T he not so quick brown fox jumps over the lazy dog.

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

The n ot so quick brown fox jumps over the lazy dog.w

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

The q uick brown fox jumps over the lazy dog.2 d w

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

Again : The quick brown fox jumps over the lazy dog.

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

T he not so quick brown fox jumps over the lazy dog.

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

The n ot so quick brown fox jumps over the lazy dog.fn

Anindya (IITB) Vim October 19, 2011 6 / 19

Vim = Count + Command + Motion

Powerful commands can be created out of a combination ofsimple components :

Basic Commands — cut, copy, paste, convert to uppercase etc.Motion — move to beginning/end-of line/paragraph/file, jump to firstoccurrence of character ’f’ in line or word ‘foo’ in file.Count — Number of repetitions of a basic command.

Example

The q uick brown fox jumps over the lazy dog.d fq

Anindya (IITB) Vim October 19, 2011 6 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Switch modes

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Switch modesInsert (i/I)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Switch modesInsert (i/I)Replace/Overwrite (R)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Switch modesInsert (i/I)Replace/Overwrite (R)Append (a/A)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Switch modesInsert (i/I)Replace/Overwrite (R)Append (a/A)Open a new line (o/O)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Operate

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

OperateReplace char (r)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

OperateReplace char (r)Delete (d/D)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

OperateReplace char (r)Delete (d/D)Yank (y/Y)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

OperateReplace char (r)Delete (d/D)Yank (y/Y)Paste (p/P)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Operate & switch modes

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Operate & switch modesReplace and insert (s/S)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Operate & switch modesReplace and insert (s/S)Delete and insert = Change (c/C)

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Some commands are not really basic i.e. they can be composedof simpler commands. Still, they help reduce typing effort

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Some commands are not really basic i.e. they can be composedof simpler commands. Still, they help reduce typing effort

a = l + i

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Some commands are not really basic i.e. they can be composedof simpler commands. Still, they help reduce typing effort

a = l + is = x + i

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Some commands are not really basic i.e. they can be composedof simpler commands. Still, they help reduce typing effort

a = l + is = x + ic =

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Basic Commands

All the commands work only in Command/Escape Mode.

Some commands are not really basic i.e. they can be composedof simpler commands. Still, they help reduce typing effort

a = l + is = x + ic = d + i

Anindya (IITB) Vim October 19, 2011 7 / 19

Review of Motion Commands

Cursor movements

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut on ce the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Cursor movements↑ (k)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Cursor movements↑ (k)↓ (j)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut on ce the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Cursor movements↑ (k)↓ (j)← (h)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut o nce the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Cursor movements↑ (k)↓ (j)← (h)→ (l)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut on ce the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Word movements

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut on ce the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Word movementsForward (w/W)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once t he user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Word movementsForward (w/W)Backward (b/B )

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut o nce the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Word movementsForward (w/W)Backward (b/B )End-of-word (e/E)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Line movements

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Line movementsEnd ($)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Line movementsEnd ($)Beginning (/̂ 0)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallyb ut once the user gets a grasp of the basics they progress quickly and their

editing becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Line movementsEnd ($)Beginning (/̂ 0)Land on char ‘p’ (f/F p )

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Line movementsEnd ($)Beginning (/̂ 0)Land on char ‘p’ (f/F p )Land just before char ‘c’ (t/T c )

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basi cs they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Sentence movements

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basi cs they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Sentence movementsBeginning of sentence (()

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.V im is said to have a steep learning curve, meaning learning is slow initially

but once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

Sentence movementsBeginning of sentence (()Beginning of next sentence ())

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. T o facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

File movements

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. T o facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

File movementsBeginning of file (gg )

Example

L earning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

File movementsBeginning of file (gg )End of file (G)

Example

Learning to use VimVim, which stands for Vi IMproved, is an open-source, multiplatform texteditor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

File movementsBeginning of file (gg )End of file (G)Line number 2 in file (2 G)

Example

Learning to use VimV im, which stands for Vi IMproved, is an open-source, multiplatform text

editor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Review of Motion Commands

and many more . . .

Moving to a mark ‘a’ in file (’a)Moving to a matching brace (%)Beginning/End of paragraph ({/} )etc.

Example

Learning to use VimV im, which stands for Vi IMproved, is an open-source, multiplatform text

editor extended from vi. It was first released by Bram Moolenaar in 1991.Vim is said to have a steep learning curve, meaning learning is slow initiallybut once the user gets a grasp of the basics they progress quickly and theirediting becomes more efficient. To facilitate this there’s the vim tutorial forbeginners, usually invoked by typing “vimtutor” on the Unix command line.- Vim on Wikipedia

Anindya (IITB) Vim October 19, 2011 8 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete a word?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete a word?dw

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Two words?OR

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Two words?2dw OR

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Two words?2dw OR d2w

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete till end of line?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete till end of line?d$

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete till end of file?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete till end of file?dG

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete all text till the first occurrence of ‘foo’?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete all text till the first occurrence of ‘foo’?d/foo

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete the parameters in “void_func(a,b,c,d)”?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete the parameters in “void_func(a,b,c,d)”?fa

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Delete the parameters in “void_func(a,b,c,d)”?fa dt)

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Yank 8 lines of text?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Yank 8 lines of text?8yy

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

And paste two copies of the same text at the end of file?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

And paste two copies of the same text at the end of file?G

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

And paste two copies of the same text at the end of file?G 2p

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Yank 8 lines of text above the current line?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Yank 8 lines of text above the current line?7yk

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Indenting (=) a block of code?

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Indenting (=) a block of code?f{

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Indenting (=) a block of code?f{ =%

Anindya (IITB) Vim October 19, 2011 9 / 19

Vim = Count + Command + Motion

Let’s play around with vim’s capabilities :

Example

Indenting (=) a block of code?f{ =%

Notes

Viewing a piece of text in terms of its constituent logical units rather asan ensemble of characters on screen

Anindya (IITB) Vim October 19, 2011 9 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire file

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire fileN1,N2 — changes restricted to lines between N1 and N2, bothinclusive. In visual mode, N1 and N2 are auto-generated from thestart and end of selection

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire fileN1,N2 — changes restricted to lines between N1 and N2, bothinclusive. In visual mode, N1 and N2 are auto-generated from thestart and end of selection

Flags

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire fileN1,N2 — changes restricted to lines between N1 and N2, bothinclusive. In visual mode, N1 and N2 are auto-generated from thestart and end of selection

Flags

No flag — change first occurrence in current line

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire fileN1,N2 — changes restricted to lines between N1 and N2, bothinclusive. In visual mode, N1 and N2 are auto-generated from thestart and end of selection

Flags

No flag — change first occurrence in current lineg — changes all occurrences in current line

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire fileN1,N2 — changes restricted to lines between N1 and N2, bothinclusive. In visual mode, N1 and N2 are auto-generated from thestart and end of selection

Flags

No flag — change first occurrence in current lineg — changes all occurrences in current linec — confirm each change before committing

Anindya (IITB) Vim October 19, 2011 10 / 19

Replacing text

Ex command : :[range]s/< old>/<new>/[flags] to replace text

Range

% — entire fileN1,N2 — changes restricted to lines between N1 and N2, bothinclusive. In visual mode, N1 and N2 are auto-generated from thestart and end of selection

Flags

No flag — change first occurrence in current lineg — changes all occurrences in current linec — confirm each change before committingi/I — Ignore / Don’t ignore case for this pattern

Anindya (IITB) Vim October 19, 2011 10 / 19

Vim — A Programmer’s Delight

Warning

Vim is addictive. Use it at your own risk!

Anindya (IITB) Vim October 19, 2011 11 / 19

Vim — A Programmer’s Delight

A vast collection of built-in tools (word completions, indentation,etc.) as well as interfaces to well known tools (make, grep etc.).

Anindya (IITB) Vim October 19, 2011 12 / 19

Vim — A Programmer’s Delight

A vast collection of built-in tools (word completions, indentation,etc.) as well as interfaces to well known tools (make, grep etc.).

Programming Language Support — For all major languages suchas C*, Java, Perl, Python, Tcl, Ruby, PHP and many more.

Anindya (IITB) Vim October 19, 2011 12 / 19

Vim — A Programmer’s Delight

A vast collection of built-in tools (word completions, indentation,etc.) as well as interfaces to well known tools (make, grep etc.).

Programming Language Support — For all major languages suchas C*, Java, Perl, Python, Tcl, Ruby, PHP and many more.

Syntax Highlighting

Anindya (IITB) Vim October 19, 2011 12 / 19

Vim — A Programmer’s Delight

A vast collection of built-in tools (word completions, indentation,etc.) as well as interfaces to well known tools (make, grep etc.).

Programming Language Support — For all major languages suchas C*, Java, Perl, Python, Tcl, Ruby, PHP and many more.

Syntax HighlightingSmart Indentation

Anindya (IITB) Vim October 19, 2011 12 / 19

Vim — A Programmer’s Delight

A vast collection of built-in tools (word completions, indentation,etc.) as well as interfaces to well known tools (make, grep etc.).

Programming Language Support — For all major languages suchas C*, Java, Perl, Python, Tcl, Ruby, PHP and many more.

Syntax HighlightingSmart IndentationFor new languages, its possible to write a script for specifyingkeywords and indentation rules.

Anindya (IITB) Vim October 19, 2011 12 / 19

Vim — A Programmer’s Delight

A vast collection of built-in tools (word completions, indentation,etc.) as well as interfaces to well known tools (make, grep etc.).

Programming Language Support — For all major languages suchas C*, Java, Perl, Python, Tcl, Ruby, PHP and many more.

Syntax HighlightingSmart IndentationFor new languages, its possible to write a script for specifyingkeywords and indentation rules.

Powerful navigation techniques to aid source code comprehensionand editing.

Anindya (IITB) Vim October 19, 2011 12 / 19

Vim — A Programmer’s Delight

Moving around quickly

Search for keyword ‘foo’ — Use *

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Search for keyword ‘foo’ — Use *

Cursor should be “within” foo

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Search for keyword ‘foo’ — Use *

Cursor should be “within” foo∗ = /\ < foo \ >

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Search for keyword ‘foo’ — Use *

Cursor should be “within” foo∗ = /\ < foo \ >Not too useful if many occurrences in file. Use n to cycle throughmatches.

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Search for keyword ‘foo’ — Use *

Cursor should be “within” foo∗ = /\ < foo \ >Not too useful if many occurrences in file. Use n to cycle throughmatches.Use ctags instead

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Go straight to the definition of a function — Use Ctrl-]

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Go straight to the definition of a function — Use Ctrl-]

Need external program called ctags-exuberant

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Go straight to the definition of a function — Use Ctrl-]

Need external program called ctags-exuberantctags stores references to all functions, variables in a tags file

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Go straight to the definition of a function — Use Ctrl-]

Need external program called ctags-exuberantctags stores references to all functions, variables in a tags fileNeed not worry if the function is defined in a different file

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Go straight to the definition of a function — Use Ctrl-]

Need external program called ctags-exuberantctags stores references to all functions, variables in a tags fileNeed not worry if the function is defined in a different file

“But how do I return to where I was earlier?” — Use Ctrl-o andCtrl-i to navigate (back and forth) through the source code. Reallyuseful!

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Switching between files quickly — Use the ex commands

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Switching between files quickly — Use the ex commands

:e <file> opens file in same window.

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Switching between files quickly — Use the ex commands

:e <file> opens file in same window.:e # toggles between previous and current file

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Switching between files quickly — Use the ex commands

:e <file> opens file in same window.:e # toggles between previous and current file:ls gives a list of all buffers

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Switching between files quickly — Use the ex commands

:e <file> opens file in same window.:e # toggles between previous and current file:ls gives a list of all buffers:buf <n> opens buffer number n (Order provided by :ls )

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Moving around quickly

Switching between files quickly — Use the ex commands

:e <file> opens file in same window.:e # toggles between previous and current file:ls gives a list of all buffers:buf <n> opens buffer number n (Order provided by :ls )gf opens filename under cursor

Anindya (IITB) Vim October 19, 2011 13 / 19

Vim — A Programmer’s Delight

Running external commands from within vim

To run a shell command, simply use :!<cmd> . For frequently usedcmds (e.g. cd , make , grep ), drop the !

Anindya (IITB) Vim October 19, 2011 14 / 19

Vim — A Programmer’s Delight

Running external commands from within vim

To run a shell command, simply use :!<cmd> . For frequently usedcmds (e.g. cd , make , grep ), drop the !

Quick fix — Vim’s support for navigating through grep or make ’soutput/error list. Very useful during the code-compile-run cycle

Anindya (IITB) Vim October 19, 2011 14 / 19

Vim — A Programmer’s Delight

Running external commands from within vim

To run a shell command, simply use :!<cmd> . For frequently usedcmds (e.g. cd , make , grep ), drop the !

Quick fix — Vim’s support for navigating through grep or make ’soutput/error list. Very useful during the code-compile-run cycle

Captures output of grep/make in a buffer

Anindya (IITB) Vim October 19, 2011 14 / 19

Vim — A Programmer’s Delight

Running external commands from within vim

To run a shell command, simply use :!<cmd> . For frequently usedcmds (e.g. cd , make , grep ), drop the !

Quick fix — Vim’s support for navigating through grep or make ’soutput/error list. Very useful during the code-compile-run cycle

Captures output of grep/make in a buffer:clist lists all messages

Anindya (IITB) Vim October 19, 2011 14 / 19

Vim — A Programmer’s Delight

Running external commands from within vim

To run a shell command, simply use :!<cmd> . For frequently usedcmds (e.g. cd , make , grep ), drop the !

Quick fix — Vim’s support for navigating through grep or make ’soutput/error list. Very useful during the code-compile-run cycle

Captures output of grep/make in a buffer:clist lists all messages:cnext /:cprev takes you to line in code corres to next/prevmessage (error/match)

Anindya (IITB) Vim October 19, 2011 14 / 19

Vim — A Programmer’s Delight

Running external commands from within vim

To run a shell command, simply use :!<cmd> . For frequently usedcmds (e.g. cd , make , grep ), drop the !

Quick fix — Vim’s support for navigating through grep or make ’soutput/error list. Very useful during the code-compile-run cycle

Captures output of grep/make in a buffer:clist lists all messages:cnext /:cprev takes you to line in code corres to next/prevmessage (error/match):copen shows the buffer in a split window — Hitting enter on anymessage line in that buffer takes you to the line in code corres tothat message.

Anindya (IITB) Vim October 19, 2011 14 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Indentation — Use = to indent a block of code

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Indentation — Use = to indent a block of code

Folding — Automatically “fold” and “unfold” a region of text

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Indentation — Use = to indent a block of code

Folding — Automatically “fold” and “unfold” a region of text

Collapse all procedures other than current one — Use :setfoldmethod=indent

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Indentation — Use = to indent a block of code

Folding — Automatically “fold” and “unfold” a region of text

Collapse all procedures other than current one — Use :setfoldmethod=indentHide commented out code — :set foldmethod=manual /\/\∗ zf%

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Indentation — Use = to indent a block of code

Folding — Automatically “fold” and “unfold” a region of text

Collapse all procedures other than current one — Use :setfoldmethod=indentHide commented out code — :set foldmethod=manual /\/\∗ zf%

Man Page Lookup — Use K to view the manpage of the wordunder cursor

Anindya (IITB) Vim October 19, 2011 15 / 19

Vim — A Programmer’s Delight

Other programming essentials

Autocompletion — Use Ctrl-n & Ctrl-p to cycle through theoptions→Works only in Insert mode

Don’t type “this_is_a_long_procedure_name( )” more than once!

Indentation — Use = to indent a block of code

Folding — Automatically “fold” and “unfold” a region of text

Collapse all procedures other than current one — Use :setfoldmethod=indentHide commented out code — :set foldmethod=manual /\/\∗ zf%

Man Page Lookup — Use K to view the manpage of the wordunder cursor

Vimdiff — To view the output of diff

Anindya (IITB) Vim October 19, 2011 15 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

Anindya (IITB) Vim October 19, 2011 16 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

set incsearch — No need to type the entire search string. Canalso highlight search term using set hlsearch

Anindya (IITB) Vim October 19, 2011 16 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

set incsearch — No need to type the entire search string. Canalso highlight search term using set hlsearch

set ignorecase — No need to use /[Ww]or[dD]

Anindya (IITB) Vim October 19, 2011 16 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

set incsearch — No need to type the entire search string. Canalso highlight search term using set hlsearch

set ignorecase — No need to use /[Ww]or[dD]

But what if I really want to search for WorD and not the others? —

Anindya (IITB) Vim October 19, 2011 16 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

set incsearch — No need to type the entire search string. Canalso highlight search term using set hlsearch

set ignorecase — No need to use /[Ww]or[dD]

But what if I really want to search for WorD and not the others? —Just add set smartcase to your vimrc!

Anindya (IITB) Vim October 19, 2011 16 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

set incsearch — No need to type the entire search string. Canalso highlight search term using set hlsearch

set ignorecase — No need to use /[Ww]or[dD]

But what if I really want to search for WorD and not the others? —Just add set smartcase to your vimrc!

set expandtabs — Tab-presses are replaced by N spaces, whereN is set using set tabstop = N

Anindya (IITB) Vim October 19, 2011 16 / 19

vimrc

RC file for setting options globally. Read by Vim during startup

set background = dark/light — Vim chooses foreground colorsto match the background

set incsearch — No need to type the entire search string. Canalso highlight search term using set hlsearch

set ignorecase — No need to use /[Ww]or[dD]

But what if I really want to search for WorD and not the others? —Just add set smartcase to your vimrc!

set expandtabs — Tab-presses are replaced by N spaces, whereN is set using set tabstop = N

Doesn’t work with make! — Need to handle makefiles separately

Anindya (IITB) Vim October 19, 2011 16 / 19

These are a few of my favorite commands

I use(d) vim for :

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

I use(d) vim for :

Preparing this presentation (using Latex packages)!

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

I use(d) vim for :

Preparing this presentation (using Latex packages)!

Editing source code (C,C++,Perl,Tcl)

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

I use(d) vim for :

Preparing this presentation (using Latex packages)!

Editing source code (C,C++,Perl,Tcl)

Writing e-mails (vim and mutt work together nicely)

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

I use(d) vim for :

Preparing this presentation (using Latex packages)!

Editing source code (C,C++,Perl,Tcl)

Writing e-mails (vim and mutt work together nicely)

Editing bash commands on-the-fly (set -o vi )

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

I use(d) vim for :

Preparing this presentation (using Latex packages)!

Editing source code (C,C++,Perl,Tcl)

Writing e-mails (vim and mutt work together nicely)

Editing bash commands on-the-fly (set -o vi )

Editing unix configuration files

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

I use(d) vim for :

Preparing this presentation (using Latex packages)!

Editing source code (C,C++,Perl,Tcl)

Writing e-mails (vim and mutt work together nicely)

Editing bash commands on-the-fly (set -o vi )

Editing unix configuration files

and many more day-to-day editing tasks . . .

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:r <buffer> — Read the contents of a buffer at the location ofcursor.

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:r <buffer> — Read the contents of a buffer at the location ofcursor.

G :r <filename> will append contents of <filename> to current file.

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:r <buffer> — Read the contents of a buffer at the location ofcursor.

G :r <filename> will append contents of <filename> to current file.Combine :r with :! (external command execution) e.g. :r!dateinserts current date

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:r <buffer> — Read the contents of a buffer at the location ofcursor.

G :r <filename> will append contents of <filename> to current file.Combine :r with :! (external command execution) e.g. :r!dateinserts current date

Combine selection of a region of text in Visual mode with :! to runexternal commands on that region

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:r <buffer> — Read the contents of a buffer at the location ofcursor.

G :r <filename> will append contents of <filename> to current file.Combine :r with :! (external command execution) e.g. :r!dateinserts current date

Combine selection of a region of text in Visual mode with :! to runexternal commands on that region

:’<,’>!sort to sort contents in place

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:r <buffer> — Read the contents of a buffer at the location ofcursor.

G :r <filename> will append contents of <filename> to current file.Combine :r with :! (external command execution) e.g. :r!dateinserts current date

Combine selection of a region of text in Visual mode with :! to runexternal commands on that region

:’<,’>!sort to sort contents in place:’<,’>!wc to count number of characters/words in a single paragraph

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:abbrev <sh> <long string here> — saves ty(ping) and correctsspelling mistaeks mistakes. Can be added to .vimrc file too

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:abbrev <sh> <long string here> — saves ty(ping) and correctsspelling mistaeks mistakes. Can be added to .vimrc file too

How to underline a long line of text ? — use yy p :s/./-/g

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:abbrev <sh> <long string here> — saves ty(ping) and correctsspelling mistaeks mistakes. Can be added to .vimrc file too

How to underline a long line of text ? — use yy p :s/./-/g

Repeat previous change cmd using . (dot)

Anindya (IITB) Vim October 19, 2011 17 / 19

These are a few of my favorite commands

:abbrev <sh> <long string here> — saves ty(ping) and correctsspelling mistaeks mistakes. Can be added to .vimrc file too

How to underline a long line of text ? — use yy p :s/./-/g

Repeat previous change cmd using . (dot)

Repeat line movement (f/F/t/T ) using ; (semi-colon) or , (comma)

Anindya (IITB) Vim October 19, 2011 17 / 19

Vim Capability Improvement Program (VIMCIP) :-)

Anindya (IITB) Vim October 19, 2011 18 / 19

Vim Capability Improvement Program

“vimtutor ” on bash prompt — a gentle introduction to Vim

“Seven habits of effective text editing”(http://www.moolenaar.net/habits.pdf)

:h [topic] provides on-line help. Navigation is very easy usingtags and Ctrl-o / Ctrl-i cmds

Vimbook (http://ntu.csie.org/ piaip/vim/vimbook-OPL.pdf)

“Mastering Regular Expressions”(http://www.oreilly.com/catalog/regex/)

“Vi Macros, Abbreviations, and Buffers”(http://www.npa.uiuc.edu/docs/vi.macros)

“Best of VIM Tips”(http://gav.brokentrain.net/projects/vimtips/vimtips.pdf )

“Vim Quick Reference Card”

Anindya (IITB) Vim October 19, 2011 18 / 19

Happy Vimming!

Anindya (IITB) Vim October 19, 2011 19 / 19