VI quick start: Difference between revisions

From Genecats
Jump to navigationJump to search
 
Line 107: Line 107:
  c$              # change text from cursor to the end of this line
  c$              # change text from cursor to the end of this line
  c0              # change text from cursor to the beginning of this line
  c0              # change text from cursor to the beginning of this line
  :r!command       # read in text from command
  :r!command       # read in text from command


==Delete text==
==Delete text==

Latest revision as of 18:39, 25 May 2021

See also:

Graphical vi/vim Cheat Sheet

Insert/Command States

vi is usually either in an 'insert' state, or in a 'command' state. It starts up in the 'command' state. There are other states that I do not use but I sometimes enter them accidentally and need to use the :q to get out of those states.

There is a lot more to vi than indicated here. This is a very brief cheat-sheet, but even with this small set of commands, vi is a powerful tool for managing documents.

To enter the 'insert' state from the 'command' state, issue some kind of insert editing command. When you are in 'insert' state, whatever you type becomes text in the document. To exit 'insert' state and return to 'command' state, press the 'Esc' key. When in doubt about the state, press the 'Esc' key to get to 'command' state. Your terminal should indicate the INSERT state with a message at the bottom of the screen:

 -- INSERT --

The bottom line of your terminal when in 'command' state will indicate the line and column position of the cursor, and a percent indication of how far down you are in your document.

Starting vi

vi file.ext      # start editing the file called file.ext - whether it exists now or not
vi -r file.ext   # recover the edits to a file.  The previous session to vi was lost due
                 # to login disconnect or system crash.  This will resume close to where you where
                 # when vi died.  After this recover and exit from vi, remove the .file.ext.swp file
                 # which is used by vi for its housekeeping.
vi -x secure.txt # edit a file in secure encoded form.  You will need to establish a password to use
                 # this file in vi.  The contents are only seen in a vi -x session, otherwise they
                 # are encoded and can not be read by others unless they know the password

For our local environment editing rules, you will want a ~/.vimrc file with these settings

set wrapmargin=8 shiftwidth=4 nohlsearch
au Filetype * set formatoptions=cql
set tags=~/kent/src/tags

Exit vi

:wq              # write file and quit vi - saves all edits to file being edited
:q!              # abandon edits since last file write and quit vi
:w               # simply write out file contents saving all edits to this point.  Remain in vi

tags

ctrl-] # go to function or structure that cursor is on
ctrl-t # return to where ctrl-] was clicked

This can be recursive down and up

Cursor movement

Cursor movement commands are given when in the 'command' mode.

It is vital to learn cursor movement keys to rapidly move around your document. You probably shouldn't be using arrow keys repeatedly to move the cursor one character at a time unless that is really what you need to be doing at that moment. It is much more efficient to move the cursor based on objects in the document. Adding a number in front of any of these commands multiplies their operation by that number.

nG               # where n is a number of digits to number a line, G goes to that line number
G                # go to the end of the document
1G               # go to the top (line 1) of the document
w                # move forward to the beginning of the next word
W                # move forward to the beginning of the next string
b                # move back to the beginning of the current word
B                # move back to the beginning of the current string
e                # move to the end of the current word
E                # move to the end of the current string
M                # move cursor to the middle of the screen
0                # go to the beginning (column 1) of this line
$                # go to the last character on this line
<CR>             # go to the beginning (column 1) of the next line
-                # go to the beginning (column 1) of the previous line
<space>          # move forward one character
Ctrl-h           # move back one character
Ctrl-f           # scroll to next page (one screen full minus two lines)
Ctrl-b           # scroll back one page (one screen full minus two lines)
Ctrl-d           # scroll down one half page (one half screen)
Ctrl-u           # scroll up one half page (one half screen)
z.               # scroll the document to move the line the cursor is on to the middle of the screen
%                # move to associated ( )  { }  [ ]
/string          # go forward to the beginning of 'string' - great for moving around the document
                 # on the screen when it is somewhere distant from where the cursor is, especially when
                 # 'string' is short and unique
?string          # go backward to the beginning of 'string' - same benefit as above, but in reverse
n                # go to next occurrence of previous search string, backwards if started with ? forward if /
N                # go to previous occurrence of previous search string, backwards if started with / forward if ?

Insert text

These functions enter the 'insert' mode (except for the rc single character replace)

i                # insert text immediately before the character the cursor is on
I                # insert text at the beginning of this line
a                # append text immediately following the cursor
A                # append text at the end of this line
O                # start(open) a new line above this line
o                # start(open) a new line following this line
rc               # replace character under the cursor with 'c' ! remains in command mode !
s                # substitute in place of the character under the cursor
cw               # change text from cursor to the end of this word
cW               # change text from cursor to the end of this string
c$               # change text from cursor to the end of this line
c0               # change text from cursor to the beginning of this line
:r!command       # read in text from command

Delete text

dd               # delete this line
D                # delete from cursor to end of this line
d0               # delete from cursor to beginning of this line
dG               # delete lines from cursor to end of document
x                # delete character under cursor
X                # delete character before cursor

Setting/using marks

ma               # create a mark at the cursor named 'a'
'a               # move cursor to first non-blank character on the line with the mark named 'a'
`a               # move cursor exactly to the mark named 'a'

Yank/Put

Y                # yank the current line
P                # (upper case P) put the yank buffer before the cursor (before line if buffer contains lines)
p                # (lower case p) put the yank buffer following the cursor (following the line if buffer contains lines)
"ay'a            # yank from here to the mark named 'a'(the y'a part) placing the yank in buffer called a(the "a part)
"ap              # put the contents of yank buffer called 'a' following the cursor
"aP              # put the contents of the yank buffer called 'a' before the cursor

Search/Replace

:%s/string/replace/flags    # % means whole document, s is the substitute function,
                            # find 'string' replace with 'replace' with potential flags:
                            # most often used flag: g meaning replace all occurrences (global)
                            # not just the first occurrence on a line.
:.,$s/string/replace/flags  # from here (.) to end of document ($) search and replace
:'a,'bs/string/replace/flags # from mark named 'a' to mark named 'b' search and replace

What's Happening

Ctrl-g                      # show information about the status of this document