What can I do in VIM that I can't already do in Visual Studio? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionI 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.
I'll just leave a link to this SO answer here.
VI means never ever having to take you fingers off the keyboard.
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 registerx
- 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 lineq
to stop recording the macro4@x
to execute the macro in registerx
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 operationy
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!
Edit a file on a Solaris machine that only allows SSH access.
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.
Use screen to keep a session running on a remote machine accessed over ssh
Visual Studio's regular expressions are a little bit Mickey Mouse. Vim has the full POSIX regular expression language at your fingertips.
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.
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.:
- http://vimcasts.org/episodes/creating-the-vimcasts-logo-as-ascii-art/
- http://vimcasts.org/episodes/selecting-columns-with-visual-block-mode/
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.
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 :)
Edit documents over SSH. Vim's really nice for that.
Edit: looks like a lot of people have already said that :)
teco is your answer. You only need a PDP-10 and an ASR-33 and you're on your way!
精彩评论