LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Vi users: Can Vi do this? (https://www.linuxquestions.org/questions/linux-general-1/vi-users-can-vi-do-this-4175447096/)

Tirsa 01-24-2013 08:34 PM

Vi users: Can Vi do this?
 
Hi. First of all, I SWEAR I'm not trying to start a flame war, but I am really, really trying to pick a good text editor that I can use for life, and I know I want either Vi or Emacs. I'm just getting started as a programmer in any real sense, but at this point I'm comfortable programming in a few different languages and will definitely be learning more. I despise "smart" modern IDEs for a variety of reasons, and I've grown very attached to the idea of having one development environment that I can use for whatever sort of code I want. I also want to feel that sense of attachment, productivity and comfort I keep reading about from those gurus who really know their editor. From what I've seen, I can get that with either of the two conventional giants.

So I've been playing around with both for a week or so, thinking I could easily decide this for myself, and at this early point I am having a much easier time with Vi. From my extremely limited experience, the keybindings seem more intuitive, and frankly I am afraid of the finger contortions it seems Emacs favors, because there is a lot of arthritis in my family and recently I've started having some pain in my hands (not through fault of Emacs--I'm just looking toward the future).

Anyway, I thought I had made my decision. I ordered two or three supposedly great books on Vi and got all excited about a month-long intensive Vi training sabbatical.

Then, however, I read a couple of very compelling Emacs testimonials here, and now I am second guessing myself. Look at the things Emacs can do! Wow.

Here is one of the good parts, from a guy called emacsdude:

Quote:

Emacs is a programmer's editor.

Yes, the comments about extensibility are true, but the default emacs behavior is very powerful. In order to use emacs properly, you need to spend a great deal of time learning how to use it. One of the ways that I learned was just watching a guru edit code for a few days. I'd ask things like "whoa - how'd you do that?" a lot. The learning curve is very steep but the view from the top is fantastic!

Here's a list of reasons why I love Emacs for programming:

0) Good programmers realize that the mouse is an inefficient input device for editing code. Emacs allows you to do EVERYTHING with the keyboard. This makes life simple and efficient.

1) You get to edit multiple buffers at the same time and you can view them in the same window (frame - in emacs terminology) via splitter bars. The bars can be placed in arbitrary locations and is all keyboard controllable. This is a very efficient use of screen space and allows you to see many parts of interrelated code at the same time which makes coding and debugging easier.

2) You get auto-formatting and coloring of code. This makes code easier to read and reduces syntax errors. Getting the right indent-style for example is a simple matter of using the TAB key.

3) You have powerful code browsing functionality. You can search for the declaration of variables, classes, etc... You can browse around code by following { } pairs for C/C++ programming for example.

4) The raw text editing commands are clean and efficient. You get word/variable/identifier/etc... completion. You get a practically infinite undo buffer. There's the concept of the kill-ring for cut-and-paste which is like the clipboard concept but easier to use.

5) The compiler can be invoked from within emacs and the error/warning messages are parsed by emacs such that you can jump to the error/warning with an efficient keystoke.

6) The debugger can be integrated into emacs. You can step through code, setup breakpoints, etc... all from within the editor and never have to use the mouse.

7) You can record macros on the fly to automate tedious repetetive operations. For the emacs gurus, you can also write your own functions/extension in elisp

8) The search function (either plain text or regular expression search) is "incremental". I have not seen this is other editors. So if I'm searching for the word "foobar", as I type "f", it will jump to the first instance of "f", then when I type "o", it jumps to the first instance of "fo", and so on. Searches are saved in a history buffer.

9) You can display/hide code heirarchically much in the same way that visual studio does with it's tree-view -- BUT, you never have to use the mouse. Another use of this is for #ifdef hiding for the C/C++ programmers

10) You can invoke a command line shell from within emacs and then use all of the powerful editing features of emacs to run the shell.

11) The concept of the "mark" allows you to place anchor points on where you were working so that if you jump around to some other area of the text file you can immediately jump back to the mark. There is also a mark history, so you can jump back to as far back/forward as there are marks.

12) Global search and replace allows you to do a search and replace for the current file you are editing and ACROSS other files as you specify.

The list goes on and on...
After reading this, I just don't know if I can ever be happy again unless I get an editor that can do all of these things. (Except maybe #10. I don't care too much about that one.) And I would hate to spend a huge amount of time mastering Vi only to find that it can't. I mean really, life is short, and I only have time for a limited number of month-long sabbaticals! I'd like to save some of them for something other than text editors.

So can any of you Vi guys tell me if Vi (or Vim, actually) can do all of this too?

Thank you.

suicidaleggroll 01-24-2013 09:15 PM

Personally, I use both vi and emacs. They both have their place, and that place isn't necessarily the same one. I truly believe that ANYBODY who does a significant amount of Linux programming absolutely MUST learn vi. Not necessarily every single keyword or shortcut, but they must be able to use it comfortably and efficiently, including searching, string replace, jumping to certain lines or columns, etc. This is because a significant number of Linux machines don't have emacs, or kate, or pico, or nano, or any other text editor that might be familiar to them. vi is the standard, it's the default. Find the lowest of the low non-graphical embedded Linux system and what'll it have? cd, cp, mv, and vi.

Now that doesn't mean that you must use vi for everything, everywhere. vi is very powerful, it's VERY fast, and it's efficient, however it does have some limitations...namely many of those listed in your quote. Personally, I use vi for quick edits, reading code, and touchups. I would say that based on the amount of time each editor is open on my machine, emacs would win. However, based on the number of times each editor is opened/closed in some amount of time, vi would win hands down. Need to change the starting value of a variable or the ending index of a loop? Pop open vi, change it, save and quit before emacs even has a chance to open. I can't even count the number of times I ask a colleague to read off the equation used in some program, just to see them open it with emacs, wait for it to open, fumble through the Edit > Search dialogues and finally get to what we're looking for after 20-30 seconds, when they could have found the same thing with vi in under 2 seconds.

However, when you get into big programming tasks, the multiple buffers of emacs, the incredibly convenient INTERACTIVE string search and replace (which is even case-insensitive but case-aware! I freaking love that!), the syntax highlighting and auto-indenting (including BATCH auto-indenting...just highlight a block of code and hit Esc Ctrl-\), and the syntax auto-completion just can't be matched by vi.

TobiSGD 01-24-2013 09:32 PM

Can Vim do it?
0) Yes.
1) Yes.
2) Yes.
3) Yes, with plugins.
4) Completion, yes with plugins, don't know about the other stuff.
5) Compiler can be invoked (like any other program), the rest: I don't know.
6) I don't know.
7) Macros: Yes, Scripting: Yes, but with Vimscript instead of Elisp
8) Yes, so this Emacs person does not have tried Vim, as it seems.
9) Yes.
10) Invoke a shell: Yes, for using vim editing in the shell you have to configure your shell to do that.
11) Yes.
12) Yes.

I personally am not a fan of Emacs, due to its excessive (and in my eyes unnecessary) use of keyboard combinations, but I have yet to find something that I need for my personal use that Vim does not offer.

suicidaleggroll 01-24-2013 09:49 PM

Quote:

Originally Posted by Tirsa (Post 4877063)
1) You get to edit multiple buffers at the same time and you can view them in the same window (frame - in emacs terminology) via splitter bars. The bars can be placed in arbitrary locations and is all keyboard controllable. This is a very efficient use of screen space and allows you to see many parts of interrelated code at the same time which makes coding and debugging easier.

Quote:

Originally Posted by TobiSGD (Post 4877091)
Can Vim do it?
1) Yes.


How do you do that with vim? That's easily my favorite feature of emacs, and I would be very interested in being able to do it with vim as well.

Myk267 01-24-2013 11:13 PM

@Tirsa:
I really wanted to buy into the emacs hype, but then one day I opened vim inside of emacs and something clicked and I haven't really opened emacs since. I also find vim easier on my hands in general, and that might be more of a factor than I previously considered.

As a programmer, you'll find that they both require you to shave the yak a bit and wade into the community to find the missing things you need.

Other than that, I think they're roughly at parity in features, it's the implementation that differs by a mile. Good luck in your search!

Quote:

Originally Posted by suicidaleggroll (Post 4877097)
How do you do that with vim? That's easily my favorite feature of emacs, and I would be very interested in being able to do it with vim as well.

There's some high quality videos about doing things with vim here: http://vimcasts.org/episodes/archive
Scroll down to #7 for one that's all about managing windows in vim (frames, views, panes, whatever...)

Quote:

Originally Posted by suicidaleggroll (Post 4877086)
However, based on the number of times each editor is opened/closed in some amount of time, vi would win hands down. Need to change the starting value of a variable or the ending index of a loop? Pop open vi, change it, save and quit before emacs even has a chance to open. I can't even count the number of times I ask a colleague to read off the equation used in some program, just to see them open it with emacs, wait for it to open, fumble through the Edit > Search dialogues and finally get to what we're looking for after 20-30 seconds, when they could have found the same thing with vi in under 2 seconds.

I think if someone is opening and closing emacs often, they haven't found the True Zen of Emacs. Ideally, they would start emacs as a daemon, and just connect the emacs client when needed, preferably with a keyboard shortcut. This dramatically cuts down on any loading time disadvantage, and also makes it a lot harder to accidentally close or lose track of buffers.

Second, if someone is fumbling around with emacs, that's not really putting the two on equal footing for comparison, right? I think you knew that, though. :)

bryanl 01-24-2013 11:39 PM

Quote:

you need to spend a great deal of time learning how to use it ... The learning curve is very steep but the view from the top is fantastic!
this applies to many (most) tools of any significance. Emacs and vi both fit this description.

From the list, what strikes me is that someone is strongly promoting a particular viewpoint while being rather ignorant of the field. That qualifies the list and the advocacy.

You say "I despise "smart" modern IDEs for a variety of reasons" but several of the 'features' in the list are fundamental to IDE's. Emacs is often favored because its focus is towards an encompassing IDE. vi is a contrast because its focus is more towards editing.

To figure which is best for you, you will need to gain sufficient familiarity with both - and a few others as well including IDE's - and use them productively on your own projects. That is the only way you will be able to clarify your own preferences and values. You will likely find that different tasks are best done with different tools. A true craftsman is not a single tool fanatic but rather a person who knows and understands many tools and is able to make choices about what particular tool will be best for a particular task for the way he does things.

If you are looking to gain an expertise that will facilitate the use of good editing tools, become a regex expert and know the differences in implementation by various apps and libraries and system utilities.

speaking of system utilities, you are aware that true *nix gurus do everything with ed? Then there's Teco ...

chrism01 01-25-2013 12:06 AM

This
Quote:

I truly believe that ANYBODY who does a significant amount of Linux programming absolutely MUST learn vi. Not necessarily every single keyword or shortcut, but they must be able to use it comfortably and efficiently, including searching, string replace, jumping to certain lines or columns, etc. This is because a significant number of Linux machines don't have emacs, or kate, or pico, or nano, or any other text editor that might be familiar to them. vi is the standard, it's the default. Find the lowest of the low non-graphical embedded Linux system and what'll it have? cd, cp, mv, and vi.
is very true, except its not just Linux, its *nix systems in general eg Solaris, *BSD, OS/X, HP-UX, AIX as well, including rescue environments.

Note that vim = vi Improved, ie vim has more features than vi, but contains all the vi cmds (as well).
Note also that (on Linux anyway) 'vi' is often aliased to actually call vim.
(The really low envs like embedded and rescue envs will be just pure vi, not vim).

Oh, and I personally use vi/vim; just in case you cared ;)

Tirsa 01-25-2013 12:18 AM

Quote:

Originally Posted by bryanl (Post 4877139)
You say "I despise "smart" modern IDEs for a variety of reasons" but several of the 'features' in the list are fundamental to IDE's.

Yes. But the reasons I despise smart IDEs are NOT on that list.

Quote:

speaking of system utilities, you are aware that true *nix gurus do everything with ed? Then there's Teco ...
Right, because Viitor and Emacsitor aren't even words, right? ;) EDiting, now that's the stuff!

Sorry, I don't know any jokes about Teco . . .

Tirsa 01-25-2013 12:24 AM

Quote:

Originally Posted by suicidaleggroll (Post 4877086)
However, when you get into big programming tasks, the multiple buffers of emacs, the incredibly convenient INTERACTIVE string search and replace (which is even case-insensitive but case-aware! I freaking love that!), the syntax highlighting and auto-indenting (including BATCH auto-indenting...just highlight a block of code and hit Esc Ctrl-\), and the syntax auto-completion just can't be matched by vi.

Yes, the syntax highlighting and indenting are big on my list. I read somewhere that Emacs is really good at auto-indenting code and putting tabs where they need to be, and I haven't been able to get Vim to do this. Of course, I haven't been able to get Emacs to do it either, so I think the problem is with me.

suicidaleggroll 01-25-2013 12:34 AM

Quote:

Originally Posted by Tirsa (Post 4877155)
Yes, the syntax highlighting and indenting are big on my list. I read somewhere that Emacs is really good at auto-indenting code and putting tabs where they need to be, and I haven't been able to get Vim to do this. Of course, I haven't been able to get Emacs to do it either, so I think the problem is with me.

Emacs always does it automatically for me, just make sure the file extension is correct for the language you're working in, that's what emacs uses to set up its syntax highlighting and auto-indenting.

The little experience I have with vi's auto-indenting has been horrible. Whenever I run into a distro that has it enabled by default, I disable it immediately. It's easy to tell too, just paste some code into a vi window, and if it has "auto-indenting" enabled it'll tab the crap out of your code so by the time you hit the end of the 50 line program you pasted you're indented 150 columns over...

suicidaleggroll 01-25-2013 12:39 AM

Quote:

Originally Posted by Myk267 (Post 4877129)
Second, if someone is fumbling around with emacs, that's not really putting the two on equal footing for comparison, right? I think you knew that, though. :)

Indeed. I often see the reverse problem as well...somebody who is comfortable in vi but not anything else. There's one guy at our company that I'm thinking of in particular. He'll open a code in vi to start doing major revisions, and he spends half of his time hitting the space bar to manually indent every line 15-20 columns over (or whatever the case may be given the place in the code). Meanwhile if he had opened it in emacs he could just hit tab once and be done with it.

It's all about using the right tool for the job. Which is why I use both for different tasks, and wouldn't give up either of them.

Tirsa 01-25-2013 12:40 AM

Quote:

Originally Posted by Myk267 (Post 4877129)
@Tirsa:
I really wanted to buy into the emacs hype, but then one day I opened vim inside of emacs and something clicked and I haven't really opened emacs since. I also find vim easier on my hands in general, and that might be more of a factor than I previously considered.

As a programmer, you'll find that they both require you to shave the yak a bit and wade into the community to find the missing things you need.

Other than that, I think they're roughly at parity in features, it's the implementation that differs by a mile. Good luck in your search!


There's some high quality videos about doing things with vim here: http://vimcasts.org/episodes/archive
Scroll down to #7 for one that's all about managing windows in vim (frames, views, panes, whatever...)

Thanks. Yes, I think the hands factor is extremely important. For me it was the deciding factor. I am really drawn to Emacs based on everything I have heard, but no program is worth that sort of physical stress. I don't mind the modes in Vim, and I would honestly much rather hit escape all the time than twist my fingers into knots. The escape key is easier to replace. :)

I've watched those Vimcasts before, and they're great! Actually, one of the really good books that I mentioned earlier that I just ordered was written by the producer, Drew Neil. It has great reviews.

http://www.amazon.com/Practical-Vim-...2&keywords=vim

I'm really looking forward to reading it!

TobiSGD 01-25-2013 04:36 AM

Quote:

Originally Posted by suicidaleggroll (Post 4877156)
Emacs always does it automatically for me, just make sure the file extension is correct for the language you're working in, that's what emacs uses to set up its syntax highlighting and auto-indenting.

The little experience I have with vi's auto-indenting has been horrible. Whenever I run into a distro that has it enabled by default, I disable it immediately. It's easy to tell too, just paste some code into a vi window, and if it has "auto-indenting" enabled it'll tab the crap out of your code so by the time you hit the end of the 50 line program you pasted you're indented 150 columns over...

Put this in your .vimrc:
Code:

set pastetoggle=<F2>
Now, if you want to paste code, press F2 before you do it and Vim will not change the indenting. For Vim there is no difference if you paste or type (you can even paste a command sequence into your vim and it will execute the commands), so you have to make it aware of the pasting.

Tirsa 01-25-2013 11:13 AM

Quote:

Originally Posted by TobiSGD (Post 4877249)
Put this in your .vimrc:
Code:

set pastetoggle=<F2>
Now, if you want to paste code, press F2 before you do it and Vim will not change the indenting. For Vim there is no difference if you paste or type (you can even paste a command sequence into your vim and it will execute the commands), so you have to make it aware of the pasting.

Thanks for that. What about auto indenting code? How would you get Vim to do that? I've seen a lot of information about different options for this online, but there are so MANY options and I do not have the Vim expertise to interpret which ones I want. Is there a simple group of settings that will give me a comfortable experience writing in C and C++?

Thank you.

Edit: Wow, I actually found these great videos by Derek Wyatt and I think they have all the information I need about that. These are really good videos. I know what I'll be doing for the next two days now.

Might be too basic for some here, but here they are:

http://www.derekwyatt.org/vim/vim-tutorial-videos/


I am watching them on vimeo because the quality is better, but the blog gives a list of their proper sequence.

trey85stang 01-25-2013 04:00 PM

fyi, vi and vim are not the same thing. although everyone knows you are talking about vim.

Nothing real important to add, but I have only ever used vim and I honestly cant even stand using a traditional word editor or even an email editor anymore. For that matter more times then I can count i have put ":wq" at the end of an email. I would say pick one but dont be afraid of knowing the other... even though I never really have used emacs.


All times are GMT -5. The time now is 04:40 PM.