Home

Lullabot

Lullabot Ideas

We know stuff. We empower you to know stuff too.

Drupal Module Development Deep Dive Week

London, UK
September 20-24, 2010

Using the VI Editor

Blog by Karen StevensonJanuary 28, 2010 - 3:48pm

I'm a relatively recent Mac convert and I still stumble a bit with some command line tricks. In particular, when I need to use VI to edit things I always have to look up the commands to edit and save my text.

So here is my cheat sheet, in a public place that is easy to find :-)

  • Type vi FILENAME to use the vim editor to alter this file.
  • Navigate to the line that needs to be changed or added and make your changes.
  • Apparently the exact keys that control navigation vary. On my computer, the following worked:
    • Down - the enter key
    • Right/Left - left/right cursors
    • Delete a letter - the 'x' key
    • Insert letters - the 'i' key
  • When finished editing, do the following: ESC :wq!

That's my short and sweet blog for the day.

Comments

WC (not verified) on March 11, 2010 - 5:26pm

Excellent

Thanks Karen and also Imre, that's actually really useful for a relatively new mac user such as myself.

Imre (not verified) on March 5, 2010 - 9:36am

Most useful vi command

'ZZ' is the same as ':wq!' but much quicker!

It's the easiest way to save and exit.

Stefan (not verified) on February 3, 2010 - 10:19am

Haha, a new release of the

Haha,

a new release of the editor-war. I thought that settled at least 15 years ago.
But once again everybody has something to say about the "best" editor (including me).
I wouldn't even consider using vi as a development tool (I like emacs much more for that).
But for some small changes on a remote machine (console only) you should definitely know the most important shortcuts for vi.

And don't forget that the most useful editor is ed !!
s. http://www.gnu.org/fun/jokes/ed.msg.html
and look at the consistent interface :-D

Bevan (not verified) on February 2, 2010 - 4:39pm

This page was useful for me:

This page was useful for me: http://www.perlmonks.org/?node_id=333737

Though vimtutor looks better!

Anonymous (not verified) on February 2, 2010 - 10:56am

how to actually edit text

# Navigate to the line that needs to be changed or added and make your changes.

to do this you'll probably need to hit 'a' or 'i' or 'o' to get into editing mode...

Mike Scearce (not verified) on February 1, 2010 - 1:42pm

Is Mac OS considered the "real" dev platform for Drupal?

I need to know so I can change if I need to. It is not made clear anywhere in the Drupal info I have seen, and I have been Drupal'ing for nearly two years.

Is it a better and more useful platform?

Just looking for the best practices. I do not have an axe to grind with any OS. Someone needs to just come clean with the easiest way to maintain and build.

Anonymous (not verified) on February 1, 2010 - 3:08pm

Mac OS X is NOT the official developer environment

However many web developers choose to use Mac OS X for many reasons. Personally I like it because of it's UNIX roots, which allows me to use all my favorite command line utilities (svn, cvs, ssh, grep, tail, head, cut, shell scripting, etc, etc) as well as the best variety of design and development tools. There are amazing applications for web development and design available on the Mac – truly innovative, stunning applications that have helped me shape my workflow over the years. The independent developer community is also very strong, and they often produce high quality, beautiful applications. Plus the fact that I can install practically any open source application is obviously something you can't ignore. I natively run apache, php, mysql, cvs, svn, perl, ruby, a package management system, etc, etc).

Of course, to each his own – you should never feel pressured into choosing a tool just because other people use it. Plus with open standards gaining ground there's no reason to choose one platform over another – at least not for 'compatibility's sake' these days. You should make up your own mind – but as a Mac user (and a full-time Linux user from 96-2006) I would strongly suggest you at least *try* the major options instead of just settling for what's in front of you. You may discover a whole new world of opportunities and enjoyment.

Cheers!

Daniel Chvatik (not verified) on February 1, 2010 - 3:53pm

Mac OS X tools

I'm a long time Mac user. When I first started using Drupal, I was surprised by how many Macs I saw - I am used to a Windows dominated world but at most Drupalcon's Macs seem to almost be the majority. Now I am starting to understand why:

The tools just seem a lot better on the Mac. This is not an inherent flaw of the PC, just no one seems to care to make better cools for the PC. For beginners, Coda and Transmit would be a great example. I still have to find a decent FTP client for the PC. Also, I found a PC user today desperate to try to apply a patch to a module. In the end, the PC programmer skyped me the files, I ran the patch command in the Terminal and skyped the files back. Seemed easier and faster than to get the setup working on the PC. :)

-Daniel
Urbopedia

jimcaruso (not verified) on February 1, 2010 - 11:14am

Nice that vi will always be there

It's nice that you can count on vi being on *NIX systems, but I have a strong preference for BBEdit (a Mac OS tool which I've used for a long time). px42 is very wise to suggest you don't edit live on the server, which usually means you have no backup.

px42 (not verified) on February 1, 2010 - 9:57am

do not edit on live server

try to not edit things in live server, it`s very easy to log in ssh and edit files, but is a very dangerous thing

Cary Gordon (not verified) on January 29, 2010 - 9:35pm

So many commands, so little time

I love vi(m)

:x - save and quit
D - cut to end of line

And then there are the shell commands! (:sh and beyond)

wla (not verified) on January 29, 2010 - 2:02pm

Take a look at Coda

Even if it costs a few bucks, take a look at the Coda editor for the Mac. It has an FTP-Client embedded which allows you to edit staight on the webserver which is quite handy.

Just one note to the vi command
:%s/find/replace/ You can use any character instead of the '/' als long as it's not in either string. This command replaces just the first occurence in a line. If you want to replace all occurences in the file use the command :%s/find/replace/g

Anonymous (not verified) on February 1, 2010 - 3:14pm

regarding Coda

I wouldn't tout "editing off the server" as Coda's strongest feature.

Among it's many useful features, it provides:
Site/project management
Integrated SVN support
Buit-in live web preview
Live collaborative editing
Built-in developer books (PHP, HTML, CSS, etc – with the ability to add more books on your own)
Active third-party plugin/scripting developer community
Clips, for inserting frequently used text snippets, with tab-triggering
Awesome Find/Replace that puts grep/regex power into the hands of normal people
And so much more...

Plus Panic created it… and it's under active development.

Granted I think "Coda" is a little off-topic when discussing how to use VI in the command line. I don't think the author was referring to the use of VI as an IDE (at least not in this context).

Cheers!

Adam DiCarlo (not verified) on February 5, 2010 - 1:13am

Coda’s great, but “edit

Coda’s great, but “edit off the server” is not particularly a necessary feature anyway; install FUSE and sshfs and you’re set with any editor. (Grab MacFUSE in OS X.) FUSE/sshfs lets you mount a filesystem on any machine you have ssh access to. (There’s an ftpfs too, but I don’t think it’s quite so stable (FTP sucks in general)).

Also, check out xdebug support for whatever editor you use—being able to debug your code (step through it, examine variables during execution) will increase your productivity significantly. It’s more than worth the time it takes to set up. (You need to change server settings to use it, though, so if your hosting sucks, just run a *AMP stack locally.)

Matt Petrowsky (not verified) on January 29, 2010 - 2:08am

Vim is cool, but Vimperator is hot!

For anyone considering using vim and you like the whole concept of key bindings. You've got to give Vimperator a try. It really does make using Firefox a kick in the pants!

Vimperator for Firefox.

I've actually done a few videos about using it on YouTube.

Vimperator - Following Links
Vimperator SearchSite Command

EclipseGc (not verified) on January 29, 2010 - 12:15am

Some of my favorite commands

yy -- yank(copy) a line
#yy -- yank # of lines
dd -- Cut a line
#dd -- Cut # of lines
yw|dw -- yank|cut word (respectively)
p -- paste whatever's in the buffer
shift+g -- goto the end of the document
:# -- goto line #
$ -- end of line
^ -- beginning of line
/string -- find string
:%s/find/replace/

Hope these are handy too! Even my komodo is set to work like vim heh... wow I'm a nerd.

R.J. Steinert (not verified) on January 28, 2010 - 10:46pm

Switching to emacs for nicer debugging

I'm starting to use emacs more than vim for the geben php debugger. The interface has folding for variables. Not the case in Vim's debugger (or at least that I've found). Also has a lighter footprint than using Netbeans or Eclipse for debugging.

http://code.google.com/p/geben-on-emacs/wiki/ScreenShots

http://2bits.com/sites/2bits.com/files/vim-debug-eval-variable.png

Anonymous (not verified) on January 28, 2010 - 8:13pm

Gotta start somewhere, right?

Yes, vimtutor. Its very, very helpful. Check it out.

Start with understanding modes, H J K and L, and what w and q do individually. You'll start to love vim more and more each time you use it.

Anonymous (not verified) on January 28, 2010 - 7:29pm

Typical Mac user...

Typical Mac user...

wik (not verified) on January 28, 2010 - 5:14pm

Vim can do much more than

Vim can do much more than that and just as you like it ;)
For example, look for "VIM for (PHP) Programmers" at http://zmievski.org/talks/

Lior Kesos (not verified) on January 28, 2010 - 5:08pm

vimtutor

Most linux distributions (and also my mac) have the vimtutor command installed.
It's a nice long file which is a great tutorial for vi and because it opens the text in vi it becomes a resoirce you can practice and learn while your browsing the tutorial
If you're into programming and you think you might use vi, do yourself a favor and run vimtotor - he best 30 minutes you can invest.
Lior

January 28, 2010 - 6:13pm Karen Stevenson

Handy!

I didn't know anything about vimtutor. Just type vimtutor into the command line and you jump into a great vim tutorial. Very cool!

Joachim (not verified) on January 28, 2010 - 5:00pm

Or just use nano, which has

Or just use nano, which has an interface that you don't have to wire into your brain with needles ;)

Recent

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Command Line Basics: More Editing with Vi/Vim

Video 8.31.2010

Lullabot's Back to School Sale

Blog 8.30.2010

Popular

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Photo galleries with Views Attach

Article 6.01.2009

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Announcing BeautyTips, a jQuery Tooltip Plugin

Article 10.20.2008

Install a Local Web Server on Ubuntu

Video 11.14.2007
 
  • Home
  • Services
  • Events
  • Ideas
  • Store

Connect the Bots:

Twitter Facebook YouTube blip.tv All Posts Newsletter
  • Ideas
  • Blog
  • Podcasts
  • Videos
  • About
  • Contact
  • Jobs
  • Services
    • Training
  • Events
    • Training Workshops
    • Other Events
    • Conferences
    • Calendar
  • Products
    • Videos
    • Books
    • Swag
  • Ideas
    • Blog
    • Podcast
    • Videos
  • About
    • Philosophy
    • Team
    • Presskit
  • Contact
    • General
    • Work Inquiries
    • Mailing List