Monday, February 13, 2012

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.

No comments:

Post a Comment