Skip to main content

Important vi Editor Commands


<<Back to Linux Main Page

Frequent Used Linux vi Editor Commands

What is vi?

vi  (visual editor) is a default editor in Linux
The vi editor has 2 modes


1: command mode
2: insert mode


To open a file with vi, invoke vi command as shown below
vi <file_name>

If the <file_name> file does not exists vi will open a new named file.
After opening the file in vi editor just press i to inter into insert mode. In this mode, entered text is inserted into the file. To turn off the insert mode just press Esc (Escape) button on keyboard

Important vi commands

How to open a file using vi editor

vi <file_name>        <=edit file starting at line 1
vi -r <file_name>    <=recover crashed file that was being edited
Example:
$ vi -r new_file1


How to close a file opened vi editor

When you wish to exit from vi editor, first turn off the insert mode just press Esc (Escape). Now you can type the command followed by : and press enter (i.e. return button on the keyboard).
You can even combine multiple command eg. :wq (command :w writes the changes in the file to the disk and command :q quits vi)

:x       <= Save the changes in file and quit vi
:wq    <= Save the changes in file and quit vi
:w      <= Save the changes in file
:q!     <= discard the changes in file and quit vi
:q      <= quit vi 

NOTE: You can not quit if the changes in file is not saved, in that case either save the changes or force quit using :q! command

Moving the Cursor in vi

j or <Return key> or <down-arrow>    <= move cursor one line down
k or <up-arrow>                                  <= move cursor one line up
h or <Backspace> or <left-arrow>      <= move cursor one character left
l or <space> or <right-arrow>             <= move cursor one character right
0                                                         <= move cursor to start of current line
$                                                         <= move cursor to start of current line
w                                                         <= move cursor to the beginning of next word
b                                                         <= move cursor back to beginning of preceding word
e                                                         <= move cursor to the end of next word
:0<Return> or 1G                               <= move cursor to first line in file
:n<Return> or nG                               <= move cursor to line n

:$<Return> or G                                 <= move cursor to last line in file


NOTE: The symbol ^ before a letter means that the <Ctrl> key should be held down while the letter key is pressed

Screen Manipulation Commands in vi

^f   <= move forward one screen 
^b   <= move backward one screen
^d   <= move down (forward) one half screen
^u   <= move up (back) one half screen
^l   <= redraws the screen
^r   <= redraws the screen, removing deleted lines

Inserting, Changing, and Deleting Text in vi

Reverting the Changes

u                                   <= undo 

Inserting the text
i                                  <= insert text before cursor
I                                  <= insert text at beginning of current line
a                                 <= insert text after cursor
A                                <= insert text to end of current line
o                                <= open and put text in a new line below current line
O                               <= open and put text in a new line above current line


Changing the text
r                                 <= replace single character under cursor
R                                <= replace characters, starting with current cursor position
cw                              <= change the current word with new text,
cNw                           <= change N words beginning with character under cursor
C                                <= change (replace) the characters in the current line
cc                               <= change (replace) the entire current line 
Ncc or cNc                <= change (replace) the next N lines, starting with the current line,


Deleting the text
x                              <= delete single character under cursor 
Nx                           <= delete N characters, starting with character under cursor  
dw                           <= delete the single word beginning with character under cursor
dNw                        <= delete N words beginning with character under cursor;  
D                            <= delete the remainder of the line, starting with current cursor position
dd                           <= delete entire current line  
Ndd or dNd            <= delete N lines, beginning with the current line;


Cutting and Pasting Text

yy                        <= copy (yank, cut) the current line into the buffer 
Nyy or yNy        <= copy (yank, cut) the next N lines, including the current line, into the buffer  
p                         <= put (paste) the line(s) in the buffer into the text after the current line


Saving and Reading Files in vi
:r                                                <= filename<Return> read file named <filename> and insert into current file after current line  
:w<Return>                               <= save the content of the file
:w newfile<Return>                  <= Save the content of file to new file 
:12,35w newfile<Return>         <= write the contents of the lines numbered 12 through 35 to a newfile  
:w! existingfile<Return>           <= over write the existingfile with the content of current file

Other Important Commands
:set nu <enter>                           <= set the line numbers
:set nonu <enter>                       <= unset the line numbers
:.= <enter>                                 <= returns line number of current line at bottom of screen
:= <enter>                                  <= returns the total number of lines
^g <enter>                                 <= provides the current line number, along with the total number of lines in the file
/string                                        <= search forward for occurrence of string in file
?string                                       <= search backword for occurrence of string in file
n                                                <= move to next occurrence of search string

N                                               <= move to next occurrence of search string in opposite direction

Search and Replace text in vi

:s/string                                <= search the string
:s/string1/string2/                 <= Search string1 and replace the first occurance with string2
:%s/pattern/replace/g          <= Search string1 and replace all the occurances with string2
:%s/pattern/replace/gc        <= Search string1 and replace all the occurances with string2 but ask for confirmation before replacing
:%s/\<string1\>/string2/gc   <=Find and Replace Whole Word Only
:%s/unix/Linux/gi                 <=  Find and Replace ignoring the case
:%s/UNIX/bar/gI                  <= Find and Replace case sensitive
:100,200s/UNIX/Linux/g      <= find and Replace All Lines Between line 100 and line 200


Comments

Popular posts from this blog

How to Power On/off Oracle Exadata Machine

<<Back to Exadata Main Page How to Power On/off Oracle Exadata Machine Oracle Exadata machines can be powered on/off either by pressing the power button on front of the server or by logging in to the ILOM interface. Powering on servers using  button on front of the server The power on sequence is as follows. 1. Start Rack, including switches  Note:- Ensure the switches have had power applied for a few minutes to complete power on  configuration before starting Exadata Storage Servers 2.Start Exadata Storage Servers  Note:- Ensure all Exadata Storage Servers complete the boot process before starting the   database servers 3. Start Database Servers Powering On Servers Remotely using ILOM The ILOM can be accessed using the Web console, the command-line interface (CLI), IPMI, or SNMP. For example, to apply power to server dm01cel01 using IPMI, where dm01cel01-ilom is the host name of the ILOM for the server to be powered on, run the

ORA-28374: typed master key not found in wallet

<<Back to Oracle DB Security Main Page ORA-46665: master keys not activated for all PDBs during REKEY SQL> ADMINISTER KEY MANAGEMENT SET KEY FORCE KEYSTORE IDENTIFIED BY xxxx WITH BACKUP CONTAINER = ALL ; ADMINISTER KEY MANAGEMENT SET KEY FORCE KEYSTORE IDENTIFIED BY xxxx WITH BACKUP CONTAINER = ALL * ERROR at line 1: ORA-46665: master keys not activated for all PDBs during REKEY I found following in the trace file REKEY: Create Key in PDB 3 resulted in error 46658 *** 2019-02-06T15:27:04.667485+01:00 (CDB$ROOT(1)) REKEY: Activation of Key AdnU5OzNP08Qv1mIyXhP/64AAAAAAAAAAAAAAAAAAAAAAAAAAAAA in PDB 3 resulted in error 28374 REKEY: Keystore needs to be restored from the REKEY backup.Aborting REKEY! Cause: All this hassle started because I accidently deleted the wallet and all wallet backup files too and also forgot the keystore password. There was no way to restore the wallet back. Fortunately in my case the PDB which had encrypted data was supposed to be deco

How to Find VIP of an Oracle RAC Cluster

<<Back to Oracle RAC Main Page How to Find Out VIP of an Oracle RAC Cluster Login clusterware owner (oracle) and execute the below command to find out the VIP hostname used in Oracle RAC $ olsnodes -i node1     node1-vip node2     node2-vip OR $ srvctl config nodeapps -viponly Network 1 exists Subnet IPv4: 10.0.0.0/255.255.0.0/bondeth0, static Subnet IPv6: Ping Targets: Network is enabled Network is individually enabled on nodes: Network is individually disabled on nodes: VIP exists: network number 1, hosting node node1 VIP Name: node1-vip VIP IPv4 Address: 10.0.0.1 VIP IPv6 Address: VIP is enabled. VIP is individually enabled on nodes: VIP is individually disabled on nodes: VIP exists: network number 1, hosting node node2 VIP Name: node2-vip VIP IPv4 Address: 10.0.0.2 VIP IPv6 Address: VIP is enabled. VIP is individually enabled on nodes: VIP is individually disabled on nodes:

ORA-16905: The member was not enabled yet

<<Back to Oracle DataGuard Main Page ORA-16905 Physical Standby Database is disabled DGMGRL> show configuration; Configuration - DG_ORCL1P   Protection Mode: MaxPerformance   Members:   ORCL1PP - Primary database     ORCL1PS - Physical standby database (disabled)       ORA-16905: The member was not enabled yet. Fast-Start Failover:  Disabled Configuration Status: SUCCESS   (status updated 58 seconds ago) DGMGRL> DGMGRL> enable database 'ORCL1PS'; Enabled. DGMGRL>  show configuration; Configuration - DG_ORCL1P   Protection Mode: MaxPerformance   Members:   ORCL1PP - Primary database     ORCL1PS - Physical standby database Fast-Start Failover:  Disabled Configuration Status: SUCCESS   (status updated 38 seconds ago)

How to Switch Log File from All Instances in RAC

<<Back to Oracle RAC Main Page Switch The Log File of All Instances in Oracle RAC. In many cases you need to switch the logfile of the database. You can switch logfile using alter system switch logfile command but if you want to switch the logfile from all the instances you need to execute the command on all the instances individually and therefore you must login on all the instances. You can avoid this and switch logfile of all instances by just running the below command from any of the instance in RAC database SQL> ALTER SYSTEM SWITCH ALL LOGFILE;   System altered.

ORA-65104: operation not allowed on an inactive pluggable database alter pluggable database open

<<Back to DB Administration Main Page ORA-65104: operation not allowed on an inactive pluggable database SQL> alter pluggable database TEST_CLON open; alter pluggable database TEST_CLON open * ERROR at line 1: ORA-65104: operation not allowed on an inactive pluggable database Cause The pluggable database status was UNUSABLE. It was still being created or there was an error during the create operation. A PDB can only be opened if it is successfully created and its status is marked as NEW in cdb_pdbs.status column SQL> select PDB_NAME,STATUS from cdb_pdbs; PDB_NAME             STATUS -------------------- --------------------------- PDB$SEED             NORMAL TEST_CLON            UNUSABLE Solution:  Drop the PDB and create it again. Related Posts How to Clone Oracle PDB (Pluggable Database) with in the Same Container

ORA-46630: keystore cannot be created at the specified location

<<Back to DB Administration Main Page ORA-46630: keystore cannot be created at the specified location CDB011> ADMINISTER KEY MANAGEMENT CREATE KEYSTORE '+DATAC4/CDB01/wallet/' IDENTIFIED BY "xxxxxxx"; ADMINISTER KEY MANAGEMENT CREATE KEYSTORE '+DATAC4/CDB01/wallet/' IDENTIFIED BY "EncTest123" * ERROR at line 1: ORA-46630: keystore cannot be created at the specified location Cause  Creating a keystore at a location where there is already a keystore exists Solution To solve the problem, use a different location to create a keystore (use ENCRYPTION_WALLET_LOCATION in sqlnet.ora file to specify the keystore location), or move this ewallet.p12 file to some other location. Note: Oracle does not recommend deleting keystore file (ewallet.p12) that belongs to a database. If you have multiple keystores, you can choose to merge them rather than deleting either of them.

Starting RMAN and connecting to Database

  <<Back to Oracle Backup & Recovery Main Page Starting RMAN and connecting to Database Starting RMAN and connecting to Database To start RMAN you need to set the environment and type rman and press enter. You can connect to database either using connect command or using command line option. using command line option localhost:$ export ORACLE_HOME=/ora_app/product/18c/dbd2 localhost:$ export PATH=$ORACLE_HOME/bin:$PATH localhost:$ export ORACLE_SID=ORCL1P localhost:$ rman target / Recovery Manager: Release 18.0.0.0.0 - Production on Sun Apr 4 08:11:01 2021 Version 18.11.0.0.0 Copyright (c) 1982, 2018, Oracle and/or its affiliates.  All rights reserved. connected to target database: ORCL1P (DBID=4215484517) RMAN> using connect option localhost:$ rman RMAN> connect target sys@ORCL1P  target database Password:******** connected to target database: ORCL1P (DBID=4215484517) NOTE: To use connect command you need to ensure that  you have proper TNS sentry for database (ORCL

How to Attach to a Datapump Job and Check Status of Export or Import

<<Back to Oracle DATAPUMP Main Page How to check the progress of  export or import Jobs You can attach to the export/import  job using ATTACH parameter of oracle datapump utility. Once you are attached to the job you check its status by typing STATUS command. Let us see how Step1>  Find the Export/Import Job Name You can find the datapump job information from  DBA_DATAPUMP_JOBS or  USER_DATAPUMP_JOBS view. SQL> SELECT OWNER_NAME,JOB_NAME,OPERATION,JOB_MODE,STATE from DBA_DATAPUMP_JOBS; OWNER_NAME JOB_NAME                       OPERATION            JOB_MODE   STATE ---------- ------------------------------ -------------------- ---------- ---------- SYSTEM     SYS_EXPORT_FULL_02             EXPORT               FULL       EXECUTING OR You can also find the job name for export/import in logfile in beginning itself. Step2>Attach to the Job and check status One you get the Export/Import Job Name attach the job and check its status. You can attach or det

Step by Step how to Create Virtual Machine using Virtualbox

<<Back to Linux Main Page How to Create New Virtual Machine Using Oracle Virtual Box Step1:   Open Oracle Virtual Box --> Click New Provide Name, Type and Version as shown in the image below and click Next  Step2:  Adjust memory (RAM) as per the requirement and availability   and click Next.  NOTE:- Remember to leave enough memory for the host OS to work properly.  Step3:   Select the option to create a new virtual hard drive and click "Create"  (erzeugen) button.  Step4:  Accept Default and click next (weiter)   Step5: Accept the dynamically allocated option by clicking the "Next" (weiter) button.  Step6:  If you don't want to use the defaults, enter the required location, name and size of the virtual disk and click the "Create" (erzeugen) button. Note:- At this point your virtual machine is created and ready for OS installation Preparing the Virtual machine for Oracle RAC Installation Step1:   Select t