Great Vim Features

Vim rules.

@siegel @Skud OH: "vi is an editor with two modes: one which destroys your input and the other which beeps at you."

OK, the emacs people have their page (GreatEmacsFeatures). Rather than putting vim comments on an emacs page, you can put the vim comments here. This is being a GoodWikiCitizen. The original comments claimed:

Actually Vim does all of these. There's not a thing emacs does that vim doesn't do, but vim does it better, simpler, and faster. Except possibly play ElizaDoctor ...

What about narrowing? I can't find any way to do that in vim.

What exactly is narrowing?

Narrowing is temporarily reducing the entire buffer context to just a specific region. This is useful for focusing on a particular section, editing SQL code embedded in another language, making absolutely sure that you don't accidentally modify stuff outside a particular range with search/replace commands, etc. It's been a while since I used vim actively, but if it doesn't already exist, it would probably be implemented it in terms of hiding everything except what appears between two marks. -- ScottVokes

Oh, and I don't think vim gives you the option to *not* use regexps

What? A search is initiated by typing '/', if the search expression you use thereafter contains no magic characters, then what does it matter? Anyway, in what sense does EMACS not use regexps for its searching, remembering that a regexp may consist solely of a sequence of characters standing for themselves? In a non-regexp isearch in emacs, characters like ., ?, (, etc., are literals - I find this useful when searching for things like "" etc.

Oh, I see what you mean now. Vim (and vi) have a boolean "magic", choosing between, say, expressing the Kleene star as '*' (true == magic) or '\*' (false == magic) with "*" as a literal.

Eg :set magic or :set nomagic

with a history of previous search values

Yes. Type / and then uparrow/downarrow to navigate the search history.

Even nicer: if you type in / followed by ctrl-f it will open a search history window where you can navigate around and use vim commands to modify your search. A similar window opens if you type in ":" followed by ctrl-f for a command history window.

 7. Insert mode completion *ins-completion*

In Insert and Replace modes, there are several commands to complete part of a keyword or line that has been typed. This is useful if you are using complicated keywords (e.g., function names with capitals and underscores).

These commands are not available when the |+insert_expand| feature was disabled at compile time.

Completion can be done for:

1. Whole lines |i_CTRL-X_CTRL-L| 2. keywords in the current file |i_CTRL-X_CTRL-N| 3. keywords in 'dictionary' |i_CTRL-X_CTRL-K| 4. keywords in the current and included files |i_CTRL-X_CTRL-I| 5. tags |i_CTRL-X_CTRL-]| 6. file names |i_CTRL-X_CTRL-F| 7. definitions or macros |i_CTRL-X_CTRL-D| 8. keywords in 'complete' |i_CTRL-N|


Quick question for vim experts (I still use vim as if it were vi):

An example of a simple perl command might be

  :%!perl -e 'print reverse <>'
  :%!perl -e 'print sort <>'
The perl interface (:perl and :perldo) doesn't seem to let me do these things as easily. I have to access a buffer structure with get and set methods. This seems to detract from the simplicity of the vi/unix interaction model. The question is, what is the simplest way to do these basic multi-line operations. The perldo interface seems very restrictive because it operates line-by-line. (BTW, I know that :%!sort is simpler for the sort example, but that's not the point!)

It should be possible (though I haven't tried) to set up a vim macro that will do :

 :perl @ARGV = $curbuf->Get(0 .. $curbuf->Count()) ; $curbuf->Set(<your transform using <> goes here>);

I haven't compiled vim with +perl or +python so the rest is left as an exercise ... but if you get it to work do please post here ... -- PeterMerel

Since you can do this stuff already with %!, is there any need to do it using the clever :perl interface?

on unix just use

 !sort % 
to sort your text.

One of the features of Vim I love is the macro feature. OK, at first sight it is not a big deal. q[letter] starts recording a macro, a second q terminates it. Call the macro by doing @[letter].

But then the fun starts. The point is that Vim has all these high-level editing commands. Which means that you can put something like "go on line down and replace the first and the second word" can be expressed as:

So place this in a macro a and call it with @a. Then give a repeat count and do it for all the lines in the file. Yes!

-- StephanHouben

This is nice, but the idiom can be improved immensely using :g/

for example, you've got a macro for changing the second word on a line to 'foo' qb^wcwfoo<esc>q (damn that looks weird to type). Then you visually select the block you want to perform the macro on (use 'v'). Don't yank the selection instead press ':', hitting ':' in visual mode will automatically do :'<,'> which means the range between the start and the end of the visual block. Then you do a command like this:

:'<,'>g/^/normal @b

Which will execute the macro for each line you had in the visual block. Complex to explain, but its surprisingly quick once you're familiar with visual mode and :g/

-- StephenThorne

What I miss in both vi and emacs (but suspect its buried there somewhere; everything else is) is the basic point and click browsing capability of IDE's like VisualAge. You know, where huge hierarchies of Java source from different directories are visible on the left and accessible with a click, whereas in vi I need to type full pathnames to browse from class to class. Can someone explain in detail how to do this in either editor? Thanks! -- BradCox

Emacs has the Speedbar for this, which is for example used in the JavaDevelopmentEnvironment. But it is not as nice as VisualAge of course. At the bottom of is a screenshot of it.

For Emacs there is also the ECB (EmacsCodeBrowser?), which integrates nicely with JavaDevelopmentEnvironment. If you like working with IDEs like JBuilder and VisualAge you will certainly like ECB.

Click on vim's "open" icon in graphic mode and you get the standard file selection explorer. Oh, you want to see what files are in your project instead of what files are in your directory? Then merely integrate vim into your IDE as its external editor. At least, VisualStudio and SourceNavigator can do that ... if yours can't then perhaps it's time to replace it ...

If you're using JBuilder, you can download JavaVi, and have a vi-like editor within your IDE.

There's also the Project plugin for vim and the Taglist plugin

Then again if you're some who prefers shells to ides you don't have a problem to begin with. What I usually do is work with MakeIt and the builtin Vim build dialog ... ShellVsIde, anyone?

In Vim you don't have to write the full path name to reach a file. You only need to write a unique substring of the path. Btw, I much prefer using ctags to using the point-and-click interface: It's keyboard-based rather than mouse-based, and it handles methods and variables as well as classes.

The above guy didn't explain how to do it, there are heaps of ways.

:find filename.hpp to look for a file of that name (duh). The path for searching is set with :set path+=/home/guy/** That would search your entire home directory, the ** means recurse.

Or, if you want to open files in a folder, use :pwd and :cd /home/whatever/folder. Then you can do :e . and it'll show you whats in the folder, or alternatively, :e filen and then tab for auto-complete.

Or, if you already have files loaded, turn on :set bufhidden (I think) so you don't have to save the file each time you jump to another one, and then try :b enam and then tab to auto-complete on a substring of the filename. That would match filENAMe.hpp. Then you can make some changes, switch back to the last file by pressing Ctrl-^, make some more changes, and eventually do :wa to save all changes to all files. I can jump around between 20 or so active files among 200+ passive (ie not yet opened, but registered as hidden buffers) files.

Then finally try :mks or :mks! to save your session. Then you can reopen vim with the same buffers via gvim -S Session.vim.

A few years back, I wrote a vim script that would munge an ExuberantCtags? file to create a listing of all the packages and classes in my project and load the listing into a buffer. When I hit enter or double-clicked on the class name, it would then list all the members of the class. If I hit enter on the member, it would jump to the tag. I wrote it with the explicit purpose of spiting a die-hard VisualAgeJava user. When I showed it to her, she just grumbled and went back to restoring her corrupted repository. IBM had the last laugh, though, since I'm now using EclipseIde and simply see my script as a pleasing but gratuitous hack. Of course, I've still got Eclipse set up to jump out to Vim whenever I want to do any heavy-duty writing or multiple repetetive edits.

Where the hell have you guys been! ctags is supported directly by vim! :help ctags !!! Just put your cursor over any identifier that's defined in the tagfile and hit ^], hit ^t to go back to where you were.

I definitely appreciate the following features of vim:

Try it:
 add to .vimrc
 set viminfo='50,\"1000,:100,n~/vim/viminfo 
 mkdir ~/vim

Edit some files and put some marks in them (ma, mb, ...). Return to the files and see that the marks are still there ('a, 'b, ...). For more info, read the section on viminfo in the vim help (:help viminfo).

By the way, has anyone seen any interesting uses of python / TCL / perl programming in vim?

Yes, python is much faster than the native scripting language, and much more powerful. I've done stuff like transposing the vim syntax highlighting to html (takes <5 seconds instead of >2 minutes for the native vim version). I also have a dodgy little script I can use to post the current buffer via XML-RPC to my blog. Very cute.

I have never been able to fully approximate the incremental search feature in emacs. The mode I use most often is word completion. While typing in a search pattern, I can complete it to the end of the current word by typing c-w, or include the next word by typing c-w once more, and so on. Is there a way to do it in vim?

A useful project could be to outline many of the operations the can be performed in both Emacs and Vim and explain how to do each one in both editors.

Having used both, there are some things I know how to do in one but not the other. Yet, I always hear that anything you can do in one you can do in the other.

IMHO, the single most useful feature of VI which Emacs does not have (AFAIK) is the . (dot) command, which repeats the previous operation. (i.e.: previous insert, replace word, delete word, ...). It makes you appreciate why VI is a modal editor.

Actually, there is a command called repeat, usually mapped to [C-x z], which I believe does pretty much what you're describing.

[C-x z] runs the command 'repeat'. It does something like the same as in vi, but seems to be more useful in vi. In vi if hit 'I', type a word and hit escape then I have inserted that word at the beginning of a line. If I then go to a different line and hit '.' then the same word will be inserted at the beginning of that line. In Emacs if I did the same, the 'repeat' command would probably repeat whatever the last movement command was that got me to the different line, unless I used the mouse to get there, in which case it causes an error message. This is possibly the only feature of vi which I missed when moving to Emacs years ago. I eased the transistion by using 'vip-mode' in Emacs for a while, which *does* provide the '.' functionality. These days I use keyboard macros in Emacs, but the problem there is that you have to know before you do something the first time that you will want to do it again, and tell Emacs to start recording. In vi you don't need to know that in advance.

Au contraire, mon frere! I'll explain how to do this in GreatEmacsFeatures.

What I appreciate in VIM are the actual "VI Improved" bits. I like the case insenstivity (something I picked up from Emacs), I like the ability to vi huge files, and the elimination of the 255 character line limit (line too long - you're doomed!).

I'm not sure if I like the incremental search feature yet, that's a love hate issue that I haven't got used to yet. When I'm in VIM, I'm in "VI" mode, whereas when I'm in Emacs, I'm in Emacs mode. The incremental search feature of Emacs is different from VIM's. Most frustrating.

< If you're not sure whether you like it, you can at least turn it on & off via :set incsearch or :set noincsearch >

I haven't explored the macros yet, as was mentioned above, but I recall living off those same kinds of macros back in the days of VMS and EDT. We would use keyboard macros to perform sweeping changes on files that way.

The '.' command of VI is very powerful, and a real idiomatic feature of VI, IMHO. Perhaps Emacs has it, perhaps not, but for me the extra power of '.' is when it's paired up with the 'n' (find again) command. I would be hitting hammering on 'n.', racing through files. Somehow, repeatedly typing [Ctrl-S] [Ctrl-S] [Enter] [Ctrl-X Z] doesn't seem as compelling.

I have not the motivation, however, to move into the advanced modes of VIM, specifically, the language specific modes. I use Emacs for that, auto-indent, coloring, [Meta-X compile], [Ctrl-X `] to find errors. No doubt VIM can do it, but I have no inclination to learn it.

Nor have I explored multiple windows/buffers in VIM. They don't interest me, really. If I'm in VIM and want to copy in a piece of text from another file, then :!vi otherfile.txt<new vi>/start of text^Mma/end of text^M:'a,.w!/tmp/x.x^M:q!^M<orignal vi>:r /tmp/x.x^M

That sequence is so hard coded into me, my children will know it at birth.

Talk about sadisitic. :vi foo.c<cr> /start<cr>v/end<cr>y:bp<cr>p is the way I'd do it.

It would be nice if it were possible to do something like CTRL+SPACE as in JBuilder. This gives a list of all the members of class and if you're inside () it gives you the parameters. Would this be possible with Vim?

Yes, have a look at tag explorerer from in the scripts section. -- StephenThorne

I'm okay with the vim editor as it is configured on RedHat 8.0, however I noticed a lot of distributions don't bother to even enable simple syntax highlighting in many extensions (such as SlackwareLinux). Yes, I know it should be "easy" to change, but I'm still a relatively new linux user. Actually, syntax highlighting and inteligent tabbing are really the only features I care about in a text editor. If I'm doing software development I prefer an IDE. -- BlakeMason

For those who don't know how to turn this on, add the line "syntax on" to ~/.vimrc -- CodyMello

Emacs does have the '.' command ... if you enable viper-mode :-) All the functionality of emacs _plus_ the modal editing of vi. I love it! -- Repton

Take a good look at the fold (:help fold) stuff for vim. Incredible! -- Richard

Here's something so ridiculously simple I can't believe I never knew it. And I've been a Vim user for years. You can not only use ESC or Ctrl-] to escape from insert mode, you can also use Ctrl-C!

vim wikis:

See VimAsPim

CategorySoftwareTool CategoryVi

EditText of this page (last edited August 6, 2013) or FindPage with title or text search