Effective text editing with vim

70
Effective Text Editing With Vim by Kentwang 11年3月7日星期一

description

share something about vim skills.lots from 《Seven habit

Transcript of Effective text editing with vim

Page 1: Effective text editing with vim

Effective Text Editing With Vim by Kentwang

11年3月7日星期一

Page 2: Effective text editing with vim

Effective text editing

“Seven habits of effective text editing”

- Bram Moolenaar

11年3月7日星期一

Page 3: Effective text editing with vim

Problem

Got lots of text to edit

- source code

- config file

- log

- etc.

11年3月7日星期一

Page 4: Effective text editing with vim

Problem

Got lots of text to edit

- source code

- config file

- log

- etc.But don’t h

ave enough tim

e!

11年3月7日星期一

Page 5: Effective text editing with vim

Tool

• Selecting a good editor is the first step towards effective text editing

• Obviously, Vim is used here

11年3月7日星期一

Page 6: Effective text editing with vim

What is vim

11年3月7日星期一

Page 7: Effective text editing with vim

Comments

11年3月7日星期一

Page 8: Effective text editing with vim

Lots of people said like this

11年3月7日星期一

Page 9: Effective text editing with vim

11年3月7日星期一

Page 10: Effective text editing with vim

难用

11年3月7日星期一

Page 11: Effective text editing with vim

其实心里往往这么想...

11年3月7日星期一

Page 12: Effective text editing with vim

11年3月7日星期一

Page 13: Effective text editing with vim

真TMD难用

11年3月7日星期一

Page 14: Effective text editing with vim

But, some in other way

11年3月7日星期一

Page 15: Effective text editing with vim

11年3月7日星期一

Page 16: Effective text editing with vim

爽11年3月7日星期一

Page 17: Effective text editing with vim

爽爽11年3月7日星期一

Page 18: Effective text editing with vim

Why

11年3月7日星期一

Page 19: Effective text editing with vim

Different view

11年3月7日星期一

Page 20: Effective text editing with vim

Different view

11年3月7日星期一

Page 21: Effective text editing with vim

Different view

11年3月7日星期一

Page 22: Effective text editing with vim

Vim is a tool, the use of which must be learned

11年3月7日星期一

Page 23: Effective text editing with vim

How to improve

11年3月7日星期一

Page 24: Effective text editing with vim

Basic steps

1. Detect inefficiency

2. Find a quicker way

3. Make it a habit

11年3月7日星期一

Page 25: Effective text editing with vim

Habit 1:Moving around quickly

11年3月7日星期一

Page 26: Effective text editing with vim

Feature

11年3月7日星期一

Page 27: Effective text editing with vim

Feature

VS

11年3月7日星期一

Page 28: Effective text editing with vim

Feature

Who moved my mouse !

VS

11年3月7日星期一

Page 29: Effective text editing with vim

Detect ineffeciency

• Depend on mouse

• Use direction keys

• One line/column each time

• Hard to find match things

11年3月7日星期一

Page 30: Effective text editing with vim

Find a quicker way

• Edit without mouse

• Use ‘h j k l’ instead of direction keys

• Move multi-line each time

• Use ‘%’ to find match things

11年3月7日星期一

Page 31: Effective text editing with vim

Find a quicker way

• Edit without mouse

• Use ‘h j k l’ instead of direction keys

• Move multi-line each time

• Use ‘%’ to find match things

Samples: 20G jump to line 20 ^ jump to first non blank of line $ jump to end of line % jump to match ( #if #endif etc.) 10j jump to 10 lines below 5w jump to 5 words after

11年3月7日星期一

Page 32: Effective text editing with vim

Detect ineffeciency

• Typical you search like this

11年3月7日星期一

Page 33: Effective text editing with vim

Detect ineffeciency

• Typical you search like this

/FuncName

11年3月7日星期一

Page 34: Effective text editing with vim

Detect ineffeciency

• Typical you search like this

/FuncNamennnn...

11年3月7日星期一

Page 35: Effective text editing with vim

Find a quicker way

• The hlsearch option

• The * command

• Folding with ‘zf ’ , ‘zo’ and ‘zc’

11年3月7日星期一

Page 36: Effective text editing with vim

Make it a habit

• Put this in your vimrc file

• Use * again and again

• Use command completion (using <tab>)

• Use fold when needed

set hlsearch

11年3月7日星期一

Page 37: Effective text editing with vim

Habit 2:Don’t type it twice

11年3月7日星期一

Page 38: Effective text editing with vim

Detect ineffeciency

• Long class names

- TiXmlAttributeSet

• Long function names

- QueryDoubleAttribute

• Hard to type and often type it wrong

• Lots of time on indenting and code style

11年3月7日星期一

Page 39: Effective text editing with vim

Find a quicker way

• Insert mode completion

• Ctrl - N & Ctrl -P

• Omni-completion

• Automatic indenting

11年3月7日星期一

Page 40: Effective text editing with vim

Find a quicker way

11年3月7日星期一

Page 41: Effective text editing with vim

Make it a habit

• Use Ctrl + P and Ctrl + N again and again

• Use omni-completion if needed

• Add these lines in vimrc:

filetype plugin on filetype indent on “ auto indent set ai set smarttab

11年3月7日星期一

Page 42: Effective text editing with vim

Habit 3:Fix it when it’s wrong

11年3月7日星期一

Page 43: Effective text editing with vim

Detect ineffeciency

• Often misspell English words

• Have to check text carefully

• Need a sequence of actions to correct tem

• After correct you misspell again and again

11年3月7日星期一

Page 44: Effective text editing with vim

Find a quicker way

• Spell correction macros

:iabbrev accnt account

:syntax keyword WordError accnt

11年3月7日星期一

Page 45: Effective text editing with vim

Make it a habit• Add new words if you see them

11年3月7日星期一

Page 46: Effective text editing with vim

Habit 4:A file seldom comes

alone

11年3月7日星期一

Page 47: Effective text editing with vim

Detect ineffeciency

• When working on a new project you have a hard time finding your way in the files

• You have to jump between files all the time

• Edit in an editor but compile in terminal

• Hard to find declaration of symbol

11年3月7日星期一

Page 48: Effective text editing with vim

A quicker way

• Find & grep

• ctags & taglist

• Quickfix

11年3月7日星期一

Page 49: Effective text editing with vim

Many other ways

• use ‘gf ’ to goto header file

• Use ‘[I’ to find the word under the cursor in include files. Or ‘[<Tab>’ to jump there

• Use a.vim to switch between .c and .h

11年3月7日星期一

Page 50: Effective text editing with vim

Make it a habit

• Use quickfix as often as possible

• Drop ‘a.vim’ to your plugin directory

• Remember to use ‘ctags’ and ‘taglist’

11年3月7日星期一

Page 51: Effective text editing with vim

Habit 5:Let’s work together

11年3月7日星期一

Page 52: Effective text editing with vim

Detect ineffeciency

• Often need to compare files

• Switch out of terminal and use windows compare tools

• Sometimes have to transfer file from IDC server to local first, just because can’t use windows compare tools there and diff command is not so readable

11年3月7日星期一

Page 53: Effective text editing with vim

A quicker way

• vimdiff or vim -d

• Works well in both terminal and GUI

• Pretty result and convenient interactive

• Use ‘[c’ ‘]c’ to jump to changes, ‘do’ ‘dg’ to copy text from one to another

11年3月7日星期一

Page 54: Effective text editing with vim

A quicker way

11年3月7日星期一

Page 55: Effective text editing with vim

More Effective

• Integrate with SVN

• SVN_EDITOR environment variable

• Set vim as diff program for svn diff

11年3月7日星期一

Page 56: Effective text editing with vim

Make it a habit

• Use vimdiff instead of windows compare tools

• Add this line to your bashrc:

• Edit ~/.subversion/config, set vim as the diff-cmd

• If you are a big fan, try Vimperator / Vimium

export SVN_EDITOR=vim

11年3月7日星期一

Page 57: Effective text editing with vim

Habit 6:Sharpen the saw

11年3月7日星期一

Page 58: Effective text editing with vim

Sharpen the saw

• Keep on tuning the set of commands you use for your needs

• Learn from what you did

• Type :help command when you need help

11年3月7日星期一

Page 59: Effective text editing with vim

Sharpen the saw

• Replacing

• automatic indenting

• quickfix

• plugins

• key-mapping

11年3月7日星期一

Page 60: Effective text editing with vim

Replacing

• Replace a word

• Use \< and \> to match the start/end of word

Samples: 10, 20s/Old/New/g %s/Old/New/g %s/\<Old/New/g %s/\<Old\>/New/g

11年3月7日星期一

Page 61: Effective text editing with vim

Replacing

• Replacing in several files

• Record / Execute register

Sample: vim *.cpp // start vim qq // start recording into the q register :%s/\<OldFunc\>/NewFunc/ge // replacing :wnext // write and goto next file q // stop recording @q // execute

11年3月7日星期一

Page 62: Effective text editing with vim

Encoding

• fileencoding

• encoding

• LC_CTYPE

11年3月7日星期一

Page 63: Effective text editing with vim

Summary• Step 1: Detect inefficiency

- Find out what you waste time on

• Step 2: Find a quicker way

- read the online help & quick reference

- ask friends & google

- do it yourself

• Step 3: Make it a habit

- do it

- keep on improving11年3月7日星期一

Page 64: Effective text editing with vim

How to effectively

• No time to read documents or new commands. keep on using primitive commands.

• Want to learn every feature and use the most efficient command all the time.

NOT

11年3月7日星期一

Page 65: Effective text editing with vim

How to effectively

• No time to read documents or new commands. keep on using primitive commands.

• Want to learn every feature and use the most efficient command all the time.

NOT

11年3月7日星期一

Page 66: Effective text editing with vim

Learn it, time rewarded.

11年3月7日星期一

Page 67: Effective text editing with vim

One more thing

11年3月7日星期一

Page 68: Effective text editing with vim

:help user-manual

11年3月7日星期一

Page 69: Effective text editing with vim

Questions?

11年3月7日星期一

Page 70: Effective text editing with vim

The End

11年3月7日星期一