vi – A Man’s Best Friend

Most of my computer time is spent on my Mac inside the gorgeous OS/X GUI using the tools that are necessary for me to run my business.  Many of my side projects, however, take me back to my beloved Bash Shell which I can use on my Mac, where it gets plenty of use, but is especially used on my many Linux servers, all of which are run without a fancy GUI (Gnome, KDE, X, etc).

If you’re not familiar with Bash, it is a Unix shell that is part of the GNU Project and is a replacement for the Bourne shell.  Bash has been distributed widely as the default shell for numerous Linux distributions, various BSD distributions and Mac OS/X.  Simply put, “Bash is a command processor that’s typically run in a text window, allowing the user to type commands which cause actions. Bash can also read commands from a file, called a script. Like all Unix shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration.”1 Bash is a POSIX shell with a number of extensions. The foundation of bash such as its keywords, syntax and other basic features were copied from Bourne Shell. Other features, e.g., history, were copied from C Shell and Korn Shell.

As I manage my Linux servers the tool that gets the most use, by far, is the text-editor vi. Many of today’s younger Linux geeks run full GUIs on their boxes.  If they work at the command line at all, they often use “simpler” editors such as Pico or nano but for me, I prefer vi – thank you very much!!

Anyone with even a casual understanding of the *nix world knows that there are many-a-topics that can start a nasty flame-war.  One such topic is vi -vs- Emacs and if you’re a Hacker on any level then you’re obliged to pick a side and hold your ground (I’m only kidding … let’s all be happy ;)

Want to be a respectable “hacker“? Learn to use vi or Emacs and you’ll have my respect!

Following are few of the basic vi commands I use on a regular basis:

Starting vi

vi filename edit filename starting at line 1
vi -r filename recover filename that was being edited when system crashed

 

Exiting vi

:x<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
:q<Return> quit (or exit) vi
:q!<Return> quit vi even though latest changes have not been saved for this vi call

 

Moving Around

1G<Return> move cursor to first line in file
nG<Return> move cursor to line n
G<Return> move cursor to last line in file

 

Paging

^f move forward one screen
^b move backward one screen

 

Adding & Changing Text

i insert text before cursor, until <Esc> hit
I insert text at beginning of current line, until hit
a append text after cursor, until <Esc> hit
A append text to end of current line, until hit
o open and put text in a new line below current line, until hit
O open and put text in a new line above current line, until hit

 

Deleting Text

x delete single character under cursor
Nx delete N characters, starting with character under cursor
dw delete the single word beginning with character under cursor
dNw delete N words beginning with character under cursor;
D delete the remainder of the line, starting with current cursor position
dd delete entire current line
Ndd delete N lines, beginning with the current line;

 

Copy & Paste

yy copy (yank, cut) the current line into the buffer
Nyy copy (yank, cut) the next N lines, including the current line, into the buffer
p put (paste) the line(s) in the buffer into the text after the current line

 

Finding Text

/string search forward for occurrence of string in text
?string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction

 

Until next time – GEEK OUT!

~GT~

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.