vi improved, a programmers text editor

30
Introduction Getting started with vim Tasks Search and replace Configuration vim – Vi IMproved, a programmers text editor Bart Van Loon 31st January 2012 1 / 30 Bart Van Loon vim – Vi IMproved, a programmers tex

Upload: bart-van-loon

Post on 02-Dec-2014

1.343 views

Category:

Technology


3 download

DESCRIPTION

A very quick introductory course on VIM, a programmers text editor. Part of our standard training courses for offshore software developers at Zeropoint.IT.

TRANSCRIPT

Page 1: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

vim – Vi IMproved, a programmers text editor

Bart Van Loon

31st January 2012

1 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 2: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

1 Introduction

2 Getting started with vim

IntroductionStarting and quittingThe cursorEditing text

3 Tasks

4 Search and replace

5 Configuration

2 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 3: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

How it all began: viA part of history

I text editor originally created for UNIX

I old: first release in 1976 (Open Source: BSD license)

I but modern: 2009 survey by Linux Journal → vi[m] mostwidely used text editor (36%); second place: gedit (19%)

3 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 4: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

How it all began: viModal editor

I vi is a modal editor:

insert mode: typed text becomes part of the documentnormal mode: keystrokes are interpreted as commands

I i in normal mode: switch to insert mode; i again at thispoint: place an “i” character in the document

I esc in insert mode: switch to normal mode

I advantage: both text editing and command operationswithout requiring removal of hands from the home row⇒ speed!

4 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 5: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

How it all began: viIt breaks my fingers!

Many ideas, shortcuts, keystrokes, . . . can be explained by lookingat a common computer keyboard from the seventies.

5 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 6: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

How it all began: viContemporary derivatives and clones

vi: traditional vi ported to modern systems

vim: (“Vi IMproved”) vi with many more features

elvis: once popular clone with some extra features

nvi: default derivative shipped with all BSDs

vile: attempt to mix emacs and vi

. . .

6 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 7: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

vimIntroduction

I first released publicly in 1991 (Open Source charityware)

I still actively developed and maintained

I cross platform

I additional features specifically designed for editing source code

I customisable through plugins and vimscript

I described as “very much compatible with vi”, but not 100%

I huge community constantly at war with the emacs-community

7 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 8: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

vimSooooooo many features

completion, comparison and merging of files, comprehensive integrated

help system, extended regular expressions, scripting languages (both

native and through alternative scripting interpreters such as Perl, Python,

Ruby, Tcl, etc. . . ) including support for plugins, a graphical user

interface, limited integrated development environment-like features,

mouse interaction (both with and without the GUI), folding, editing of

compressed or archived files in gzip, bzip2, zip, and tar format and files

over network protocols such as SSH, FTP, and HTTP, session state

preservation, spell checking, split (horizontal and vertical) and tabbed

windows, unicode and other multi-language support, syntax highlighting,

trans-session command, search and cursor position histories, multiple

level undo/redo history which can persist across editing sessions, visual

mode, . . .

8 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 9: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Getting started

Starting vim

I vim; or

I vim <filename>; or

I vim [options] <filename>

One useful option is +<n>, which opens the file and immediatelyputs the cursor on line <n>.

9 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 10: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Getting started

Modes

I by default you start in normal modeI go to insert mode from normal mode

type i to start entering text at the cursor

type R to start replacing text at the cursor

type o to open a new line at the cursor

type O to open a new line above the cursor

I hit esc to enter normal mode

10 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 11: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Getting started

Exiting vim

I commands to quit:

: x ←↩ : save and quit

: q ←↩ : just quit

: q ! ←↩ : force quit (without saving!)

I shortcut from normal mode:

Z Z : quit and save only if changes were made

11 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 12: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Moving the cursor

Relative movements:

h : one character left

j : one line down

k : one line up

l : one character right

w : one word forward

b : one word back

Adding a digit multiplies the movement. Try 5 w , 1 2 k ,

2 b , . . .

12 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 13: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Moving the cursor

Absolute movements in the file:

^ or 0 : beginning of the line

$ : end of the line

g g : beginning of the file

G : end of the file

<d> G : line <d>

` . : your last edit

13 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 14: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Moving the cursor

Absolute movements in the screen (visible area):

H : highest line on the screen

M : middle line on the screen

L : lowest line on the screen

ctrl-f : page (screen) forward

ctrl-b : page (screen) backward

14 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 15: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Editing text

Inserting text:

i : insert text at the cursor

a : insert text after the cursor (append)

I : insert text at the beginning of the line

A : insert text at the end of the line

In insert mode, you can use the arrow keys to navigate the cursor,but often going back to normal mode will be much faster.

15 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 16: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Editing text

Deleting text:

x : delete character at the cursor (delete)

X : delete character before the cursor (backspace)

Replacing text:

r <c> : replace the current character with <c>

16 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 17: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Visual mode

For selecting areas of text, there is visual mode:

v : start visual mode

V : start visual line mode

ctrl-v : start visual block mode

17 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 18: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Operators and motions

Example operators:

d : delete

y : yank (copy)

c : change

Example motions1:

$ : to end of line

G : to end of file

e : to end of current word

1remember the part on “moving your cursor”?18 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 19: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Combining operators and motions

Combining operators and motions generates some really powerfulcommands. Some examples are:

y $ : copy from the cursor until the end of the line

d g g : delete from the cursor until the beginning of the file

Now lets add counts to increase the power:

y 3 k : copy the previous 3 lines

d 5 w : delete the next 5 words

19 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 20: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

More power

Another nice operator:

= : fix indenting

Some other nice motions:

( : to the beginning of the current sentence

) : to the beginning of the next sentence

% : to the matching bracket, parenthesis, braces, . . .

20 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 21: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

Double operators

When entering an operator twice, it operates on the completecurrent line:

d d : delete the current line

y y : copy the current line

. . .

21 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 22: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

IntroductionStarting and quittingThe cursorEditing text

The put command

To paste previously deleted or yanked (copied) text:

p : put (paste) after the cursor

P : put (paste) before the cursor

Some nice usage examples:

x p : swap the current character with the next one

d d p : swap the current line with the next one

5 p : paste 5 times after the cursor

22 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 23: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

Repeating tasks

Undo and redo:

You can think of each command (combined or not) as a task.

. : repeat last task

u : undo last task

ctrl-r : undo last undo (redo)

Typing text is also a task!

23 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 24: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

Macros

A group of tasks can be recorded as a macro:

q <a> : start recording macro with name <a>

q : stop recording current macro

@ <a> : replay macro with name <a>

24 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 25: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

Search

To start:

/ <p> ←↩ : forward search for <p>

? <p> ←↩ : backward search for <p>

Afterwards:

n : repeat previous search

N : repeat previous search in opposite direction

25 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 26: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

Search and replace

Structure of the command:

:As/B/C/D←↩

with:

A : area on which to operate

B : the pattern to search for (regular expression)

C : the new word to replace the found pattern with

D : any flags to fine tune the behaviour

26 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 27: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

Search and replace

Examples areas:

% : the complete file

’<,’> : the selected area

(empty) : the current line

Example flags:

c : confirm each substitution

g : replace all occurrences on one line

i : ignore the case for searching

27 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 28: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

~/.vim*

I your configuration is stored in ~/.vimrc

I system-wide configuration is stored in /etc/vimrc

I your plugins, languages, . . . live in ~/.vim/

28 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 29: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

The set-command

To change your configuration at runtime, use the set-command.Examples:

I :set spell←↩ to enable spell checking

I :set number←↩ show line numbers

I :set syntax=<lang>←↩ to highlight according to <lang>

To unset an option, prepend it with no:

I :set nospell←↩I :set nonumber←↩I . . .

29 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor

Page 30: Vi IMproved, a programmers text editor

IntroductionGetting started with vim

TasksSearch and replace

Configuration

References

I the vimtutor-command

I http://en.wikipedia.org/wiki/Vi

I http://en.wikipedia.org/wiki/Vim_(text_editor)

I https://users.cs.jmu.edu/bernstdh/web/common/

help/vim.php

I http://www.youtube.com/watch?v=SI8TeVMX8pk

I http://www.youtube.com/watch?v=V3ccIf-cfnQ

30 / 30 Bart Van Loon vim – Vi IMproved, a programmers text editor