Sunday, December 25, 2016

Essentials of Vi editor - part 2

In the previous post we looked at some of the basics of the vi editor. In this post let's walk through searching, replacing and undoing.

Search and Replace
------------------------------------------------------------------

/text - searches the text.
?text - searches backward
n - repeats the previous search
N - repeats the previous search in backward direction

. matches any single character e.g - /a.c matches both abc, adc etc. Doesn't match 'ac'
\ has special meaning. e.g - /a\.c matches a.c exactly
   e.g - /a\\c matches a\c, /a\/c matches a/c
^ - matches line beginning. e.g - /^abc matches lines beginning with abc
$ - matches line ending e.g - /xyz$ matches lines ending with xyz
[] - matches single character in a set. e.g - /x[abc]y matches xay, xby and xcy
e.g - /x[a-z]y matches xzy, xay etc
e.g - /x[a-zA-Z]y matches xay, xAy etc
e.g - /x[^a-z]y // matches x followed by anything other than a lowercase letter followed by y. Therefore 'xay' doesn't match, but xBy matches.
* - zero or more matches. e.g - xy*z matches xyz, xyyyyz and also xz
\( \) - e.g - /\(xy\)*z matches xyz, xyxyx, xyxyxyz etc
/<.*> - matches <iwhewoip> and <my_name> etc
/<[^>]*> - matches anything in between <>

:s/old/new/   - replaces the first occurrences of old to new on current line
:s/old/new/g - replaces all in the current line

:%s/old/new/   - replaces the first occurrences of old to new of every line in the document
:%s/old/new/g - globally replace all occurrences in the document


You may use any special character other than / for delimitation. For example you may use | or ;

Few special examples.
:%s/test/(&)/g - Here the replacement string is (&), Here the & says the current match. Therefore whatever test words in the document will be put into parenthesis as in (test) 

Undoing
------------------------------------------------------------------

u - undoing the last change in command mode
Ctrl + r - Redo the last change
U - undo all fhe changes in the current line
. (period) - Repeats last change in your cursor locations

yy - yanks (copy) a line (Similar to dd like delete equivalents) - the yanked texts goes to vi's buffer not to the OS clip-board
yw - yanks a word (just like dw deletes a word)
p - pastes the yanked text after the cursor
P - pastes the yanked text before the cursor


------- Update - Youtube tutorial of the above (04/01/2023) ---------
Youtube Video

1 comment:

  1. This way my pal Wesley Virgin's biography begins in this SHOCKING AND CONTROVERSIAL VIDEO.

    Wesley was in the military-and soon after leaving-he revealed hidden, "self mind control" tactics that the CIA and others used to get everything they want.

    THESE are the exact same methods lots of famous people (notably those who "come out of nowhere") and elite business people used to become wealthy and successful.

    You probably know how you use less than 10% of your brain.

    That's really because most of your brainpower is UNCONSCIOUS.

    Maybe this thought has even taken place IN YOUR own head... as it did in my good friend Wesley Virgin's head around seven years back, while driving an unregistered, beat-up garbage bucket of a car without a driver's license and with $3.20 on his bank card.

    "I'm so frustrated with living check to check! When will I get my big break?"

    You've taken part in those types of conversations, right?

    Your own success story is waiting to happen. You just need to take a leap of faith in YOURSELF.

    Take Action Now!

    ReplyDelete