Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

Essentials of Vi editor - part 2

$
0
0

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

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


Viewing all articles
Browse latest Browse all 11063

Trending Articles