What is Vim ? ------------- Vim is an almost 100% compatible version of the UNIX editor Vi. Many new features have been added: Multi level undo, syntax highlighting, command line history, filename completion, block operations, etc. Those who don't know Vi can probably skip this message, unless you are prepared to learn something new and useful. Vim is especially recommended for editing programs. Vim runs on almost any Unix flavor, MS-DOS, MS-Windows 3.1, MS-Windows 95/98/ME/NT/2000/XP, OS/2, Atari MiNT, BeOS, VMS, RISC OS, Macintosh and Amiga. For more information, see http://www.vim.org. This is also a great place to find Vim tips and scripts! Why use Vi(m) ? --------------- Vi is available on all POSIX.2-conforming systems. This allows you to move between many different systems, and know that your editor will be there and will work as you expect. The Basics ---------- To edit a file in Vim: vim You're now in Vim; you should see the contents of the file, and a line of status information on the bottom line. To quit: hit escape a couple times to be sure you're in normal mode, then: :q Normal mode ? ------------- Vim is a "modal" editor (c.f. "modeless" editors like emacs, pico, nedit, ...). The modes you will encounter most are: - Normal mode: the starting mode, where you enter most commands. You can get into Normal mode from (almost) any other mode by hitting twice. - Insert mode: the mode you'll usually be in when entering text. Type a 'i' (lowercase I) from Normal mode to get into Insert mode. - Visual mode: for selecting blocks to be operated on. - Type a 'v' from Normal mode to get into Visual mode. - Type a 'V' from Normal mode to get into Visual Line mode. - Type a ^V from Normal mode to get into Visual Block mode. - Command-line mode (Cmdline mode): for entering ex commands. From normal mode, type any of: '!', ':', '/', '?'. In addition, there are a few variants of the basic modes: - Replace mode: like Insert mode, but typing characters will overwrite what's already there. (hit 'R' from Normal mode, or the "insert" key while in Insert mode) - Insert Normal mode: When in Insert mode, and you want to type one command, you can type ^O (Control + 'o') to enter Insert Normal mode, which will accept one command, and then drop back into Insert mode. - Insert Visual mode: like Insert Normal mode, but you the command you type is a command which starts Visual mode ('v', 'V', or ^V). (for more on the different types of modes, type ":help vim-modes" into vim while in normal mode.) That's pretty confusing... -------------------------- Your fingers and brain are likely to be confused when first learning Vim. There are a few prompts available to you to help you out, however. For instance, as I'm typing this, down at the lower left corner of my terminal is the helpful text: -- INSERT -- This tells me that I'm currently in Insert mode. Hitting to go into Normal mode removes that bit of text to tell me I'm in Normal mode. These are the only two modes you strictly need, so if you're just trying to survive in vi over a 300 bps modem link, that's all you'll need. Things you probably want to do in an editor ------------------------------------------- So you can open a file and quit. You probably want to be able to move around the file, search for things, do search and replace, and so on. Movement: k ^ | h <--+--> l | v j To move around a file in Normal mode, use 'h', 'j', 'k', and 'l' to move left, up, right, and down. Those blessed with a numeric keypad can probably move around with the keypad in all modes, assuming numlock is off (and your TERM environment variable is set up right, and the moon is in phase). Those with bona-fide arrow keys can probably move around in all modes (some restrictions apply, your mileage may vary). Searching: From Normal mode, type '/' (which will place you in Command-line mode), then type what you're searching for. This may be combined with other commands (eg: d/< will delete from the current cursor position to the next <). To search backwards, type '?' Search and Replace: Go to the line you want to search and replace on, then type ':' from Normal mode to get into Command-line mode, then type 's///'. If you want to replace over the whole file, just type ':%s///'. For more information, run the command 'vimtutor' on any machine that has a recent install of vim, and it will start vim with a text file with a tutorial in it that details all this and more. On to more fun stuff -------------------- Ok, so we promised that Vim would help you 'code quicker and cleaner'. Syntax highlighting: The very first thing I turn on when starting vim is syntax highlighting. It should be noted that gvim has better support for syntax highlighting, because you can have many more colors in X than on the console. On the other hand, gvim tends to be installed slightly less frequently. In any case, let's open up a file. vim foo.c :syntax on Vim comes bundled with several syntax files, which contain more or less descriptions of keywords and constructs in various languages, and corresponding colors that go with them. My installation of Vim comes with 387 syntax files for various file types, including C, C++, Objective C, Perl, Java, Lisp, Scheme, Postscript, the .XPM image file format, Verilog, Visual Basic, HTML, Ruby, PHP, and hundreds more. Automatic (re-)Indentation My installation of Vim comes with 45 indentation files that tell Vim how to indent C code, CSS, HTML, Java, PostScript, Ruby, XML, and dozens of other file types. To try it out, select a block of code and type '=='. Vim will re-indent the lines you selected according to what it thinks the file type is. Alternatively, if you have a block of code that has mixed indentation, eg some lines have tabs and some have spaces, you can fix it with retab. Select the block you wish to change (in visual mode), then use the command :retab to set everything to spaces (equal to the current tabstop value for each tab), or :retab! to set everything to tabs. ^N/^P, tab completion While typing in a buffer, if at any time you want to complete the current token, you can type ^P to search backwards through the buffer and complete the current token, or ^N to search forwards, doing the same. If you're used to the command line and completing filenames and directory names with the tab key, add the following snippet to your .vimrc: ======================cut here================ function! InsertTabWrapper(direction) let col = col('.') - 1 if !col || getline('.')[col - 1] !~ '\k' return "\" elseif "backward" == a:direction return "\" else return "\" endif endfunction inoremap =InsertTabWrapper ("forward") inoremap =InsertTabWrapper ("backward") ======================cut here================ This remaps the key to semi-intelligently either complete the current word or insert an actual tab (like, say, if you're at the beginning of a line and you want to tab out to where the line of code should start). For more tips ------------- For more fun stuff that you can do with vim, check out vim.org, specifically http://www.vim.org/tips/index.php and http://www.vim.org/scripts/index.php For those burning questions that you know somebody has run into before, try http://vimdoc.sourceforge.net/vimfaq.html This file --------- The original file will be available at http://www.linux.ucla.edu/~psnwbrgr/vimtalk.txt This slightly modified file will be at http://www.linux.ucla.edu/~ashandarei/vimtalk.txt Phil Snowberger psnwbrgr@linux.ucla.edu Mark Painter ashandarei@linux.ucla.edu