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

No comments: