Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box?
Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.
Using the VI Editor
January 28, 2010
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 on this post will automatically be closed three months from the original post date.



RSS Feed


Comments
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 ;)
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
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!
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/
Typical Mac user...
Typical Mac user...
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.
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
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.
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
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
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!
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.)
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)
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
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.
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.
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!
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
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...
This page was useful for me:
This page was useful for me: http://www.perlmonks.org/?node_id=333737
Though vimtutor looks better!
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
Most useful vi command
'ZZ' is the same as ':wq!' but much quicker!
It's the easiest way to save and exit.
Excellent
Thanks Karen and also Imre, that's actually really useful for a relatively new mac user such as myself.
Post new comment