vi editor useful commands
Oct 23, 2018 00:01 · 213 words · 1 minute read
| Description | Command |
|---|---|
| comment all the lines | :%s/^/# |
| uncommnet all the lines | :%s/^#/ |
| comment line no. m to n | :m,ns/^/# |
| uncomment line no. m to n | :m,ns/^#/ |
| add comma after all the lines | :%s/$/, |
| remove comma after all the lines | :%s/,$/ |
| remove comma after line no. m to n | :m,ns/,$/ |
| search for a string from forward | /string |
| search for a string from backward | ?string |
| repeat the search in same direction | n |
| repeat the search in reverse direction | N |
| delete a line | dd |
| delete n lines | ndd |
| delete nth line | :nd |
| delete line no. m to n | :m,nd |
| copy a line | yy |
| copy n lines | nyy |
| copy nth line | :ny |
| copy line no. m to n | :m,ny |
| put or paste a line | p |
| running a command without exiting vi | :! |
| undo last change | u |
| redo last change | Ctrl + r |
| write to a file | :w |
| write and quit | :wq |
| quit forcefully without saving changes | q! |
| move cursor to start of current line | ^ |
| move the cursor to end of current line | $ |
| move cursor to start of current line | 1G |
| move the cursor to end of current line | G |
| move the cursor to nth line | nG |
| delete a line after the cursor | C |
Please let me know, if there is any error in above commands.