views:

739

answers:

13

I heard it takes 30 days minimum to get comfortable with vi. I'm on day 2 hehe. Right now, I seem to be merely memorizing different shortcuts for things I already did in visual studio. (incremental search, prev/next word, etc)

So far the most powerful aspect seems to be the numeric keys combined with commands (5 * next line), and the idea of normal/insert modes.

There are a few things I miss from visual studio. Ctrl-Click'ing the mouse for quick copy and pasting is probably the biggest.

So that I don't get discouraged, can you guys walk me through some things in vi that you do regularly that can't be done in Visual Studio? It'll help me focus on what to learn and develop habits around.

+2  A: 

Visual Studio's regular expressions are a little bit Mickey Mouse. Vim has the full POSIX regular expression language at your fingertips.

twerq
True, but I can't remember the last time I needed a full POSIX regular expression (while coding anyway).
uncle brad
@unclebrad, I use regex search literally multiple times a day.
JSBangs
@JSB I was just saying that I get by with the Mickey Mouse regular expressions.
uncle brad
damn I suck so hard at regex.. it's probably one of my biggest flaws as a programmer. maybe using VI will help me learn though.
djmc
@JSB: In all my years of coding I've never needed to regex search a source file. If you are using regex for what you should be using an actual parser (which vim is not) for - searching for usages of a method, for instance, or finding the definition of a variable - ur doin' it wrong
BlueRaja - Danny Pflughoeft
+14  A: 

I'll just leave a link to this SO answer here.

tzaman
The linked SO is one of the crowning glories of this site. It offers only an introduction to one small part of vi (not even vim,just the original 30 year old vi), and yet every sentence reveals more capability than most IDEs. When you combine the inherent power of vi with the extensions and plug-ins available (Intellisense? Yeah, it's got that), you arrive at what is still one of the most capable and powerful development environments ever.
Zoe Gagnon
hhaha massive.. I'll definitely read this and comment back here later.
djmc
More recent vim versions even include the notion of 'objects', so that instead of `{` `d}` to delete a paragraph, you can use the wonderfully mnemonic (if `vi`-incompatible) `dap`. It extends, so to delete three paragraphs, instead of `{` `d3}` you can write `d3ap`. Also `aw` "a word", `a[` "a [block]", `a(` "a (block)", `a<` "a <block>", `a{` "a {block}", `a"` "a quoted string", (respecting single and double quotes and language-specific escaping).
hobbs
+9  A: 

VI means never ever having to take you fingers off the keyboard.

Jim Barrows
that's true.. I hate working on different laptops or computers and dealing with the inconsistencies of where Home and End and Page keys are. The mouse is a slight hassle, but so far it saves me time cuz I can accurately click around the page very fast. and my thumbpad on my laptop still lets my hands stay on the keyboard.
djmc
This is *the* reason that I can't get away from vim (and why I prefer vim to emacs).
JSBangs
So if you miss Ctrl+Clicking, then just try to learn about the register (copy/pasting) or (if you don't like it) don't use vim.
Umang
but will learning about the register to copy and paste, be faster than using my thumbpad and ctrl+click'ing? for example, when I'm renaming several words in a selection of text.
djmc
Yes. Keyboard is faster then mouse most of the time. You waste time 1)reaching over, finding the mouse. Ascreen real estate, 2) moving the mouse over unneeded 3) Moving your hand back to the keyboard, and finding home row again.With VI, you use search and replace a lot more, and with full regex, you can do some very sophisticated pattern matching. That will eliminate your need to ctrl-click like a ferret on speed.
Jim Barrows
+3  A: 

Use screen to keep a session running on a remote machine accessed over ssh

Daenyth
hhehe I love screen
djmc
+1  A: 

As far as I can tell (in Visual C# express 2010) ctrl-click just selects whatever word you click on. To do the same in VIM, you can combine the yank command with a movement command.

So you press "y" for yank (copy) then "e" or "w" to copy to the end of the word.

Peter Recore
Or "aw"/"iw" (which stands for "a word" and "inner word") if you are in the middle of the word.
d.m
I should have clarified this.. I was referring to the ability to rapidly move the mouse while ctrl-clicking all over the screen. How can I use a keyboard to copy and paste 5 times when the words are spread out all over the screen? I use my mouse and my vision to target all the words I want to replace.Is there a more efficient way to do this in VI?
djmc
@djmc - Are you serious? That example is just flagrant mousterbation. If you spend your day frantically ctrl-clicking all over the screen then you don't need vi. You need a qualude. :-)
Amardeep
hhaha maybe I'll take some screenshots and post another SO question on this aspect. I don't think its excessive or time consuming. If it's a bigger search/replace job spread over multiple files, I would use the quickfind/quickreplace box. but if it's a block of code that doesn't get changed thru refactoring intelligence, then it only takes 5 secs to mouse around and copy/paste the new code.
djmc
@djmc - Yeah it's very circumstance driven. One area where vi differs greatly from other editors is the paradigm used for cutting and pasting. It is VERY different. When I use a non-modal editor (like this comment box) I often find myself missing the directed motion operations. If I see a misspelled word I have to move the mouse there, instead of reverse-searching for the misspelling. Blocks of text area easy with { and } motions.
Amardeep
A: 

Vim Essentials is a nice set of slides.

Personally, I got used to vi a long time ago, when we didn't have the luxury of a mouse in student's Unix terminals. Since then, I used vi/vim for everything safe for writing emails.

To this day, I probably use only 1/20 of the commands, but never felt the need to write code with another text editor, and reaching for a mouse in an IDE feels very clumsy to me.

Using high level and expressive languages, that do not require an IDE (mainly python, sql, javascript) really helps. I suppose it wouldn't be as easy with Java or C++.

Not having to move and point with the mouse when coding (safe for using the browser) also helps preventing Carpal tunnel syndrome.

BTW, I suppose Vim integrates better with Unix than with Windows... and who said 30 minutes was a little optimistic :)

Marco Mariani
thanks for the link.if you're not using an IDE, I can definitely see how VI would be very very powerful.However, I was phrasing the question coming from an IDE + mouse point of view. How does VI improve upon that experience?
djmc
I don't know :) It's been a long time since I last used an IDE. But if you want the best of both worlds, http://www.viemu.com/ is about emulating vi _inside_ visual studio. It's a commercial product with a 30 days trial. For me it's mainly speed... searching for the next occurrence of the word under cursor? Press '*'. Incrementing the number under cursor? ctrl-a. And so on.
Marco Mariani
yeah that's true.. viemu would hopefully be the best of both worlds. Looks like they're still in beta for vs2k10 but I'll give it a shot anyways later tonite..I think visual studio can do a lot of the things vi can also. find next word is ctrl-f3. I'm not sure if there's an equivalent for incrementing numbers tho hehe. I guess that's useful if you need to change the initial value of a ffor loop indexer?
djmc
+3  A: 

Edit a file on a Solaris machine that only allows SSH access.

mwalker
+1  A: 

There is many differences.

  • Block (and column) wise copy, paste, edit
  • the dot command! (after duck tape the second most powerful tool on the planet, seriously)

I suggest you watch some screencasts at http://vimcasts.org/ to get a feeling of the power of vim.

e.g.:

Ronny
thanks. I'll give these screencasts a view tonite when I get home.
djmc
+4  A: 

This article is what got me started on Vim, and I never looked back:

http://www.viemu.com/a-why-vi-vim.html

It has some great examples on Vim's power.

Amir Rachum
that article is very inspiring. I read it while googling also
djmc
A: 

Edit documents over SSH. Vim's really nice for that.

Edit: looks like a lot of people have already said that :)

Rei Miyasaka
A: 

teco is your answer. You only need a PDP-10 and an ASR-33 and you're on your way!

Pete Wilson
A: 

You could always use the Vim emulator/add-on for Visual Studio and get some of the power of vim mixed with the features of VS. If you're already using Visual Studio, I assume you're using a .NET language, which without VS, would be much more painful to use.

Jason Miesionczek
Right. I'm considering installing viemu. Since you're a .NET developer also, what shortcuts or behaviors do you find yourself doing in the vim emulator that you cant do in visual studio?
djmc
+2  A: 

Note that I don't use Visual Studio, and know little about the available features in it. The following are examples of what I find useful in Vim, not a list of missing features in Visual Studio.

Macros

It's easy to create macros for complex (but repetitive) operations. To illustrate with a simple example, let's say we start with:

Line1
Line2
Line3
Line4
Line5

Now we want to envelop each line in a print(""); statement.
Place the cursor on the first line, and enter:

  • qx to start recording a macro to the register x
  • Shift+I print(" Esc    to insert text at the beginning of the line
  • Shift+A "); Esc            to append text at the end of the line
  • j to go down one line
  • q to stop recording the macro
  • 4@x to execute the macro in register x 4 times

See :help complex-repeat for more info on Vim macros.

Text objects

Note that this is one of the improvements Vim has over the traditional Vi. If it doesn't work, you're probably running in Vi compatibility mode; use :set nocompatible to enable the full functionality of Vim.

Text objects allow you to easily select regions of text. Let's say we start with the following text, and place the cursor on some text:

<b><i>some text</i></b>

Now we want to delete everything between <i> and </i>. This can be done by simply typing the command dit (d'elete i'nner t'ag)! Or if we want to include the tags themselves in our selection, use dat (d'elete a t'ag). To delete everything inside the <b> tags, use d2it (d'elete two i'nner t'ags).

You can similarly use daw (delete a word), dap (delete a paragraph), di" (delete inside double-quotes), etc; see :help text-objects for the complete list.

Another useful example of text objects:

v2ap"+y

  • v toggles visual mode. This makes it easier to see what you're selecting, and lets you adjust your selection with a series of multiple motions before you execute a command.
  • 2ap selects this paragraph and the next one
  • "+ selects the system clipboard as register for the next operation
  • y yanks the selection to the given register

In other words, that command would copy two paragraphs from your text to the system clipboard (e.g. for pasting them here at StackOverflow).

Global editing
The global command is used to apply an Ex command to all lines matching a given regular expression. Examples:

  • :global/test/print or :g/test/p would print all lines containing the phrase test
  • :global/test/delete or :g/test/d would delete said lines
  • :global/test/substitute/^/#/ or :g/test/s/^/#/ would search for lines containing the phrase test, and comment them out by substituting the regexp anchor ^ (beginning-of-line) with the symbol #.

You can also do some cool stuff by passing the search motions /pattern or ?pattern as ranges:

  • :?test?move . searches backwards for a line containing test, and moves it to your current position in the file
  • :/test/copy . searches forwards for a line containing test, and copies it to the current position in the file

Good luck and have fun learning Vim!

Jabir Ali Ouassou