Vim Primer
From Plasmaworks
Contents |
Essential Info for Experienced Users
If you're already using Vim, here's what you need to know. If you haven't used it before, skip this section and start with the Overview, below.
- To syntax highlight and indent .slag files in Vim for Plasmacore programming:
au BufRead,BufNewFile *.slag set filetype=slag
filetype indent on
syntax on
- There is a nice graphical Vim "cheat sheet" available here: http://www.viemu.com/vi-vim-cheat-sheet.gif
- There's also a Dvorak-layout cheat sheet: http://www.egr.nau.edu/~ap27/vi-vim-cheat-sheet-dvorak.gif
Overview
Something along the lines of a word processor, Vim is one of the most frustrating, hardest-to-learn text editors available - but it's worth it! It's extremely powerful and is designed to make editing programs fast and easy once you know it. If you want to do as much as possible with as few keystrokes as possible you should give it a shot.
The secret to Vim's power (and difficult learning curve) is twofold. First off, there are two modes of operation: Command Mode and Text Edit Mode. In Command Mode, every key on the keyboard is a different editing command. Secondly, these commands may be strung together and combined in certain ways, making Vim a little programming language itself - a language that deals with text editing, with key-press statements that modify your text file in the same way that statements in other languages might draw graphics to the screen.
Vim was programmed by Bram Moolenaar and resides at http://www.vim.org.
Installing Vim
Windows
- Download and run the gvim73.exe installer.
- Add Vim's directory to your system path. To do this, click Start -> (My) Computer -> Properties, find Advanced System Settings, and click "Environment Variables" at the bottom. Find "Path" in the System Variables list, double-click it to edit it, and add something similar to the following in front of (not instead of) what's already there:
c:\Program Files\vim\vim73;
- Now you can run gvim from the command line ("gvim game.slag") and run the vim tutorial by typing "vimtutor".
- See #Configuring Vim, below, for further customization options.
Mac
Macs come with a command-line version of Vim already installed, but consider using the Mac-customized MacVim instead.
- Download MacVim 7.3 or better and install the MacVim app in the Applications folder.
- Create a folder in your home directory (e.g. "abe" if your username is abe) called "bin" and drag the "mvim" file that was also in the download into it.
- Bring up a Terminal window (Command+SPACEBAR, type in "Terminal" or else double-click Terminal in Applications->Utilities). You'll be in your home folder.
- Type "vimtutor" to learn the basics of the command-line Vim so that you know enough to finish installing mvim.
- Type "vim ~/.profile" (without the quotes but including the period in front of "profile" - that makes it a hidden file) to edit your command-line configuration file. Type "i" for insert and then type in the following line to make the "bin" folder we made one of the default places the system looks for commands. Replace "abe" with your own username:
PATH=/Users/abe/bin:$PATH
Next, hit ESCAPE and type ":wq" (without quotes). The cursor should jump to the bottom of the screen when you type the colon and then wq writes (saves) and quits. - Open another terminal window so that our change takes effect. Now you can type "mvim game.slag" (etc.) to edit source code!
- See #Configuring Vim, below, for further customization options.
Other Operating Systems
There's a good chance Vim is available for your Operating System at http://www.vim.org
Configuring Vim
Vim gets its configuration values from the file "_gvimrc" (Windows) or ".gvimrc" (Mac).
Here's an example configuration file to get you started. Click on the link and make a copy of all the text: gvimrc
Now, type one one of the following at the command prompt:
gvim "c:\Program Files\vim\_gvimrc" (Windows), or mvim ~/.gvimrc (Mac)
to start editing your configuration file. Type "i" (no quotes) to enter insert mode, then paste the copied code in (just do Edit->Paste from the menu). Finally, type:
[ESCAPE] :wq
to save (write and quit) the configuration file.
Using Vim With Plasmacore
Windows
Save the following syntax file in the folder "C:\Program Files\vim\vimfiles\syntax": http://www.plasmaworks.com/files/slag/syntax/slag.vim
Next, save this indentation file in the folder "C:\Program Files\vim\vimfiles\indent": http://www.plasmaworks.com/files/slag/indent/slag.vim
Mac
From a terminal, type the following:
cd ~ mkdir vim mkdir vim/syntax mkdir vim/indent
Save the following syntax file in the "vim/syntax" folder of your home directory: http://www.plasmaworks.com/files/slag/syntax/slag.vim
Next, save this indentation file in the "vim/indent" folder of your home directory: http://www.plasmaworks.com/files/slag/indent/slag.vim
Finally, type in the following to change the vim settings folder name from "vim" to ".vim" (which is the proper name but makes it hidden, which would have complicated the downloading process):
mv vim .vim
If you are using the configuration file listed above then highlighting and indenting will be automatically active the next time you edit a .slag file!
Quick Reference Sheets
- There is a nice graphical Vim "cheat sheet" available here: http://www.viemu.com/vi-vim-cheat-sheet.gif
- There's also a Dvorak-layout cheat sheet: http://www.egr.nau.edu/~ap27/vi-vim-cheat-sheet-dvorak.gif
Common Commands
- Remember that you begin in COMMAND MODE. The following commands are available in COMMAND MODE. All commands are case sensitive.
- This is by no means a comprehensive list of commands, but rather a useful starter set - type ":help" in Vim to access the full documentation.
Inserting
These commands will take you into TEXT-EDIT MODE. Quality, price and users testimonials are those 3 factors that I took into consideration while choosing appropriate writing service to write my essay. I hold my eye on supremeessays.com and have never regretted my choice. They provided me with custom papers of the highest quality. To return to COMMAND MODE hit the [ESC] key.
| i | Insert text before the current character |
| a | Append text after current character and before succeeding chars. |
| I | Insert text at the beginning of the current line. |
| A | Append text at the end of the current line. |
| R | Replace - overwrite existing text with new text. |
| o | Open (lower) - create a blank line after the current line and begin inserting text into it. |
| O | Open (upper) - Create a blank line before the current line for new text. |
| s | Substitute - delete character under cursor and begin inserting text. "4s" means substitute 4 chars. |
| cw | Change Word - deletes the word under the cursor and allows you to type in a replacement word. |
Delete, Cut & Paste
| x | Deletes a single character under the cursor. |
| 5x | Deletes 5 characters starting with the one under the cursor. |
| dd | Deletes a whole line of text. |
| r | Replace the current character with the next char you type. |
| d$ | Delete text from the current cursor position to the end of the line. |
| d0 | (d-zero) Deletes all characters from the beginning of the current line up to the current cursor position. |
| yy | (Yank) Copies the current line into memory. |
| 4yy | Copies 4 lines, beginning with the one under the cursor. |
| p | Paste previously deleted or yanked text, inserting it after after the current cursor position. |
| J | Joins the next line to the end of the current line. |
| v | Visual (lower) - starts visual highlighting mode per character; characters selected by cursor movement can be deleted with "d", copied with "y", changed with "c", etc. |
| V | Visual (upper) - starts visual highlighting mode per line; selected lines can be copied, deleted, saved with ":w filename", or filtered through a Unix command (such as "cat -n") with "!" - for example, "!cat -n" (Unix/Linux). |
| ^v | (Control+v) Starts a visual block. A highlighted rectangular block of text can be deleted with "d", copied with "y", be altered on all lines with "s", have letters inserted in front of the highlight on each line with "I", or have letters appened to the end the highlight on each line with "A". |
Miscellaneous
| . | (Dot) Repeats the last command that did not start with a colon. For example, typing I[SPACE][SPACE][ESC] will insert two spaces at the beginning of a line (& indent the line). Moving the cursor down and typing . (dot) will repeat that command and indent the next line too. |
| 2 | (Number) The next command you type will be performed 2 times instead of 1. To delete 26 lines, type "26dd". |
| u | Undo the last editing command. The undo command may be repeated as many times as desired (see also "Configuring Vim" below). |
| ^r | (CTRL+r) Redo the last editing command that was undone with "u". |
| qa | Start recording macro "a" (this can be "b..z" as well). Any keystrokes you type are remembered. |
| q | Stop recording macro. |
| @a | Play back macro "a" as if you just typed the keystrokes all over again. |
| @@ | Play back the most recently played macro again. |
Moving the Cursor
| h,j,k,l | (Arrow keys) Move the cursor around. h, j, k, and l can be used instead, and don't require your hand to be lifted off the keyboard. |
| :5 | Move the cursor to line 5. |
| w | Move to the next word |
| b | Move back to the previous word |
| 0 | (Zero) Moves the cursor to the beginning of the current line. |
| $ | Moves the cursor to the end of the current line. |
| gg | Move the cursor to top of the file. |
| G | Move the cursor to the bottom of the file. |
| ^G | (Control+g) Tells you what line number the cursor is currently on. |
| /pattern | Move the cursor forward (search) until the word pattern is found. Type ":help usr_27" and ":help pattern" for information on using regular expressions in your searches. For example, using a regular expression you can search for words that start with a capital letter and end with a number. |
| ?pattern | Perform a reverse search for the word pattern. |
| n | Find the next occurrence of the previous search. Searches forward if the prior search was "/" or in reverse if it was "?". |
| :%s/pat1/pat2/g | Replaces all occurrences of pat1 with pat2. For example, to change all occurrences of the word "srcx" with the word "src_x" in the file, type ":%s/srcx/src_x/g". If you are in visual mode (v, V, or ^v), you can type ":s/pat1/pat2/g" to search and replace within the highlighted text (appears on screen as if you typed ":'<,'>s/pat1/pat2/g"). |
Saving
| :w | (Write current) Save the current file. |
| :w filename | (Write as) Saves the file, calling it "filename" instead of what it's currently called. Unlike the "Save As" in most programs, you are still editing the original file after you give this command, not the new file. Use ":e" to switch to the new one. |
| :q | Quit Vim. You will be unable to quit if the file has unsaved changes (see ":q!"). |
| :q! | (Forced Quit) Quit vim even if the file hasn't been saved (change are lost). |
| :wq | (Write + Quit) Save and quit at the same time. |
| :e filename | Switch to editing the specified file. You can type part of the filename and hit [TAB] to cycle through files that match the partial name. You must save first (":w") or else use ":e! filename" to discard changes to the current file. |
| :sp filename | (Split) Use a splitscreen view to edit both the current file and the named file. Any number of files may be split over the same view using multiple consecutive "sp" commands. Use the mouse or CTRL+w plus an arrow key to move the cursor between the windows. Type ":help sp" for more info. |
| :r filename | (Read) Inserts contents of file "filename" at current cursor pos. |
Vim Family History (in a nutshell)
Vim (rhymes with "him") was based on a popular earlier program called "vi" (letters are spelled out "vee aye"). Many of the Vim commands work in Vi as well.
Vi itself is a visual mode extension to an old line editor called "ed". Designed in the days when a computer's output was printed on an actual printer, ed was designed to allow you to specify a range of lines to print or modify. For example, to get a list of all lines matching a certain regular expression (re) and then print them to the printer, you'd type:
:g/re/p