Linux.com

Feature

Tips: Making Vim easy

By Joe 'Zonker' Brockmeier on April 10, 2007 (8:00:00 AM)


Printer friendly page Print
Comment on this article Comments

Vim's flexibility and countless features are a major asset for experienced users, but a challenge for newbies. If you've always wanted to try Vim but were put off by your first attempts, you can start off gradually by getting to know Vim's GUI and easy mode. This article is a primer for those who haven't used Vim much and want to wade in gradually.

If you're new to Vim and vi-like editors, the Vim GUI is probably the best way to get started. You can perform the operations you're familiar with using Vim's keybindings, but when you get stuck, you can use the mouse and the gVim menus to get things done quickly and efficiently.

Even better, when there is a keybinding for a command, it's usually displayed in the gVim menu -- so if you keep performing the same operation, you'll probably pick up the keybinding through repetition and eventually be able to go mouseless -- if that's your goal, of course.

There's little, if anything, you can do using gVim that you can't do in the standard version of Vim, and many operations are easier or more intuitive when you have menus and icons to deal with.

Staring gVim

To start gVim, run gvim or vim -g. If you want to edit a file with gVim right away, you can launch gVim with gvim filename or vim -g filename .

As with Vim, you can also launch gVim with the +n option, where n is the line number you want to start on. So, for instance, gvim +10 filename will bring up gVim with filename loaded and the cursor on line 10.

Another trick is to load gVim at the first instance of a search term. Let's say you want to start editing at the first instance of the word foobar. Use:

gvim +/foobar filename

Once you start gVim, you'll notice that it looks more or less like any other GUI text editor. Like any other editor, it provides several menus, a scrollbar on the right-hand side of the window (which can be moved, or done away with, if desired), and a toolbar at the top of the window for quick access to common functions like Save, Undo, and Search.

Easy Vim

Vim also has an "easy" mode, which isn't touched on too often in Vim tutorials and docs, since it really goes against what most users are looking for from Vim. Vim, as you know if you've used it much, is a "modal" editor, based on Bill Joy's classic vi. Vim has an insert mode, a command mode, a visual mode, and (depending on whom you ask) a few additional modes. Once you get the hang of it, the modal nature of Vim is extremely useful. However, until you get used to it, Vim can be a hassle when trying to figure out simple operations like searching and replacing text.

Easy mode is actually a collection of various Vim options that tell Vim to act like any standard text editor. Instead of using y to "yank" (copy) text, and then pasting text using p or P in normal mode, Vim's easy mode lets you use the standard Ctrl-c to copy text and Ctrl-v to paste text. Anybody who has use Notepad or Microsoft Word for a week or two will be familiar with these shortcuts, and emulating them makes Vim more intuitive for the average user. Check Vim's help for more on easy mode to find out exactly what Vim's doing behind the scenes.

Why would you want to use Vim without access to its modes? Well, Vim still includes a number of spiffy tools and features that aren't dependant on its vi-like modes -- macros, syntax highlighting, sessions, and other Vim goodness that you might not find in an editor like Kate or gedit.

Easy mode also enables a context menu, which isn't present in standard Vim/gVim. When you right-click in the gVim window, you'll see a menu that will allow you to select text by the word, sentence, paragraph, line, or all text in the buffer. You can also use the context menu to copy, cut, delete, and paste text, and to undo operations.

If you like the context menu, but don't want to run Vim in easy mode, you can simply set the mouse behavior using this command:

:behave mswin

Then you'll have the context menu, even when you're not in easy mode.

By default, pressing Esc in easy mode will not put you in command mode. If you decide you want to start using gVim in regular mode, you can go to Edit -> Global Settings -> Toggle Insert Mode, which will put you into command mode. After running this, you'll be able to switch between easy mode and normal gVim/Vim modes. If you want to drop back exclusively to easy mode, toggle it with the menu or run :set im! in command mode.

The rest of this article will assume that you're using gVim in normal mode, but I wanted to point out the easy gVim mode for users who might like to get started with Vim is the friendliest manner possible.

By default, gVim will start with a conservative color scheme of black text on a white background. If you prefer something a bit more interesting, gVim has several color schemes available. Go to the Edit menu and select Color Scheme. You should see about a dozen choices, including blue, darkblue, and peachpuff.

Working in gVim

This probably goes without saying, but since gVim is designed as a GUI text editor, you can select text using the mouse in addition to Vim's usual text selection methods. This means that all you need to do to highlight a letter, word, or sentence is to grab the mouse and highlight the text that you'd like to select. You can also place the cursor anywhere on the screen just by moving the mouse. (You can also enable this in regular Vim running in an xterm or other terminal emulator, but it requires mouse support being built in and is not on by default.)

However, don't forget that the mouse is a clumsy tool compared to Vim's normal selection capabilities. Using v you can start the visual mode for selecting by character, V to select by lines, and Ctrl-v to select text blocks.

It's also worth noting that you have two clipboards in gVim. There's the current selection, which means any text that's highlighted, and there's text copied to the clipboard, which persists after you've selected new text. For example, if you use the mouse to select a sentence and then click the middle button, you'll paste the text that you just selected. But if you select text and use Vim's Edit menu to copy text, it will be stored in the clipboard even when you select new text. This can be useful if you have a string that you'll be pasting several times (say, a letter template) and need to use the standard selection method for other text.

I've written about Vim's viewports before, but viewports are much easier to manage in gVim. You can use the normal Vim keybindings to open a viewport, or you can just go to Window -> New to create a new viewport so you can edit a new file, Window -> Split to split the window using the same file that's already open, or Window -> Split Vertically to divide the viewport vertically instead of horizontally. Using Ctrl-w will move you from viewport to viewport, or you can use the Window menu to move to another viewport, or just click the left mouse button in the viewport you want the cursor to be in.

The really nice thing about viewports in gVim, though, is that you can easily resize them with the mouse. In standard Vim, you need to use Ctrl-w - and Ctrl-w + to resize the windows, but in gVim you can resize them using the mouse or the standard Vim keybindings, depending on which you find easier to use.

Even though gVim is a GUI-based program, the shell is never far away. To get to a shell from a gVim session, just :sh; type exit when you want to get out of the shell. Note that gVim's terminal emulation may leave something to be desired in terms of screen refresh, so you probably want to stick to a regular terminal emulator for most of your work.

gVim's file explorer

You can use gVim to edit remote files almost as easily as using Vim in a remote shell. I usually edit files on my server by using SSH to get a shell on the remote machine and then editing a file, but you can do the equivalent using gVim (or regular Vim) from your local session.

To open a remote file using secure copy (scp), do the following:

:e scp://user@remote.host.net/path/to/file

This will open a secure connection on the remote machine as the user specified. If you have an SSH key set up, you can log in without a password. If you don't have a key set up, you will be prompted for a password each time you save the file, which can be a bit of an annoyance.

Note that this will work with several protocols, so you could substitute sftp, ftp, rsync, and other methods. See the Vim help guide for the complete list.

What if you don't know the name of a remote file, or the exact path? Leave off the filename and you'll be dropped into Vim's file browser:

:e scp://user@remote.host.net/

You can browse through the directories and find whatever file you need to edit. Press Enter once the file you want is highlighted and start editing.

Finally, I would be remiss if I didn't mention gVim's spelling features. If you turn on behave mswin and turn on spellchecking, you can use the context menu to add a word to the dictionary, accept an alternate spelling, or ignore a word for spellchecking purposes. To turn on the spellcheck feature, go to Tools -> Spelling -> Spell Check On. You'll probably need to turn syntax highlighting off for the spellcheck to work properly; you can do that from the Syntax menu.

If you've never used Vim, or if you've used Vim and found it too difficult, try gVim for a few days. It has all of the amenities that you'd want from a boring old text editor, but with all of Vim's features lurking below the surface where you can unleash them at your leisure.

Slashdot Slashdot it!   -   del.icio.us del.icio.us   -   Digg This!

Comments

on Tips: Making Vim easy

Note: Comments are owned by the poster. We are not responsible for their content.

strange abilities Anonymous Coward April 11, 2007 01:46 AM
Well, if you can type blind and if you can rtfm, then vim is great. All IT professionals do have these strange abilities.
Everybody else won't need vim at all. Just use your favorite editor to write your blinky fancy greeting email cards, it's much easier!

#

How Do You... Anonymous Coward April 11, 2007 01:47 AM
How do you delete a line in vim? I use the backspace or delete key and easily delete all the characters on a line, but I can't delete the blank line. I'm all like, WTF?

As for the GUI stuff; if I'm going to use a GUI text editor I'm going to use Kwrite or oowriter, not gVim.

#

Re:How Do You... Anonymous Coward April 11, 2007 01:54 AM
dd in normal mode should do the trick.

#

Intuitive Anonymous Coward April 11, 2007 03:40 AM
dd in normal mode should do the trick.

Indeed. And <ESC><nobr> <wbr></nobr>:dd is so much more intuitive than a backspace or delete key. But, I'm not bitter about it.

#

Re:Intuitive Anonymous Coward April 11, 2007 05:12 AM
You don't use the colon character.

The use of 'dd' is logical, as the first 'd' means "delete" (so 'dw' will delete a word) & the repetition means "operate on the line" (so 'cc' will change the current line). It is at least as reasonable as the equivalent emacs command (ctrl-k, ctrl-k).

If you find this vi syntax esoteric, you can always use delete or backspace in insert/easy mode.

#

Re:How Do You... Anonymous Coward April 11, 2007 02:40 AM
and if you have multiple lines to delete then after dd hold down .

#

Re:How Do You... Anonymous Coward April 11, 2007 02:52 AM
or do 5dd which will erase 5 lines.

most commands in Vim can be multiplied in this way; for example to delete 5 letters in normal mode, do 5x (x being the 'delete' key)

Vim is certainly an improvement over vi, which i struggle to understand some days. Not that I know Vim well yet; gVim here I come! (what, no kVim yet? Thats what *I* want!)

#

Re:How Do You... Anonymous Coward April 11, 2007 02:40 PM
Actually, I think there is a kvim... I remember using something like that in my KDE days. Anyway, it's a command line app... it won't do any harm to use it as such. Just run `vimtutor' and you're set<nobr> <wbr></nobr>:)

#

another way... Anonymous Coward April 11, 2007 03:40 PM
Or select multiple lines in visual mode (shift-v, use normal navigation to move around), then press 'd'.

#

Re:How Do You... Anonymous Coward April 12, 2007 03:44 AM
no no no no no.

You put the cursor at the start of where you want to delete, and if you want to take out 5 lines you simply type 5dd in normal mode.

The problem with just holding d down is you may cut way more than you want if your shell hangs for a few seconds. Just because you don't see anything happening doesn't mean the commands haven't been sent. Without instant feedback you may have cut more than you want.

Then you'd need to take the extra step of typing u to undo it<nobr> <wbr></nobr>:P Of course this only applies when working in a remote shell. Locally the feedback is instant.

-Viz

#

Re:How Do You... Anonymous Coward April 12, 2007 12:05 AM
If you are unable to backspace over blank lines, then you might need to add the following to your<nobr> <wbr></nobr>.vimrc (_vimrc on Windows) file:

set backspace=indent,eol,start

Most vim installations that I've seen do come with a pre-configured init file that sets up things in a pretty intelligently. Perhaps your vim just isn't finding its<nobr> <wbr></nobr>.vimrc file. I would also make sure you are NOT running vim in "compatibility" mode, which makes it behave like traditional vi. (I've been using vim for years and still find vi mode pretty annoying... but it's useful to know if vi is the only text editor available on an OLD Solaris machine your forced to log into occasionally.<nobr> <wbr></nobr>:) )

#

Re:How Do You... Anonymous Coward April 12, 2007 03:40 AM
get out of edit mode [esc] and type dd

-Viz

#

I dont see the point of this class of tutorials Anonymous Coward April 11, 2007 12:41 PM
The people that do not use vim or emacs, are probably more used to Kate or Kwrite or any INTUITIVE editor. What I expect in a tutorial like this is to see some capabilities that emacs or vim can and that Kate for example cannot do, and that I would be beggin the developers of Kate to implement.

Diego

PS: my favorite editor in the console mode is either "midnight commander's editor" or "joe"

#

Re:I dont see the point of this class of tutorials Administrator April 11, 2007 08:56 PM
Well, you have to start somewhere, and the article suggests vimtutor. I think an article series on "advanced vim" would be a good thing too. This article is specifically about gvim - many people don't even realize that vim HAS a gui option. for those people, this article would be useful.

Frankly, as a vi user for over 20 years, I don't see the point of gvim. We weren't "allowed" to use emacs at school as the old vax 11/780 didn't have the memory for it...

#

If you're not hooked, don't start Anonymous Coward April 11, 2007 01:43 PM

All flavors of vi are for people whose brains have been so damaged by using vi that they don't like anything else. There is absolutely no point learning vi in 2007. Learn one of the newer editors, or emacs, which is still a good choice for progarammers.

#

Re:If you're not hooked, don't start Anonymous Coward April 11, 2007 06:09 PM
Hm. I don't know. I don't see any reason for being damaged when using VIm. Someone just find this type of editors better than others. I like to be able to change the text or move very quickly just pressing a few keys. That was the reason I ended up with VIm when I was learning to use text editors in Linux. According to the polls I'm not alone<nobr> <wbr></nobr>:-).

#

Re:If you're not hooked, don't start Anonymous Coward April 11, 2007 09:28 PM

And no doubt it now seems quite natural to you that lower-case 'L' is the command for moving the cursor to the right.

(Just about everyone who has not suffered brain-injury by using vi expects lower-case 'L' to be the command for inserting a lower-case 'L' into the document at the current cursor position.)

#

Re:If you're not hooked, don't start Anonymous Coward April 11, 2007 10:34 PM
If vim had a real vi compatibility mode we would be set! Honestly, vi/vim is for folks who like to keep the hands on the keyboard to type and navigate. If you dont mind moving to a mouse or Ctrl/Alt keys the go with something else.

#

Re: VIM != VI Administrator April 12, 2007 04:21 AM
In vim, the arrow keys work just fine...

And while in INSERT mode, the L key DOES insert an L.

Maybe you should think before you waste your time.

-Jeremy

#

Re:If you're not hooked, don't start Anonymous Coward April 12, 2007 10:10 PM
Sysadmins should learn vi and maybe even ed.
When things go really really wrong, sometimes those are the only tools left.

Otherwise, I agree with you.

#

VIM has Emacs envy Anonymous Coward April 11, 2007 03:13 PM
It would seem that, unhappy with vi's simplicity, VIM developers decided to turn it into a complex monstrosity - not unlike Emacs. The catch is, Emacs is more versatile.

      Thus, if I want to use a big editor with lots of features I'll use Emacs. For the simple tasks that I do most of the time, vi (or a simple clone thereof) is more than enough. I don't want to waste any time with the VIM white elephant.

#

Re:VIM has Emacs envy Anonymous Coward April 11, 2007 09:10 PM
vi is everywhere and is installed by default. Emacs is generally not. While I agree that emacs has a little more versatility, the fact is that both vim and emacs are very powerful editors for programming.

I have never found emacs to have compelling enough features for me to use it exclusively. So what if you can read mail it it - mutt is better. So what if you can run a shell in it - a new window is better. In fact, for any application built on emacs, you can find a stand-alone program that works better. Yeah, I can cut my 20' x 40' lawn with a $2,500 lawn tractor, but why would I?

If you want to talk about white elephants, the emacs binary is over twice as large, and requires all sorts of additional scripts to do what vim has built-in. And elisp? Give me a break. I'll take perl any day as a scripting language.

#

Re:VIM has Emacs envy Anonymous Coward April 11, 2007 09:55 PM
A white elephant is something that comes with drawbacks that make it not really worth the while. It is in this sense that VIM is a white elephant, whereas Emacs (big and all) is not.

    Interestingly, I also do not think much of Perl as a scripting language, or anything else at that - even elisp would be more acceptable to me.

#

New VIM user here...on a Mac! Anonymous Coward April 11, 2007 11:22 PM
A lot of posts here seem to be down on VI because it's complicated to move around, the modes, etc. I am a former Emacs user on Unix, and using OSX I have a choice of awesome editors (TextMate, BBEdit, TextWranger, etc.) but I have chosen to go with VIM. Why?

Very simply: VI is standard equipment on Unixes of all stripes; Emacs is not. The Unix boxes I work on (Solaris and NetBSD) a lot of time do not have Emacs installed, and the admins won't install it. The standard reason is that VI is less resource-intensive than Emacs and doesn't require chasing down either a pre-built binary or compiling from scratch (and the Solaris boxes don't have a C compiler).

So when I'm logged into a Unix machine I'm using VIM. But why on the Mac? In this case, it's a speed issue...Because VIM is not mouse-oriented, I can type and bang out stuff fast without my hands leaving the keyboard for the mouse. It's amazing how just the act of moving your hand to the mouse can alter your concentration.

Is VIM easy? No. No it's not. I keep a cheat sheet nearby, but I'm using it less and less now. I'm amazed at how VIM really can do everything the more GUI-based editors can do, without resorting to hiding all the functionality in the menus, sometimes without a keyboard shortcut.

And that brings me to my final point: VIM is consistent across platforms. The commands I use in VIM on NetBSD are the same on my Mac are the same using VIM on Windows. I don't need to learn a totally new set of commands or keyboard shortcuts going from platform to platform. At the end of the day I'm trying to get the job done and don't want to have a different mindset for editing text on three different operating systems. I'm going to stick with one thing I know works everywhere, is available everywhere, and that is VIM.

#

vim is like strong black coffee... Anonymous Coward April 12, 2007 12:41 AM
... seemingly bitter when you first try it, but pretty easy to become addicted to.

Yes, vim does have a pretty steep learning curve. But like an above poster said, being able to do everything from the keyboard is very nice (yes, I know emacs falls into that category too). The "modalness" of vim is the biggest turn-off to many people, but it's also one of vim's greatest strengths. If you have a lot of reformatting to do at once for example, "normal mode" and "command-line mode" are very handy and fast.

There also a number of quick reference cards available, like:
If you are a fan of LaTeX, then the vim LaTeX-Suite (http://vim-latex.sourceforge.net/) is great. I'm currently working on doctoral dissertation and using Vim/LaTeX for all of my writing... I couldn't even imaging the pain of having to go back to a normal word processor. Vim 7 has great integrated spell checking, a rudimentary (doubled words and missing cap at the start of a sentence) grammar/syntax checking, auto-completion of recently used words, syntax highlighting, auto-indenting, a powerful regexp system, etc., etc.

I know that many other editors support many of these features as well, but... I guess my point is that I really didn't like vim much when I first used it, but -- after the initial frustration hump -- vim is easily my favorite text editor. Now, using any other "simple" editor seems just as frustrating to me as vim did years ago; just for the opposite reasons.

#

vi for dummies Anonymous Coward April 12, 2007 04:10 AM
Starting VI
-------------
Create new file:
$ vi [filename]
Open existing file:
$ vi [filename]
-------------
Getting around in vi
-------------
i - insert mode. This allows you to add text to the file. In insert mode, vi acts a lot like pico or notepad.
[esc] - get back to normal mode
normal mode commands:
$ - get to end of line
^ - get to beginning of line
alt-q - get an unresponsive vi back
d[backspace] - delete the character in front of cursor (acts like backspace in notepad)
dd - delete the current line (where cursor is)
5dd - delete 5 lines starting with line cursor is on
291gg - go to line 291
u - undo

:w - write file

:q! - exit without writing (you opened wrong file and made a bunch of changes? : P)

:22,791 s/this/that/gi - Starting with line 22 and ending up at 791, replace this with that, be greedy and case insensitive. (of course really getting the most out of this requires a working knowledge of regex). Escape non-alphas using regex escaping rules...

I can do 99% of what I do with these commands. Please note: your mileage may vary depending on which OS and vi you run.

These apply to dedrat via ssh. Other distros have been identical. BSD is a little different for some reason (Different vi?)

I don't need to toot vi's horn but I will say that once you become proficient and can get around efficiently, you'll begin to hate any gui text editor more and more. I'd rather pull my own toenails out than write code in a editor that requires me to use a mouse and scrollbar to get to a line I need to work on.

-Viz

#

Re:vi for dummies Anonymous Coward April 12, 2007 04:15 AM
dammit, forgot something for normal mode

ctrl-f - go forward one page
ctrl-b - go back one page

-viz

#

Re:vi for dummies Anonymous Coward April 12, 2007 04:19 AM
grrrr

<nobr> <wbr></nobr>/[search text] - find text in the file

-Viz

#

Cream - The real easy VIM Anonymous Coward April 12, 2007 07:59 AM
Remote file editing et. al. Administrator April 11, 2007 02:17 AM
Hi, if the target audience is supposed to start using vim, there are two things to mention first (I know, they're probably covered in enough publications already, but hey...): "vimtutor" (it's a separate program), which does a nice job getting you started (but is in no way like the "easy mode"), and of course ":help" in vim's command mode (probably it has a menu entry in gvim).

For all advanced users that liked the "scp://" file names: There's even more to it and it's pretty configurable. See ":help netrw-ref" or ":help netrw-urls" for a list of supported wrappers. There is e.g. DAV, rsync and sftp. Note that you need to use sftp://host//absolute/path for absolute path, i.e. double-slash in the path string! Otherwise, it will be rooted on the remote home directory.

But in all honesty: I never use those capabilities. I like to run my vi(m) on the remote machine, preferably in a GNU screen session.

#


Post a new comment

Name : Anonymous


Copy the word from the image on the left into the input box.