Getting more out of Vim - some tips

Getting more out of Vim - some tips


I'm donning my flame-proof suit for this post. Vim is arguably one of the two most popular text-editors used in the free software world: built on vi (its name stands for VIiMproved) it will be found as a default package in many GNU/Linux distributions. The other popular editor is EMACS (although I am sure there are those who will argue that EMACS is much more than a mere text-editor). I use Vim a lot in my work and have found it to be a little like chess: a moment to learn a lifetime to master. With that in mind here are some of the tips and handy commands I have picked up over the years. I thought I'd pass them on in case they help.

I'm going to assume you are familiar with the basics of Vim: moving the cursor, entering modes and adding/editing content. I'm also going to presume you know what entering a command that starts with a : means. This is not a beginners class, but it's not an expert one either. This is not the place to begin learning Vim - sorry. Some of these were seemingly obvious to me, but unknown to other Vim users I've encountered. The rest are the reverse of that scenario. I've limited myself to ten, because honestly something like this go on forever without such a limit. These are not necessarily the best or most needed tips but they are the ones I found were best received by vim users when they first heard them (including me).

Moving around

  • [{ move to the beginning of the current codeblock
    This one depends upon the language you are coding in, but it works pretty much wherever there are clear demarcations of code blocks. ]} moves to the end of the current codeblock.
  • :<line_num> go to <line_num>
    Okay an obvious one but worth mentioning because I have seen people scrolling up and down to get to a specific line number.
  • Ctrl+O put cursor back to its previous location
    Often I find myself adding break points of making debugging code in several places within a file. Once the debug is finished, finding and removing these can be a pain. Ctrl+O (not zero) will move the cursor backwards within the locations where you changed the file. Think of it like an undo without actually performing the undo. Ctrl+I will move forward in the list of locations.

Editing

  • xp reverse the next two characters
    It's surprising how many typos involve two characters being typed in the w_or_ng order (see what I did there?). Correct them with just two keystrokes.
  • Ctrl+P autocomplete the current word_
    I lose track of how many times I used to be dubugging and find the problem was that I was using two slightly different variable names to refer to the same thing. This command will toggle through the available words in the file and/or dictionary in an attempt to complete the one you have started. Ctrl+P does this by going forward through the list of words. Ctrl+N does it by going backwards.
  • n. repeat the previous command n times
    If you have just made a complex command and you need to repeat it somewhere else in the file you can do so by pressing ".". Giving a number before it will repeat the command that many times.

Multiple files

  • :set wildmode = longest,list change file name autocomplete behave more like BASH
    Most people know you can open another file with :e, write to a different one with :w or open one alongside the current one with :vs. An annoyance to me was always that Vim's autocomplete when specifying file names is to give you the full path of the first file that matched rather than the way BASH does it, which is to stop on the first ambiguity and give you the choice of where to go next. Setting the wildmode option as shown will make Vim behave more like BASH in these instances.
  • -O, -o open multiple files alongside each other_
    I was surprised how many people didn't know this one. When running Vim you can use specify multiple file names to be opened in the same session. Give the -O parameter to have them displayed in a split-view next to each other and -o to have them shown above or below each other. If you are new to working on multiple viewports you can use Ctrl+W and a cursor key to switch between the files.

When you're not coding

There are times when you might use Vim to edit a non-coding file (no really, it's true).

  • g Ctrl+G word count
    Okay so it's a bit more than just that but it's one of the more useful er uses I've found for this command. g then Ctrl+G will display something like Col 179 of 179; Line 23 of 24; Word 791 of 791; Byte 4312 of 4314 at the bottom of the screen. If you select some text first (using v for example) you will be presented with the stats for the selection and not the whole file.
  • :s/pattern//gn number of occurrences of pattern in the file
    Sometimes you need to know how often a word appears in a file. This command will tell you.

As said, this is not an exhaustive list and I appreciate this is a bit of a niche topic and I'm not expecting big reading figures here, but these kinds of tips are the ones you often rack your brain trying to remember so having them in a blog post will hopefully save your little grey cells the effort. One of the things about free software is the multiplicity of options available to you, and I am sure there are plenty who can think of "better" ways to do some if not all of this. If so, feel free to post them as comments here. If you want to read more tips on using Vim (at various levels of expertise) have a look at the Vim Tips Wiki. I'd expect most if not all of these tips to be on there (along with a myriad of others) but it's not where I got them from.

Category: 

Comments

vigor's picture
Submitted by vigor on

"[line_num] G" shaves one keystroke off of ": [line_num] ENTER"

Ryan Cartwright's picture

Doh - obviously when autocompleting a word Ctrl+P[revious] goes backwards through the list and Ctrl+N[ext] goes forwards. Sorry about that must have nodded off when I wrote that bit ;)

--
Equitas IT Solutions - fairness, quality, freedom
http://www.equitasit.co.uk

Author information

Ryan Cartwright's picture

Biography

Ryan Cartwright heads up Equitas IT Solutions who offer fair, quality and free software based solutions to the voluntary and community (non-profit) and SME sectors in the UK. He is a long-term free software user, developer and advocate. You can find him on Twitter and Identi.ca.

Most forwarded

Interview with Dave Mohyla, of DTIDATA

Dave Mohyla is the president and founder of dtidata.com, a hard drive recovery facility based in Tampa, Florida.

TM: Where are you based? What does your company do?
DTI Data recovery is based in South Pasadena, Florida which is a suburb of Tampa. We have been here for over 10 years. We operate a bio-metrically secured class 100 clean room where we perform hard drive recovery on all types of hard disks, from laptop hard drives to multi drive RAID systems.

Anybody up to writing good directory software?

Since the very beginning, directories (of any kind) have had a very central role in the internet. (I have recently grown fond of Free Web Directory. Even Slashdot can be considered a directory: a collection of great news and invaluable user-generated comments. As far as software is concerned, doing a quick search on Google about software directories will return the free (as in freedom) software directories like Savannah, SourceForge, Freshmeat and so on, followed by shareware and freeware sites such as FileBuzz, PCWin Download Center and All Freeware (great if you're looking for shareware and freeware, but definitely less comprehensive than their free-as-in-freedom counterparts).

Interview with Mark Shuttleworth

Mark Shuttleworth is the founder of Thawte, the first Certification Authority to sell public SSL certificates. After selling Thawte to Verisign, Mark moved on to training as an astronaut in Russia and visiting space. Once he got back he founded Ubuntu, the leading GNU/Linux distribution. He agreed on releasing a quick interview to Free Software Magazine.

Is better education the key to finding better software?

I read David Jonathon's article Anybody Up To Writing Good Directory Software? the other day, which got me thinking about software directories in general. As David mentioned, many of the software directories one finds when doing a quick google search are free as in beer, not as in freedom. But what interests me is the software directories that already exist, providing a combination of both free as in beer software, and open source software. Sites such as Freeware Downloads and Shareware Download don't advertise themselves as providing free as in liberty software, but each of them have a good selection of open source software available... if you know where to look.

Most emailed

Free Open Document label templates

If you’ve ever spent hours at work doing mailings, cursed your printer for printing outside the lines on your labels, or moaned “There has got to be a better way to do this,” here’s the solution you’ve been looking for. Working smarter, not harder! Worldlabel.com, a manufacture of labels offers Open Office / Libre Office labels templates for downloading in ODF format which will save you time, effort, and (if you want) make really cool-looking labels

Creating a user-centric site in Drupal

A little while ago, while talking in the #drupal mailing list, I showed my latest creation to one of the core developers there. His reaction was "Wow, I am always surprised what people use Drupal for". His surprise is somehow justified: I did create a site for a bunch of entertainers in Perth, a company set to use Drupal to take over the world with Entertainers.Biz.

Update: since writing this article, I have updated the system so that the whole booking process happens online. I will update the article accordingly!

So, why, why do people and companies develop free software?

More and more people are discovering free software. Many people only do so after weeks, or even months, of using it. I wonder, for example, how many Firefox users actually know how free Firefox really is—many of them realise that you can get it for free, but find it hard to believe that anybody can modify it and even redistribute it legally.

When the discovery is made, the first instinct is to ask: why do they do it? Programming is hard work. Even though most (if not all) programmers are driven by their higher-than-normal IQs and their amazing passion for solving problems, it’s still hard to understand why so many of them would donate so much of their time to creating something that they can’t really show off to anybody but their colleagues or geek friends.

Sure, anybody can buy laptops, and just program. No need to get a full-on lab or spend thousands of dollars in equipment. But... is that the full story?

Fun articles

Santa Claus - the most successful open source project

It dawned on me the other day, as I was shopping for the dozens of gifts it seems I have to buy every December, that Santa Claus is the most successful open source project in history. (Bridget @ Illiterarty would agree with that). Santa Claus is essentially a marketing development that is embodied by everyone who stuffs a sock, gives a gift, hosts a dinner or wishes Merry Christmas over the holiday season.

Most emailed

Editorial

When I first started thinking about Free Software Magazine, I was feeling enthusiastic about the dream. I had Dave, Gianluca, and Alan willing to help me, I had established members of the free software community willing to help me out, I had writers volunteering their time and energy for free, and I had a generous offer from OpenHosting for servers, all before I'd proved myself. There was a sense of excitement in the air, and I thought maybe, just maybe, I could make this work.

Free Software Magazine uses Apollo project management software and CRM for its everyday activities!