Monday, January 26, 2009

More VIM text editing command

The more I use VIM, the more it surprises me.
Here, I list some useful commands in everyday text editing. I have known some of them for long time. But others were learnt recently.

Command Explanation Note
Delete
cc delete current lines and change to insert mode  
dd delete current lines  
c{motion} delete until the character {motion} moves to and change to insert mode.  
d{motion} delete until the character {motion} moves to  
C delete characters until the end of the line. Then change to insert mode  
D delete characters until the end of the line.  
s delete [count] characters and change to insert mode  
x delete [count] characters under and after the cursor  
:[range]d delete those lines in the range  
S delete [count] lines and change to insert mode  
Replace
R Enter Replace mode  
gR    
Line joining
[range]J join [count] lines

Steps of execution of these commands
(1) delete leading white space on the next line,
(2) if there is trailing white space for current line the next line starts with a ')',
        delete <EOL>
    else if the 'joinspaces' option is on and current line ends up with a '.', '!' or '?'
        replace <EOL> with two spaces
    else
        replace <EOL> with one space

{Visual}J join the selected lines
:[range]j[!] join range lines. With !, the join doesn't insert/remove any spaces.
[range]gJ join [count] lines Don't insert or delete any spaces.
{VIsual}gJ join the selected lines

Some commands useful in source code editing

Commands Explanation
<{motion} shift covered lines leftwards by 1 shiftwidth
>{motion} shift covered lines rightwards by 1 shiftwidth
{Visual}[count]< shift selected lines leftwards by 1 shiftwidth
{Visual}[count]> shift selected lines rightwards by 1 shiftwidth
[range]< shift selected lines leftwards by 1 shiftwidth
[range]> shift selected lines rightwards by 1 shiftwidth
<< shit current line leftwards by 1 shiftwidth
>> shit current line rightwards by 1 shiftwidth

Resource
http://www.vim.org/htmldoc/change.html

Text formatting in VIM

How to set options for VIM?

using set command.
Examples:
Setting Explanation
set paste set options for commands that don't need parameters.
set filetype=java Directly set options for commands that accept parameters.
set guioptions-=option Remove a specific option from the configuration of a command.
set guioptions+=option Add a specific option to the configuration of a command.

Basic concept
Sentence/Paragraph/Section: http://www.vim.org/htmldoc/motion.html#sentence

Text format
http://www.vim.org/htmldoc/change.html#formatting
Alignment

Setting Explanation Examples
:[range]ce[nter] [width] center lines :.,+3 center 80
:[range]ri[ght] [width] right alignment :% right
:[range]le[ft] [indent] left alignment.(Unit of identation is space) :left 4

Format

Command

Explanation

Examples

Note

gq{motion} Format lines that {motion} moves over.
Cursor is put where {motion} moves to
gqap
format a paragraph
Motion:
http://www.vim.org/
htmldoc/motion.html
gw{motion} Format lines that {motion} moves over.
Put cursor back to the original position.
gwj
gqgq
gqq
Format current line    
{Visual}gq Format highlighted text    

How to control formatting setting of gq command?
Formatting does not change empty lines, but it changes lines only containing white spaces.

(1) textwidth
This option controls length of every formatted line.
If option textwidth is 0, the formatted line length is the screen width (max value is 79).

(2) autoindent
If this option is on, Vim uses indent of the first line for the following lines.

(3) joinspaces (boolean)
Used when lines are joined together. By default, it is on. Use set nojoinspaces to turn it off.

(4) formatprg
Set an external program to format text. In this case, the textwidth and other options have no effect on text formatting.

(5) formatoptions
Most useful options:
t: Auto-wrap text using textwidth. (does not apply to comments)
c: Auto-wrap commens using textwidth.
q: Allow formatting of comments with gq command. When using gq command in comments, blank lines and lines only with comment leaders and white spaces are considered as paragraph delimiters.
r: auto insert comment leader after hitting 'Enter' in Insert mode.
o: auto insert comment leader after hitting 'o' or 'O'.
a: auto format paragraphs when text is inserted or deleted.
w: A trailing non white space ends a paragraph.
Note: Formatting would be applied anyway no matter whether the paragraphs are delimited correctly.
You can use set formatoptions-=a to disable automatic formatting.
More options: http://www.vim.org/htmldoc/change.html#fo-table

(6) comments
This option controls how to format comments (usually in source code).
Default value is 

"s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-"
s: start of a three-piece comment
s{digit}: add extra indent to middle part. Unit is space (not tab).
s-{digit}: remove extra indent to middle part. Unit is space (not tab).
m: middle part of a three-piece comment. Middle part is left adjusted with start part of the comment by default. This is controlled by option l.
b: blanks required after the specified string.
e: end of a three-piece comment
x: In C++ comment, just type / to end a comment when middle-comment string is inserted. The space between middle-part and / would be removed automatically.
n: recognize numbered list(see below for details)
More options: http://www.vim.org/htmldoc/change.html#format-comments
My setting is
set textwidth=79
set formatoptions=tcqron

How to format numbered lists?
This sometimes is really tricky.
See this post: http://objectmix.com/editors/332035-vim-how-create-bullet-list-numbered-list.html
Usually, textwidth must be set and formatoptions must include nwt.
Also see option formatlistpat(flp) for how to set list header. That is used by vim to recognize numbered lists.