What's so great about the vi editor?

Usually, it’s for cases where I have a bunch of output I snitched from someplace in format A, and want to quickly massage into a form to use as input in program B, and I’m just doing it as a one time exercise. Perhaps “A” is somebody’s web page full of data that I want to turn into a CSV to load into a DB table or the spreadsheet you mentioned. Or it’s somebody’s list of something like timezone abbreviations and offsets that I found, and want to munge into the compiler initialized list of time zones recognized by the code I’m writing. Or the 1000 line config file I have for something has to grow a new comma separated field in the 5th place for a new version of the software.

Yeah, as an actual text editor, I’ll use a gui editor. But vi has its place.

How can you use Visual Studio without the mouse? Aren’t your keyboard shortcuts just Alt shortcut keys that bring up the menus? Like Alt-E Alt-Z which might do something like Edit->Zoom?

In vi, the key commands are the command. For example, if I wanted to take 10 lines from the xyz function and put it into the abc function, I would type something like this in vi:

/xyz
10yy
/abc
p

The ‘/’ means search, ‘yy’ means yank, ‘p’ means paste the buffer from yy. You see how the commands are commands themselves and not keyboard shortcuts to the menu options. Could you do something like that in VS without touching the mouse?

Base unix is not designed for end-users. Most everything in unix is cryptic unless you know what’s going on. Vi is no different. If someone gets into vi and can’t figure how to get out, they probably shouldn’t be using unix. Sure it would be nice if the default editor was easy to use, but it’s not really that big of a deal in the unix world. Unix is complex, but with that complexity comes power.

When it comes down to it, pretty much all editors can do the same thing with plain text files. It’s hard to say one editor is better when the end result will look exactly the same no matter which editor you use. But the power of vi allows me to do complex changes in the editor easily.

For example, by making use of the regular expression capabilities in vi, I can quickly do a complex search and replace. Not just a simple replace abc with xyz, but something very involved. For example, find all words which are of the pattern letter number (like a1) and reverse the characters so it’s number letter (1a). The vi pattern command would be:

%s/ ([a-z])([0-9]) / \2\1 /g

That command would replace every number-letter word with letter-word in the whole file at one time. How long would that take in Visual Studio? There’s no denying it looks like complex gibberish to a novice, but tricks like that can save me hours of editing time.

If you’re using commands that complicated in your text editor, then you are effectively doing a bunch of throwaway shell script, just in a different shell. And what effect will that command have, if you misplace one of those backslashes or parentheses? If I’ve got something that complicated, I want to save it in a file, to easily correct it if I make a mistake.

I’ve never even used Visual Studio, and I’ll bet I still know how to do this. Control-f for find, then type in xyz, then use shift and the arrow keys to select the lines I want (which can be any lines, not just the 10 next lines or whatever that vi command would be), then control-c to copy them, then control-f and type in abc for find again, then control-v for paste. Frankly, I’m amazed that anyone with more than a passing familiarity with computers would not know this, since those same hotkeys are used in nearly everything (on a Mac, the control would be replaced with command, but otherwise just the same).

And if I didn’t know the hotkey for some operation, I could use the mouse for it the first time, and while I’m on the menu, take a look at what the hotkey is (since they’re always listed on the menu, too). Even if it’s a program I’ve never used before, and a task which I’ve never before performed, it’s just that easy to find the keyboard shortcut.

Point taken, but it’s a shell I’m comfortable with for doing that sort of tripe. As for mistakes, that’s why editors are provided with “undo” commands. On top of which, if it was something I’d think of doing with vi, and I was doing it in shell script, I’d probably be running it through sed anyway, which means I’m just doing the edit commands at shell level.

:smiley:

There’s a truth behind that joke, though. Vim is a programmer’s editor – it’s primarily designed for editing source code, not prose text. And as any programmer knows, the majority of the time is spent not on entering new code but on delving through large amounts of existing code and making small changes all over the place.

So that’s why Vim offers an enormous array of cursor movement and text-replacement commands. Watching an experienced Vim user edit a piece of code is a sight to behold: the cursor jumps all over the screen, never requiring more than a few keystrokes to get to exactly the right place, make a small change, and move on. And when that is what you’re spending most of your time on for the majority of your day, every day, being able to do in three keystrokes what would take twelve (or grabbing the mouse) in another editor, it really adds up.

Another thing about Vim is the ease with which you can quickly create and use a macro. Sure, most other editors have those, too, but generally it’s an advanced feature, hidden deep within the menu, on the assumption that defining a new macro is something you’ll want to do only once in a blue moon. To a Vim user, creating a new macro on-the-fly is a routine matter; it’s almost as common as using the copy/paste functionality. You have a file containing several lines of text and you want to perform the same transformation on each of them, so you use a macro to record what you’re doing with the first line and then you apply that macro to the rest of the file.

What does it matter whether you call Alt-E Alt-Z a “command” or a “menu shortcut”? It’s not using the mouse either way. (The advantage of it being a menu shortcut, mind you, is that you have a much easier time finding it if you don’t already know it, and finding related commands as well… the ability to use a menu or even a mouse when desired is not some drawback)

Here.

Yup, to do the replace that yabob wanted just took me 2 minutes to figure out in VS.

You turn on Regular Expressions, type {[a-b]}{[0-9]} in the Find box and /2/1 in the Replace with box. You can do it the current document, all open documents, the entire project, the entire solution, or the current code block.

Edit: Also, there are handy drop-down menus that let you insert RegEx expressions in your dialog so me (having used RegEx once like 10 years ago) was able to figure out the slight differences in syntax very quickly.

Any editor that has a following among programmers will have some way to handle complex regular expressions. I do similar things with nedit and notepad++.

If you’re comparing the tasks based on a novice operator, the GUI will win every time. Most every other editor will win on the novice experience. I would say Vi is the hardest editor for the novice. A novice may not be able to type anything into Vi. But if you’re using an editor, you will likely use it for years. You should compare editors based on the experience of an expert user of that editor. You can use Notepad as your editor and the end result will look the same. That doesn’t mean Notepad is as good as everything else.

A Vi user will be able to do many tasks quicker than the equivalent task in a GUI editor. Many tasks will take less keystrokes in Vi. Vi is harder to learn, but after you learn it the complexity doesn’t matter.

For an experienced user, is there any difference in the compexity to type ‘/xyz’, ‘ctrl-f xyz’, or mouse to Edit -> Find and type xyz? Really it’s the same complexity to the expert user. But generally Vi will take less key/mouse action to accomplish the same task. So if you edit a lot, that may make a difference to you. The complexity of Vi is evident when you are learning it, but once you are an expert it is no more complex than any other editor.

One advantage Vi has over most other editors is the ability to perform an action on just certain lines which match a pattern. For example, delete all lines that have the word dog (g/dog/d means globally find the lines with dog and 'd’elete them). For lines 1-10, replace the word cat with feline (1,10s/cat/feline/ means from line 1 to 10 replace cat with feline). For any line with the word dog, replace the word food with kibble (g/dog/s/food/kibble/).

Maybe there is some editor that has the same power as Vi. That’s great if there is, but is it also free, installed with the OS on many platforms, and freely available on every other platform? Probably not, but Vi is, and that’s one of the reasons it is so great.

All these menu based editors let you access the menus with the keyboard as well… I don’t see what’s so terrible about menus. They’re a great bit of design, as far as I’m concerned.

Menus are terrible to navigate and they’re modal, meaning they lock you into a path which is difficult to get out of.

I think you’re confusing my example with filmore’s. My point wasn’t simple search and replace, but I included use of the “g” command to limit search and replace to only certain lines of the file matching a different pattern. I see that filmore has now brought up the “g” command, too. I’ll add that you can do something like “g/Sunday/.-1s/sunrise/sunset/” - change “sunrise” to “sunset” on all lines PREVIOUS to a line containing the word “Sunday”. Or even “g/Dog/?Horse?s//Pony/” - On every line containing “Dog”, search backwords for one containing “Horse” and change it to “Pony” - If there are multiple lines with “Horse” preceeding the “Dog” line, I’m only changing the last one. Then there’s the key sequence mapping feature …

As I said, I only want this sort of thing once in a while, but when I do, I’m glad to have learned how it works, ridiculous syntax and all.

That’s pretty much why I use it. That and the fact that it’s guaranteed to be available and work predictably. I code mostly in terminal windows, over ssh to remote servers. Vi is always there, everything works in a ssh session, and it’s designed to keep working predictably even when the connection is slow or unreliable (which can occasionally be critical when you’re dealing with remote servers).

Anecdotally, I’ve noticed that many of the more experienced developers I’ve worked with rarely use the advanced features of their text editors. I couldn’t tell you what 95% of the vi commands are, despite using it for many years. I rarely use anything more advanced than search/replace, or n-prefixes to basic commands. I think there’s something to be said for relying only on basic features and bundled system tools.

Why in the world are you comparing a text editor to an IDE? It’s like comparing a guitar to a synthesizer, and frankly, VS is probably as if not more user-unfriendly to the uninitiated for editing or creating simple text files than vi.

I use vi on Cygwin as my scratch pad and composition editor for large documents because back when I was in a hands-on role, we used vi on the Linux infrastructure servers, because we used vi on the AIX database servers, because we used vi on the SCO retail backend boxes, where our standard development and deployment language was ksh.

I had to learn vi’s intricacies because it was the only editor I could count on to be available on whatever random system I got woken up at 3AM to fix, and I continue to use it because I have no reason to learn something else.

You’re right - sorry!

And yeah, for more complicated scripting vi is likely more powerful than VS. Right tool for the job, and all that.

You guys bringing Word into this are out of your element. Text editors are not word processors. Word processors are all about layout. Bolding, highlighting, changing colors, font size, inserting pictures, managing section headings, chapters, titles, etc. (I would argue that whole paradigm is misguided. I believe in separating content and layout. Which is why I use LaTeX and emacs. :slight_smile: )

You can make something easy and discoverable for beginners, or powerfully useful for experts, but it is almost impossible to do both at the same time. Vi and emacs were designed by and for people who edit large quantities of text all day, every day. Programmers, mostly. I would argue they’re a great investment (in terms of the time it takes to learn them) for writers, too.

The only thing I know how to do in vim is get into and out of insert mode, save and quit. But I’m getting to be pretty adept at emacs. I can search and replace across entire directories using regular expressions in a few short keystrokes. There is no way in hell you can do that with notepad. Vi follows the traditional unix philosophy more than emacs (which flagrantly assaults it). It does one thing and it does it really well. It is installed on every unix machine by definition.

The great thing about emacs, and to a lesser extent vim, is the huge amount of customization you can do. Emacs is basically just an operating system written in lisp. You can play games, check and respond to email, browse the web, explore files and directories, and use the shell, all within emacs. And all that lisp code is free for studying, modifying and adding to. But you usually don’t have to, because people have been doing just that for 30 years, and contributing their changes back to all of us. It’s a community just as much as an it’s editor.

Their steep learning curve is part of the allure. It makes the editor, at least in a small way, part of your identity. It is what caused the editor wars. Nobody fights about whether notepad is better than nano (it isn’t, by the way :wink: .) If you’re not a pretty hard core computer nerd, you’re not going to understand the communities behind these programs or what they represent. It isn’t that you’re excluded, it’s just that ease, accessibility, and popularity aren’t what they’re about.

Emacs is an immigrant to the Unix world; it came from the Incompatible Timesharing System (ITS) by way of Multics and, at least in the Unix world, picked up more than a little Lisp Machine on the way.

Deeply true and deeply false at the same time. :wink: Emacs isn’t an OS so much as it’s a UI: The Emacs Way is to use Emacs to do everything, enabling you to use the same keystrokes in similar ways to achieve all your computing tasks and tie everything you do into one coherent world. The Vi Way is to live in the command line and use the command line as the single, coherent world everything happens in.

This is all true. This is also all Stallman; if Emacs is Superman, Stallman is Jor-El and the ITS/Lisp Machine world is dead, departed Krypton.

Because VI was designed to be an IDE, essentially. Most of its features are only useful to programmers. It’s not fair to compare it to text editors because that’s not what it is.

And VS is far from the only bit of software that can do this stuff. Notepad++ is one I use. It likewise calls itself a text editor when it really is more of an IDE, with its syntax highlighting (a feature I don’t believe VI has).

The point is that there is software out there that does what VI does and more, and yet is easier to use. From an objective standpoint, this software is better. The “nerd wars” are leftovers from an earlier time and are rapidly fading, and thus can be ignored.

Oh, and VI isn’t the same thing as VIM, as the latter added some much needed intuitiveness. I can open VIM, and I can at least start typing a text file, instead of starting in a mode that turns every key into a hidden function key.

Does anybody really use vi anymore instead of vim? All the machines I have encountered in the last 5 years alias vi to vim.