Monday, February 13, 2012

VIM - Search and Replace

Assume:
You know some VIM.

VIM has an amazing and daunting search and replace command but we're going to ignore most of it and show you the basic search and replace. 

The Search/Replace command is activated with the command mode (:).  Incidentally, the command mode actually triggers ex which is the command line editor in Unix/Linux.  Ex was the grandfather of Vim (or actually Vi).  Ex was the main reason why so many people wanted to burn their computer to the ground.  It's a line editor which means you only see one line at a time.  In the DOS world, it was Edlin (edit a line).  I actually used Edlin once...notice the Once!

 Anyway, to activate search/replace you actually do (substitute -- which is the actual name of the command). 

:s


That triggers the search replace command and does nothing since you've told it nothing.  The basic thing it does is:

:s/search text/replace text/

So
:s/fred/bob/

will search for fred on the current line and replace it with bob.  I know not very exciting.  VIM can get really complex when you start specifying line numbers in searches but what we really want to start of is a global search and replace with a prompt.  That's what every single editor does on the planet. Here's how.

:%s/search text/replace text/gc

%s says all lines.
/g means global and that means do every single replacement on all lines -- not just the first time we find it (ya like that's useful right?)
/c means prompt you for the replacement.


:%s/fred/bob/gc

Will replace every instance of fred with bob in the entire document with a prompt warning.  (say y or n and ignore the rest for now).  That's about as basic as it gets.

Now, I'm going to give you one more idea before we depart.  Ever had an instance where you want to do a global search and replace BUT on only certain conditions.  For example, I want to replace every instance of Fred with Bob but only if the last name is Sampson.  Hmmm...can't be done on any editor I know...but VIM is NOT just any editor is it?

:g/Sampson/s/Fred/Bob/gc
 
 
In other words, globally search for Sampson but then search next to Sampson for Fred 
and replace it with Bob. Pretty neat hu!
 
 
That's about it.  Hope you enjoyed!!! 

VIM - Command mode

Assume:
You know VIM basics.


Most people poking around in VIM don't realize that the command mode (typing a : or a colon) can really lead to some powerful features.  Now did you know there are several commands that are of great use to VIM available in the command mode.  Here are my favorites.

Directory Listing (:Ex)
That's right.  You can get a directory listing of the current directory or anywhere you want with Ex -- think Execute directory.  Once you execute that you can get a directory listing.  This goes great with the tab command.

:tabnew | :Ex

This will create a new tab (see my tab blog) and give you a directory listing.  Notice the '|' pipe command.  That allows you to string two commands together.

Shell (:sh)
Ever wanted to execute a command from the shell without leaving VIM.  No problem.  Execute this command (sh) and you can get to a shell.  NOTE, just don't load VIM while you're in the shell.  Type quit to get back to VIM. 

Execute a command (!)
Ever wanted to just execute a command instead of a shell.  Use bang (!)

:! ls

That of course is the famous list command.


Quit all :qa
You know about (q) and (wq) and perhaps q!) but did you know about (qa).  That means quit all and it dumps you back to the prompt exiting all files.  Please note that it will prompt for saving the file.

MISC (:q, :wq, :q!)
Just in case, we'll be complete.
q -> means quit
wq -> means write current file and quit
q! -> means don't save the file even if there are changes and quit

There you go.  Not too bad a lesson right and don't forget the tab commands.

Tab commands (:tabnew, :tabn, tabp, tabn (number)
Just as a review...
:tabnew - opens a new tab
:tabn - next tab
:tabp - prior tab
:tabr - rewind to first tab
:tabl - last tab
tabn (number) just to a tab in the list.


Goto line number (: #)
Ever wanted to jump to a line number.  No problem.  Just literally type the line number.
:50 - go to line 50
:20 - go to line 20

That's it for today.