If started as ´view´ the file is opened read-only, you can however write
using :!w if you have write permissions on file-system level
Always leave the file using :q or :n, vi will warn you if you have made
unsaved changes. Don’t exit using :!wq by default as I did, sometime you will
save something you really don’t want.
I found some good advise in this Cheat Sheet. A nice tutorial can be found on http://www.truth.sk/.
-
Selections
- :.
- Current line
- :3
- Line number 3
- :3,5
- Line 3 up to 5
- :%
- The whole file
- :+4
- 4 line below current line
- :-5
- 5 lines above current line
- :$
- Last line in the file
You can combine these selections with most command-line (:) commands
-
Commandmode
- :%s/<oldstring>/<newstring>/g
- Replace <oldstring> with <newstring> in current
document (g makes sure mulitple occurences on one line are
replaced). - :%/^M$//
- do dos2unix (put in ^M using CTRL-V CTRL-M)
- :1,2t3
- Copy line 1 and 2 under line 3
- :1,3m6
- Move line 1 up to 3 to linel 6
- :set number
- Display line numbers
- :set ts=3
- Set tabstop to 3
- :.=
- Current linenumber
- :$=
- Last linenumber
- :n
- Goto next file (if editing multiple files)
- :se ic
- Search Case insensitive
- :%v/./d
- Delete all empty lines.
Execute d (delete) for all lines not matching . (any character). - /<string>
- Search forward for <string>
- ?<string>
- Search backward for <string>
- n
- Find next occurence of <string>
- ^L
- Refresh screen
- J
- Join current and next line (remove <newline>)
- O
- Go to input mode above current line
- o
- Go to input mode under the current linel
- i
- Go to input mode (insert)
- a
- Go to input mode (add)
- %
- Jump to matching {, [ or (
- .
- Repeat last action
Screen edit mode