17

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 help me develop better habits.

CJBS
  • 15,147
  • 6
  • 86
  • 135
djmc
  • 853
  • 9
  • 20
  • 9
    obligatory http://imgs.xkcd.com/comics/real_programmers.png –  Jul 16 '10 at 20:35
  • 1
    Just to be snarky... I've always missed the x-mouse on Windows... even a ctrl-click is too much movement for that functionality. – Zoe Jul 16 '10 at 21:00
  • hhaha ya xkcd nailed that one – djmc Jul 16 '10 at 21:37
  • imo it's more like taking a year to get your speed up to your previous editor. At first you think much about shortcuts and how to do something the best way. This is time, which distracts from the task itself. – Ronny Brendel Jul 16 '10 at 22:15

13 Answers13

18

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

Community
  • 1
  • 1
tzaman
  • 46,925
  • 11
  • 90
  • 115
  • 9
    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 Jul 16 '10 at 20:54
  • hhaha massive.. I'll definitely read this and comment back here later. – djmc Jul 16 '10 at 21:30
  • There's a Visual Studio Add-in called VsVim that allows much of VIM's functionality to be available within Visual Studio.... so in that respect, the answer is "almost nothing"! See http://visualstudiogallery.msdn.microsoft.com/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329 or use `Tools -> Extensions & Updates` within Visual Studio, and search for VsVim. – CJBS Mar 20 '14 at 18:08
12

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

Jim Barrows
  • 3,634
  • 1
  • 25
  • 36
  • 1
    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 Jul 16 '10 at 21:31
  • This is *the* reason that I can't get away from vim (and why I prefer vim to emacs). – JSBձոգչ Jul 17 '10 at 03:53
  • 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 Jul 17 '10 at 10:31
  • 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 Jul 17 '10 at 19:53
  • 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 Jul 19 '10 at 19:05
8

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!

jabirali
  • 1,315
  • 7
  • 13
6

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

mwalker
  • 460
  • 4
  • 9
6

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
  • 76,817
  • 74
  • 166
  • 248
4

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

Daenyth
  • 35,856
  • 13
  • 85
  • 124
2

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

twerq
  • 528
  • 4
  • 11
  • 1
    True, but I can't remember the last time I needed a full POSIX regular expression (while coding anyway). –  Jul 16 '10 at 20:39
  • 2
    @unclebrad, I use regex search literally multiple times a day. – JSBձոգչ Jul 16 '10 at 20:44
  • 1
    @JSB I was just saying that I get by with the Mickey Mouse regular expressions. –  Jul 16 '10 at 21:33
  • 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 Jul 16 '10 at 21:34
  • 1
    @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 Jul 17 '10 at 02:24
  • The syntax in VS2013 is now POSIX – the_mandrill Jul 20 '15 at 13:15
2

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
  • 14,037
  • 4
  • 42
  • 62
  • 2
    Or "aw"/"iw" (which stands for "a word" and "inner word") if you are in the middle of the word. – dmedvinsky Jul 16 '10 at 20:56
  • 1
    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 Jul 16 '10 at 21:33
  • 2
    @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 AC9MF Jul 16 '10 at 22:57
  • 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 Jul 16 '10 at 23:37
  • @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 AC9MF Jul 16 '10 at 23:46
2

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 Brendel
  • 4,777
  • 5
  • 35
  • 55
2

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
  • 14,268
  • 17
  • 76
  • 108
  • 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 Jul 17 '10 at 19:58
1

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
  • 13,556
  • 6
  • 39
  • 55
  • 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 Jul 16 '10 at 21:36
  • 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 Jul 16 '10 at 22:23
  • 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 Jul 16 '10 at 22:31
1

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

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

Rei Miyasaka
  • 7,007
  • 6
  • 42
  • 69
0

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

Pete Wilson
  • 8,610
  • 6
  • 39
  • 51