Difference between revisions of "Vi"

From wiki
Jump to: navigation, search
m
(Commandmode:)
 
Line 28: Line 28:
 
;<nowiki>:%s/<oldstring>/<newstring>/g</nowiki>
 
;<nowiki>:%s/<oldstring>/<newstring>/g</nowiki>
 
:Replace <oldstring> with <newstring> in current document (g makes sure mulitple occurences on one line are replaced).
 
:Replace <oldstring> with <newstring> in current document (g makes sure mulitple occurences on one line are replaced).
;<nowiki>:%/^M$//</nowiki>
+
;<nowiki>:%s/,/\r/g</nowiki>
 +
:Replace all comma's with a newline
 +
;<nowiki>:%s/^M$//</nowiki>
 
:do dos2unix (put in ^M using CTRL-V CTRL-M)
 
:do dos2unix (put in ^M using CTRL-V CTRL-M)
 
;<nowiki>:1,2t3</nowiki>
 
;<nowiki>:1,2t3</nowiki>

Latest revision as of 12:52, 30 July 2020

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 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