Difference between revisions of "Vi"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
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. | 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. | ||
+ | =Tips= | ||
+ | # 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 [http://www.lagmonster.org/docs/vi2.html Cheat Sheet]. A nice tutorial can be found on [http://www.truth.sk/ http://www.truth.sk/]. | ||
+ | # To see what is happening at startup: <code>vi --cmd 'set verbose=2'</code> | ||
− | + | =Usage= | |
− | |||
− | |||
− | |||
==Selections== | ==Selections== | ||
Latest revision as of 15:55, 19 April 2022
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.
Tips
- 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/.
- To see what is happening at startup:
vi --cmd 'set verbose=2'
Usage
Selections
- :.
- Current line
- :3
- Line number 3
- :3,5
- Line 3 up to 5
- :%
- The whole file
- :+4
- 4 lines 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).
- :%s/,/\r/g
- Replace all comma's with a newline
- :%s/^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).
Screen edit mode
- /<string>
- Search forward for <string>
- ?<string>
- Search backward for <string>
- n
- Find next occurrence of <string>
- ^L
- Refresh screen.
- J
- Replace newline with space on the current line. Join current and next line with a space in between.
- O
- Go to input mode above current line.
- o
- Go to input mode under the current line.
- i
- Go to input mode (insert) on current position.
- a
- Go to input mode (add) after current position.
- %
- Jump to matching {, [ or (
- .
- Repeat last action