Showing posts with label Linux vi editor. Show all posts
Showing posts with label Linux vi editor. Show all posts

Tuesday, 13 August 2019

Linux - How to Copy, Cut, and Paste in Vi

In this article, we’ll show you how to copy, cut and paste in vi.
This will be explained both selecting text as using movement commands. We’ll also tell you how to copy and cut multiple chunks of text.

1. Introduction

To remember easily the commands used for copying and cutting in vi, it’s useful to know the words they come from:
  • d comes from delete, which you usually know as cut.
  • y comes from yank, which you usually know as copy.
  • p comes from paste, so nothing new here.
Keeping this in mind, you may find it easier to memorize the commands.

2. Selecting, Copying (or Cutting) and Pasting Text

We’ll start with normal mode and position the cursor at the start of the text that we want to copy. Then we’ll do the following steps:
  • Press v to start selecting.
  • You can also use V (capital v) to select whole lines or Ctrl+v to select rectangular blocks.
  • Move the cursor to the end of the text you want to copy.
  • Press y to copy the text (or d to cut it).
  • Move the cursor where you want to paste the text.
  • Press p to paste the text after the cursor (or P (capital p) to paste it before the cursor).

3. Copying and Cutting with Movement Commands

As well as you can copy and cut by selecting the text that you want to, you can also use movement commands to specify what text will be selected. This way you can do:
  • yy or `Y — copy the whole line (including the new line character).
  • y$ — copy from the cursor to the end of the line (excluding the new line character).
  • y0 — copy from the cursor to the start of the line.
  • yiw— copy the current word (excluding surrounding whitespace).
  • yaw— copy the current word (including surrounding whitespace).

4. Multiple Copying or Cutting

To copy or cut several chunks of text at the same time, you’ll need to use vi’s registers. A register is a location in Vim’s memory identified with a single letter. A double quote plus character is used to specify that the next letter typed is the name of a register (so "a would be the a register).
After this short explanation, for doing this you should type "ay and yank that word to the a register. Then you’ll be able to paste that word anywhere in the text using "ap" (or“aP` if you want to paste it before the cursor).

4.1. A Little More About Vi’s Registers

  • By default, all copying and cutting operations are stored in the unnamed registry "" (also named quotequote).
  • A double quote plus an uppercase character is used to append content to a register(with "Ay you would append a yanked text to the a register).
  • There are more default registers, some of them are:
  • "+ for Linux clipboard.
  • "* for Windows clipboard.
  • "0 will store the text of the last yank.
  • "1 will store the text from the last delete.
  • All current registers can be listed the list of all of them with the :reg command.

5. Conclusion

At this point, we’ve learned how to copy and cut text in vi.
To sum it up, you need to v select, y copy, d cut and p paste.

Monday, 5 August 2019

Linux - vi editor

vi editor is a visual text editor. It is pronounced as V and I editor. Here we would be learning how to use the vi editor, how to exit vi editor. vi shows you part of your file and allows you to enter commands that change something (add new stuff, delete a char or line, etc).

vi has a couple of modes:
command mode: move the cursor around, move to a different part of the file, issue editing commands, switch to insert mode. This mode enables you to perform administrative tasks such as saving files, executing commands, move the cursor around, move to a different part of the file, issue editing commands, switch to insert mode. In this mode, whatever you type is interpreted as a command.
insert mode: whatever you type is put in the file (not interpreted as commands).
when you first start vi you will be in command mode.
how to exit vi editor
Now we would be seeing the command for How to start the vi editor,how to exit vi editor, How to manipulate text in editor etc

When Starting VI

vi filenameEdits filename or open up a new file  example vi x.sh
vi -r filenameEdits last save version of filename after a crash
vi + n filenameEdits filename and places curser at line n  example vi +2 x.txt
vi + filenameEdits filename and places curser on last line example vi + x.txt
vi +/string filenameEdits filename and places curser on first occurance of string
vi filename file2 …Edits filename, then edits file2 … After the save, use :n

how to exit vi editor


ZZ or :wq or ðŸ˜¡Saves and exits VI
:wSaves current file but doesn’t exit
:w!Saves current file overriding normal checks but doesn’t exit
:w!file Saves to file overriding normal checks but doesn’t exit
:n,mw fileSaves lines n through m to file example :2,10w j.txt
:n,mw >>fileSaves lines n through m to the end of file
:qQuits VI and may prompt if you need to save
:q!Quits VI and without saving .If you have done some mistake and want to quit without making any changes
:e!Edits file discarding any unsaved changes (starts over)
:we!Saves and continues to edit current file

Inserting Text

iInsert before cursor
IInsert before line
aAppend after cursor
AAppend after line
oOpen a new line after current line
OOpen a new line before current line
rReplace one character
RReplace many characters
:rfile Reads file and inserts it after current line
:nrfile Reads file and inserts it after line n

Cursor Movement


hMove left
jMove down
kMove up
lMove right
wMove to next word
WMove to next blank delimited word
EMove to the end of Blank delimited word
0 or |Move to the beginning of the line
n|Moves to the column n in the current line
$Move to the end of the line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file .Suppose you want to move to 100th line then 100G
:nMove to nth line of the file
HMove to top of screen
nHMoves to nth line from the top of the screen
MMove to middle of screen

Deleting Text

DDelete to the end of the line
d$Deletes from the cursor to the end of the line
dd or :dDelete current line
ndwDeletes the next n words starting with current
ndbDeletes the previous n words starting with current
nddDeletes n lines beginning with the current line. Suppose we want to delete 100 lines then 100dd
:n,mdDeletes lines n through m
“npRetrieves the last nth delete (last 9 deletes are kept in a buffer)

Yanking Text

yyYank the current line
:yYank the current line
nyy or nYPlaces n lines in the buffer-copies

Putting text

pPut after the position or after the line
PPut before the position or before the line

vim search command

/stringSearch forward for string like error
?stringSearch back for string
nSearch for next instance of string
NSearch for previous instance of string
%Searches to beginning of balancing ( ) [ ] or { }
?strFinds in reverse for str
:set icIgnores case when searching. Suppose you want to search for failure ,now we dont the case it will happen,so setting this will help in finding all the occurence
:set noicPays attention to case when searching
:n,ms/str1/str2/optSearches from n to m for str1; replaces str1 to str2; using opt-opt can be g for global change, c to confirm change (y to acknowledge, to suppress), and p to print changed lines
&Repeats last :s command
:g/str/cmdRuns cmd on all lines that contain str
:g/str1/s/str2/str3/Finds the line containing str1, replaces str2 with str3
:v/str/cmd Executes cmd on all lines that do not match str
Examples of vim search command
A file x.txt is given ,we do below search strings on it
/fmw_homesearch forward for fmw_home in the file
?fmw_homesearch backward for fmw_home in the file
nrepeat previous search
Nrepeat search in opposite direction
/, ?repeat search forward or backward

Replace
It is a very function which is used in vi. Many times you have to replace certain field with other ,that time it is very useful
The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command (below).
:s/pattern/string/flags Replace pattern with string according to flags.
g Flag – Replace all occurences of pattern
c Flag – Confirm replaces.
& Repeat last :s command
It can be used like this also
:s,pattern,string,flags
:s^pattern^string^flags
Examples of Replace
:s^/oracle^/applmgr^gThis will replace /oracle with /applmgr everywhere in the file
s,^a,^b, gThis will replace ^a with ^b everywhere in the file
:2,8/this/s//thatreplace first occurrence of `this’ with `that’ in lines 2 through 8
:2,$/this/s//thatreplace first occurrence of `this’ with `that’ in lines 2 through the end
:g/\/home\/me/s//\/r2\/you/greplace /home/me with /r2/you throughout the file
: 33,224s/^/hh/insert the string ‘hh’ at the beginning of lines
:%s/{TAB}*$//Strip blanks at end of line:
Ranges
Ranges may precede most “colon” commands and cause them to be executed on a line or lines. For example :3,7d would delete lines 3-7. Ranges are commonly combined with the :s command to perform a replacement on several lines, as with :.,$s/pattern/string/g to make a replacement from the current line to the end of the file.
:n,m Range – Lines n-m
:. Range – Current line
:$ Range – Last line
:% Range – All lines in file
:g/pattern/ Range – All lines that contain pattern
vi cheat sheet for Shell Functions
:! cmdExecutes shell command cmd; you can add these special characters to indicate:% name of current file# name of last file edited
:!!Executes last shell command
:r! cmdReads and inserts output from cmd
:f fileRenames current file to file
:w !cmdSends currently edited file to cmd as standard input and execute cmd
:cd dirChanges current working directory to dir
:shStarts a sub-shell (CTRL-d returns to editor)
let us take the examples of Executing Unix commands in vi:
Any UNIX command can be executed from the vi command line by typing an “!” before the UNIX command.
Examples:
“:!pwd” – shows your current working directory.
“:r !date” – reads the results from the date command into a new line following the cursor.
“:r !ls -1” – Place after the cursor, the current directory listing displayed as a single column.
Files
:w fileWrite to file
:r fileRead file in after line
:nGo to next file
:pGo to previous file
:e fileEdit file
!!programReplace line with output from program
Editing multiple files:
vi file1 file2 file3
:n Edit next file (file2)
:n Edit next file (file3)
:rew Rewind to the first file (file1)
VI Settings
Note: Options given are default.
To change them, enter type :set option to turn them on or :set nooptioni to turn them off.
To make them execute every time you open VI, create a file in your HOME directory called .exrc and type the options without the colon (:) preceding the option :set ai Turns on auto indentation
:set all Prints all options to the screen
:set dir=tmp Sets tmp to directory or buffer file
:set ic Ignores case when searching
:set list Shows tabs (^l) and end of line ($)
:set nu Shows line numbers
:set ro Changes file type to “read only”
:set showmode Indicates input or replace mode at bottom
Some other commands
u – Undo the latest change.
U – Undo all changes on a line, while not having moved off it (unfortunately).
:u – Undo last substituion on line (only one)
^G – Give file name, status, current line number and relative position.
:[x,y]s/
//g – Substitute (on lines x through y) the pattern
with ,`g’ for `global’
:nnpu paste after line nn





Friday, 2 August 2019

Vi Editor Commands in Linux

Vi editor

The Vi editor is the text editor mostly available in all Unix and Linux flavors. This editor is used to create the files and modifies the files.
In this VI indicates that visual interface.
In the new versions of Linux, vim editor is coming which is Vi improved version.

There are three modes in vi editor
  • Command mode
  • Insertion mode
  • Execution mode
By opening the file using vi editor first it will go to command mode.

Command mode

In command, we can modify the file like copy, paste and delete the lines etc.,
Press ctrl+R to redo the operation
/<search_word> is used to search a particular word in the document or file.
The subcommands used to do particular operations in the file are as follows.

CommandOperation
Esc+cwTo change the word
Esc+dwTo delete the word
Esc+rTo replace the character
Esc+shift+rTo replace the line
Esc +aTo move courser to at the end of the word
Esc+shift+aTo move courser to the end of the line
Esc+oTo add the line below the courser
Esc+shift+oTo add the line above the courser
Esc+shift+ggTo move courser last page to the last line
Esc +ddTo delete the line
Esc+nddTo delete the number of lines
Esc+shift+oTo add the line above the courser
Esc+ yyTo copy the line
Esc+pTo paste the copied lines
Esc+nyyTo copy the number of lines
Esc+uTo undo the operation
Esc+ctrl+RTo redo the operation

Insertion mode

By pressing a,I we will go to insertion mode from command mode. To go back to the command mode press Esc button.
You can navigate through the text using the dos keys.

Execution mode

By pressing shift+: we will go to to the execution mode. in the execution mode, we will do the tasks like saving the file, quitting from the file etc.,
The following are the commands in the execution mode to do the particular tasks.

CommandOperation
Shift+Goes from command mode to the execution mode
Shift+set nuTo set line numbers to the file
Shift+set nuTo set line numbers to the file
Shift+:set nonuTo unset the line numbers
Shift+set nuTo set line numbers to the file
Shift+:wq!To save and quit forcefully
Shift+q!To quit without saving forcefully
Shift+:1,$s/old_string/new_string /gTo replace the string