Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
astr0man
Feb 21, 2007

hollyeo deuroga
It's been a really long time since we have had a vim thread, but seeing as how it is the greatest text editor in existence I think we need one. Feel free to make any suggestions and I will add them to the OP.

vim.org posted:

What Is Vim?

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.

Vim is often called a "programmer's editor," and so useful for programming that many consider it an entire IDE. It's not just for programmers, though. Vim is perfect for all kinds of text editing, from composing email to editing configuration files.

What Vim Is Not?

Vim isn't an editor designed to hold its users' hands. It is a tool, the use of which must be learned.

How do I use vim?
  • Install vim and then run vimtutor. Also, see the basic tutorial post after the OP.
  • Set up a not terrible .vimrc (see the end of the OP for recommendations).
  • Unplug your mouse (not really, but you should really get used to not relying on using your mouse).
  • Also remember that :he(lp) is your friend!

In TYOOL 2013 why don't you just use an IDE

IDE's are great and all, but there are times when some people have to SSH into a headless box and edit code. When that happens you will be thankful that you know vim instead of having to fight through using nano or something. Also, it may be hard to believe, but not having to ever touch your mouse may actually increase your productivity as a developer.

A properly configured vim will provide you with almost all of the functionality of a modern IDE. Tagfiles give you the basic functionality to jump to function/object/class declarations just like an IDE does. Other plugins will let you do fancier things like add autocompletion, and auto updating of your tagfiles.

Resources

Recommended Plugins

Plugin Management
  • vundle - It's basically like Ruby's bundler, as in it uses a "Gemfile"-like directives to download your plugins automatically for you.
  • vim-pathogen - In practical terms, pathogen.vim makes it super easy to install plugins and runtime files in their own private directories.
vundle and pathogen do essentially the same thing. If you like managing git stuff yourself pathogen works fine. If you want something that will do all the work for you use vundle.

General
  • vim-sensible - Basic vim settings that everyone should use. If you don't have a vimrc this is a good place to start.
  • vim-taglist - Provides a vim window with a navigable list of available tags.
  • NERDTree - File system browser that works from inside vim. Preview here.
  • pyclewn - Use vim as a frontend to GDB
  • ctrlp - I can't live without this any longer. Fuzzy searching is amazing and completely removes the need for any kind of folder navigation if your work is contained to one folder.
  • vimwiki - personal wiki written in markdown. Dump it in Dropbox and you have all your notes always available anywhere. I switched from OneNote/Evernote to this because I dislikep proprietary formats.
  • vim-commentary - language-agnostic commenting out of any block of code. Incredibly useful.
  • vim-textobj-user, vim-textobj-entire, vim-repeat - a few generic utilities that are pretty mandatory
  • vim-matchit - smart jumping between opening and closing tags with <C-5>, very useful in HTML editing
  • syntastic - Syntax checker
  • vim-indent-guides — useful when you deal with layout-based languages
  • vim-unimpaired — some useful mappings like inserting new lines above/below or moving text blocks up/down
  • Tabular — aligning text (e.g. :Tab /^[^=]*\zs=/l1l1 to align assignments)
  • vim-powerline — a good statusline thing

Language specific
  • python.vim - Improved Python syntax handling. The default python syntax stuff in vim is OK, but if you do any serious Python work you should use this instead.
  • vim-flake8 - PyFlakes integration. Provides Python syntax and pep8 checking from within vim.
  • clang-complete - Intellisense for C, C++ and Objective-C!
  • vim-coffee-script - amazing plugin for CoffeeScript editing. The live compiled CS code buffer is very nice.
  • vim-fireplace / paredit - Clojure specific, make developing Clojure without Emacs fairly tolerable.

Source Control
  • vim-fugitive - A git wrapper. Provides easy access to git commands from within vim. Also, vim-fugitive will make vim look for tagfiles inside .git/ if you are inside a git repo, so you don't have to worry about relative paths and crap for your tags.
  • git-ctags - This isn't actually a vim plugin, but if you are using vim-fugitive you should also use this anyways. It will auto-update your tagfiles when you make a git commit.

IDE integration
  • VsVim - Vim Emulation for Visual Studio 2010 and later. Free!
  • ViEmu - Another vim emulation thing for Visual Studio. Costs $100, but supports older VS versions and MS Word.
  • Eclim - Some thing that integrates Eclipse into vim. I've never used this but I think a lot of people like it?

Goon .vimrc's

astr0man fucked around with this message at 21:54 on Jun 20, 2013

Adbot
ADBOT LOVES YOU

astr0man
Feb 21, 2007

hollyeo deuroga
:eng101: Basic Tutorial :eng101:

This was shamelessly stolen from an archived vim thread

Modes
The aspect of vim that most shocks newcomers is that is modal - that is, the program has various different modes of operation, and you will find it extremely hard to avoid them if you want to use vim to its full potential.

By default, vim will start in normal mode. In this mode, you simply navigate the text using the h, j, k and l keys - or the normal arrow keys if you want- and press other keys to perform operations on the text. You cannot insert text in this mode. It's often best to think of normal mode as a baseline state for the editor - it's easy to get back to. If you get mired, stuck, don't know what to do, just press the Escape key until vim starts beeping at you, and you're back in normal mode, ready to try again. Remember that - the escape key is your escape key

By pressing the i or insert key, you can enter insert mode. In fact, there are a number of ways to do this starting from normal mode. To place the cursor before the current character, press i. To place it after, press I. To place it at the start of the current line, press A. To place it at the end of the line, press a. To create a new line above the cursor and move to the beginning, press O - below the cursor, press o. The text -- INSERT -- will appear at the bottom of the vim screen and you will be able to type away to your heart's content.

The third major mode is visual mode. This is usually entered from normal mode in a variety of ways, although it is possible to enter from insert mode if you wish. However, we'll focus on entering from normal mode - it is this way that the greatest magic can be peformed. Visual mode, in essence, allows you to select regions of text to perform actions on.

Absolute basics
So let's try something out. Open up vim, and have a look. If you haven't already customised vim, you'll see something similar to the screenshot above, and be in normal mode. The first thing we're going to do is exit vim - one of the things that has caught out many a UNIX newbie on the command line. In the GUI age it's not quite as relevant, but always worth knowing. To do this, from normal mode, type :q<enter>(colon qthen press the enter key). This will exit vim. Simple! You can also type :q!<enter> to exit and abandon any changes you've made to the document without prompting.

Now let's take a quick look at the help file. Again in normal mode type :he - short for :help which also works. This is something to note - any command you type starting with a colon is usually short for something else. If you want to know what it's short for, press Tab and vim will complete the command for you. Now , we should be looking at a split window- one a blank document, and the other showing some documentation. It's a good idea to read this. If you want help on a specific topic and know how to refer to it, type :he topicname and vim will take you right there if it knows the topic. To exit the help, type :q. You'll be back at your blank document again.

Now let's type and edit something. Enter insert mode, and type out anything you like. Make it a few lines long by pressing <enter> a few times as you would in any other editor. Make a few typos. Next, choose a typo. Imagine, for a moment, you've tried to type the wort emacs, but ended up with rtmacs. This is, of course, wrong. But easy to correct. Return to normal mode, and navigate to the start of the word. In normal mode, the w and b move the cursor forward and backward by one word at a time respectively. Place the cursor on the start of the word rtmacs, and still in normal mode type x. Your delete and backspace keys will also still work, though backspace will only work in insert mode usually. x is the key to delete the text under the cursor, so you should be left with the word tmacs. Now we need to replace that errant t. Place the cursor on the t, then press r, then type E. The t will be replaced by a capital E, and all will be well.

You can also repeat normal-mode commands easily. If you've just performed a command such as this and want to do it again, move to the place where you want to repeat it and press . (dot, full stop, period - whatever you call it). This is especially useful when you run a command consisting of many keystrokes. Another way to repeat command sequentially is to prefix it with a number - to delete 5 lines starting with the current one, type 5dd - 5 is the number of times to perform the command, the first d is the command (delete) and the second d tells the delete command to operate on whole lines. Ergo, 5 lines (if that many exist) will be deleted. You might want to delete 3 words including the current word - the command would be 3daw for completeness. You may have deciphered the format of vim commands of this type now - optional count, followed by the command, followed by the 'motion'. In this case, 'delete 3 entire words', or '3 times delete entire word'. To delete the two words after the current one, but leave half of the current word intact, place the cursor on earliest letter you wish to delete, and type 2dwp - see the difference? aw is the motion to represent entire words, regardless of the cursor position, and w represents from the cursor until the end of the word.

Vim contains a multitude of commands such as these for all sorts of purposes. I'll not go into them all here because this is already getting long-winded.

Visual mode and the ex line
Now we'll take a quick look at visual mode. Return to normal mode, and we'll start.

To enter visual mode in the simplest way, press the v key, and move the cursor about. Some text will be highlighted as you do so. This represents the range of text that any future commands will act upon until visual mode is cancelled. To exit visual mode without doing anything is simple - press Escape as usual.

There are two more ways to enter visual mode - V (note the case) and ^V (ctrl-v). V will enter Visual Line mode (which selects entire lines as you move up and down), and ^V enters Visual Block mode, which allows you to select an arbitrary rectangle of text.

To run a command on text selected this way, simply do as you already know how to from the Absolute Basics section, or type an ex-mode command. ex-mode? Ah, yes. Remember when we were learning to exit vim and look at the help? Those commands that start with a colon are ex-mode commands, typed into the ex line. If you have selected text in visual mode, then that selection is preserved and acted on.

Perhaps the most useful ex-mode command you'll ever use is the :s command - where s is short for substitute - the search-and-replace command. The format of this will be familiar to anyone who uses regular expressions - because it runs a regular expression. The pitfall here is that vim regular expressions are similar to Perl and POSIX ones, but not quite the same. :he pattern and :he patterns are relevant commands at this point, if you ever get stuck or want to learn a little about vim's regular expressions.

We'll just do a simple substitution here. Let's say we want to substitute the word cat for the word dog - just once - in our visual selection range. Once you've selected some text any old how, type :s/dog/cat - the first found instance of the word dog will be replaced with cat. To replaces ALL occurrences of dog, we use :s/dog/cat/g. What will happen is that vim will automatically realise that you want to work in the selection, and add some range indicators to the command as soon as you type that initial colon - so what you'll see before pressing Enter is, in fact, :'<,'>s/dog/cat/g. This only happens when there is a visual selection.

To replace only on the current line, don't select anything, but type :s/dog/cat anyway - by default, single ex-mode commands concerned with text manipulation work only on the current line. To replace within the entire document, select nothing and type :%s/dog/cat/g

Finding what you want
Now we move on to finding text in a file. This is simple. In normal mode, press / (slash), and type the text you want to find, then press <enter>. To repeat the search, just type / on its own. You can move from one match to the next by pressing n. To search backwards, type ? then the text you want to find, or to repeat a search type ? on its own. Pressing n will then move backwards through the document. To make searches ignore the case of text, in normal mode type :set ignorecase. To make searches case-sensitive again, :set noignorecase.

Conclusion

Now, we've written a lovely text file, or program or something. It's time to go to dinner, and we need to save our work and shut the computer down. We do this by entering normal mode and using the :w command. If you've started totally from scratch, use :w /path/to/filename.ext, to save the file as /path/to/filename.ext. If you've been adding to an existing file, simply type :w to save it. This command can also be combined with the :q command easily - :wq to save the file and quit. :wq filename.whatever to save the file as filename.whatever and exit.

And in all this, I've barely scratched the surface of what vim can do. I'm so very sorry. It is a fact that vim is guilty of virtually everything that is said about it. It is not imediately user-friendly, it is obtuse for only casual use, it's old, it's archaic, it's weird. It's also extremely powerful, fast, small, efficient, customisable, modern, up-to-date, and available just about everywhere. If you ever find yourself on a UNIX system, it's pretty much a foregone conclusion that at least one Vi derivative, perhaps vim itself will be present. There is a whole lot more that vim can do that I've not managed to cover in this whole introduction, and I hope to be able to cover more soon. And fear not - if this scares you, there is a script bundled with vim that allows you to change some of the behaviour to be more like MS Windows until you get more used to things.

astr0man fucked around with this message at 01:35 on Jun 5, 2013

astr0man
Feb 21, 2007

hollyeo deuroga
I'll add all that stuff to the OP when I get back to my desk. Does VSVim support versions older than 2010?

astr0man
Feb 21, 2007

hollyeo deuroga
I'm stuck using 2005 :downs:. But hey, I got my work to expense the $100 for viemu.

astr0man
Feb 21, 2007

hollyeo deuroga

DreadCthulhu posted:

What do you guys use for something like renaming a ubiquitous variable across an entire project? I know there's a way of telling vim to do a substitute with confirmation across all the open buffers. Would you use that or perhaps the shell instead?

Yeah, this is what sed is for.

I added everything people have posted to the OP.

astr0man
Feb 21, 2007

hollyeo deuroga
You guys should post your vimrc's because mine sucks.

astr0man
Feb 21, 2007

hollyeo deuroga
Oh yeah, if any of you feel like owning a really :spergin: keyboard with vim commands instead of letters on it you can get the same one as me from wasd: http://www.wasdkeyboards.com/index.php/wasd-v1-custom-keyboard.html?kt=1&k=1&s=7cdc41a18e757810db84d26f5875626f

astr0man
Feb 21, 2007

hollyeo deuroga

Keebler posted:

I've been using VIM for coding for a while now, my only complaint over using an IDE is that I don't have a nice visual debugger available to use. Does anything like the MSVC debugger exist that might run in this environment? I tend to use C/C++.

If you're comfortable using GDB pyclewn works pretty well. But I probably do end up just using the VS debugger more often.

astr0man
Feb 21, 2007

hollyeo deuroga

Sailor_Spoon posted:

https://github.com/natw/dotfiles - Here's my dotfile repo, with my vimrc and all that other good stuff. I use submodules and pathogen for plugin management. I can't say it's all in active use, but I've been tending to it for a while.

Wow, I don't know how it never occurred to me to just version my entire .vim directory using git submodules for the plugin stuff.

astr0man
Feb 21, 2007

hollyeo deuroga

Ephphatha posted:

I think that should refer to normal mode.

Thanks, fixed it.

astr0man
Feb 21, 2007

hollyeo deuroga
So vundle is pretty nifty.

astr0man
Feb 21, 2007

hollyeo deuroga
I use the built-in torte scheme because I'm too lazy to find a better one :v:. Also dark background terminals forever :colbert:

This isn't a vim question, but should I start using zsh? I've always just used bash because it's available everywhere in addition to being the default in OSX.

astr0man
Feb 21, 2007

hollyeo deuroga

MononcQc posted:

I'm using the light version of solarized: http://ethanschoonover.com/solarized

Thanks for this! Started using it today and it's awesome. Took me forever to get it to play nicely with tmux though :argh:

I'll try to get the OP up to date with everyone else's stuff soon

astr0man
Feb 21, 2007

hollyeo deuroga

DreadCthulhu posted:

Any idea how that compares to our Messiah tpope's vim-commentary? I've been using it very successfully for years, but would love to find out if there's something out there that's 10x better.

If it's good enough for tpope it should be good enough for you :colbert:

Adbot
ADBOT LOVES YOU

astr0man
Feb 21, 2007

hollyeo deuroga
The only C code completion plugins I know of (YouCompleteMe and clang-complete) rely on clang.

Also this is not really a helpful suggestion but you could always just open the man pages from inside vim with :!man 2 open :v:

  • Locked thread