Effective Text Editing With Vim by Kentwang 11年3月7日星期一
May 22, 2015
Effective Text Editing With Vim by Kentwang
11年3月7日星期一
Effective text editing
“Seven habits of effective text editing”
- Bram Moolenaar
11年3月7日星期一
Problem
Got lots of text to edit
- source code
- config file
- log
- etc.
11年3月7日星期一
Problem
Got lots of text to edit
- source code
- config file
- log
- etc.But don’t h
ave enough tim
e!
11年3月7日星期一
Tool
• Selecting a good editor is the first step towards effective text editing
• Obviously, Vim is used here
11年3月7日星期一
What is vim
11年3月7日星期一
Comments
11年3月7日星期一
Lots of people said like this
11年3月7日星期一
11年3月7日星期一
难用
11年3月7日星期一
其实心里往往这么想...
11年3月7日星期一
11年3月7日星期一
真TMD难用
11年3月7日星期一
But, some in other way
11年3月7日星期一
11年3月7日星期一
爽11年3月7日星期一
爽爽11年3月7日星期一
Why
11年3月7日星期一
Different view
11年3月7日星期一
Different view
11年3月7日星期一
Different view
11年3月7日星期一
Vim is a tool, the use of which must be learned
11年3月7日星期一
How to improve
11年3月7日星期一
Basic steps
1. Detect inefficiency
2. Find a quicker way
3. Make it a habit
11年3月7日星期一
Habit 1:Moving around quickly
11年3月7日星期一
Feature
11年3月7日星期一
Feature
VS
11年3月7日星期一
Feature
Who moved my mouse !
VS
11年3月7日星期一
Detect ineffeciency
• Depend on mouse
• Use direction keys
• One line/column each time
• Hard to find match things
11年3月7日星期一
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日星期一
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日星期一
Detect ineffeciency
• Typical you search like this
11年3月7日星期一
Detect ineffeciency
• Typical you search like this
/FuncName
11年3月7日星期一
Detect ineffeciency
• Typical you search like this
/FuncNamennnn...
11年3月7日星期一
Find a quicker way
• The hlsearch option
• The * command
• Folding with ‘zf ’ , ‘zo’ and ‘zc’
11年3月7日星期一
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日星期一
Habit 2:Don’t type it twice
11年3月7日星期一
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日星期一
Find a quicker way
• Insert mode completion
• Ctrl - N & Ctrl -P
• Omni-completion
• Automatic indenting
11年3月7日星期一
Find a quicker way
11年3月7日星期一
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日星期一
Habit 3:Fix it when it’s wrong
11年3月7日星期一
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日星期一
Find a quicker way
• Spell correction macros
:iabbrev accnt account
:syntax keyword WordError accnt
11年3月7日星期一
Make it a habit• Add new words if you see them
11年3月7日星期一
Habit 4:A file seldom comes
alone
11年3月7日星期一
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日星期一
A quicker way
• Find & grep
• ctags & taglist
• Quickfix
11年3月7日星期一
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日星期一
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日星期一
Habit 5:Let’s work together
11年3月7日星期一
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日星期一
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日星期一
A quicker way
11年3月7日星期一
More Effective
• Integrate with SVN
• SVN_EDITOR environment variable
• Set vim as diff program for svn diff
11年3月7日星期一
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日星期一
Habit 6:Sharpen the saw
11年3月7日星期一
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日星期一
Sharpen the saw
• Replacing
• automatic indenting
• quickfix
• plugins
• key-mapping
11年3月7日星期一
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日星期一
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日星期一
Encoding
• fileencoding
• encoding
• LC_CTYPE
11年3月7日星期一
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日星期一
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日星期一
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日星期一
Learn it, time rewarded.
11年3月7日星期一
One more thing
11年3月7日星期一
:help user-manual
11年3月7日星期一
Questions?
11年3月7日星期一
The End
11年3月7日星期一