Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy

Friday 28 June 2013

Vi - Marks and Registers

June 28, 2013 Posted by Dinesh ,

Marks:

vi has 26 "marks". A mark is set to any cursor location using the m command. 
Each mark is designated by a letter. 
Thusma sets the 'a' mark to the current location, andms sets the 's' mark. 
 You can move to the line containing a mark using the ' (single quote) command. 
 Thus 'a moves to the beginning of the line containing the 'a' mark. 
 You can move to the precise location of any mark using the ` (backquote) command. 
 Thus `s will move directly to the exact location of the 's' mark.

copy/cut/paste :

One can use d,y,p to delete,yank,paste respectively. 
step 1 : set the mark by using m
step 2 : y'a to copy from current position to line where mark a is set.
             y`a to copy from current position to exact location of mark a.
             [ 'd' can be used similarly ]
step 3 : use p to paste the content.

To access all currently defined markers hit :makrs

some other interesting features are there in vi where we can use marks.

# for example if you want to sort a set of lines in vi what you do ?? There is a simple solution for it.
place two marks on two lines. and then you you hit :'a,'b!sort ( ! will execute shell commands on vi)

# if you want to redirect a part of file to another file
place two marks on two lines and then :'a,'b w temp_file. this will copy the content from mark a to b to the new temp_file. if file already exist use :'a,'b w>>temp_file to append.
  
Note: a-z can be used to mark only in same file. use A-Z to mark across the files.. So now copy a line from one file to other file is much more easier !! 

Registers:

vi has 26 "registers". we can use these registers by prefixing  " (double quote)Each register is designated by a letter.
Thus"a starts the register. now its up to you to use this register to copy the content to it or cut the content to it or paste the content from it.

This is more useful when you want to copy lines from multiple locations and paste it in different places.

copy/cut/paste :
Ex:

I am at line 2: "ayy will copy the current line to register to a
I go to line 30:  "bdd will cut the current line to register b.
I am at the end of file : "ap  and "bp  will paste line 2 and line 30 at end of file.

"ap pastes a copy of the 'a' register's contents into the text after the cursor and "aP pastes a copy from 'a' to before the current line.

capital letters can be used to append the content to a register. 
 "Ayy will append the current line to the register A.

To access all currently defined registers hit :reg

We can do some interesting things with registers.
for example if you want to execute a command which is at current line in vi what you do ?? There is a simple solution for it.
Copy the line you want to execute to a register -  "ayy 
Execute the content of register using '@'  -  :@a