|
Main /
VimLearning VIMThis tutorial assumes running Vim on a Cycle server, because Cycle has set up Vim in a very specific way that wouldn't be the default on your own computer. For Vim on Windows systems, please see Cygwin. Start by creating a file with this command: vim test.java
This launches Vim, editing the file "test.java". Vim offers syntax highlighting based on the extension of the file - because our file ends with .java, Vim will give us Java syntax coloring. When Vim starts, it is in "command" mode. In this mode, keystrokes will not appear in your file. In order to start writing to the file, type: i
This changes us to "insert" mode, as evidenced by the word "-- INSERT --" at the bottom left. Now the letters you write show up on the screen, and can be written to file. You can use the arrow keys to move the cursor, as well as the home/end/pg up/pg down keys on the keyboard. If you're using PuTTY, mouse wheels will not scroll your text file. Write a quick Java test program to see how syntax coloring, auto-tabbing, and commenting works in Vim. In order to save, we need to change back to command mode. In order to do this, hit ESC. "--Insert--" disappears and file information appears in the bottom left. Command mode essentialsCommands in vim tend to begin with a colon. In order to write a file to disc (save it), type: :w
In order to quit: :q
These commands can be combined to write and quit: :wq
Appending an exclamation point to either :w or :q changes its behavior: :w! fileName
This will write a new file, named fileName. :q!
This allows you to quit a file that has been modified without saving changes. Other useful Vim commandsWhen in command mode, type a '/' and a word to search for. Vim will highlight all instances of this word: /searchTerm
To get rid of the highlighting: :noh
To go directly to a line: :20
will take you to line 20, for example. This is incredibly useful when compilers throw you syntax errors. To delete the line your cursor is on: :dd
If you want to paste code from your terminal, but don't want auto-indent or auto-commenting to screw things up: :set paste
To undo this and go back to regular insert mode: :set nopaste
More advanced Vim commands are available, and can be found virtually anywhere on the internet. Configuring VimVim is incredibly flexible, and you can set default options. Several options are already set by Cycle, but you can further edit these options by creating a file in your home directory called ".vimrc" (The dot makes this a hidden file that won't show up with a "ls" command, unless you use "ls -a"). vim .vimrc
This file consists of vim commands, without the preceeding ':'. You can add the double quote character " to the beginning of lines in order to comment a line. Commands are separated by newlines. Recommended settingsHere are some useful lines for you to use in your .vimrc on the Cycle server. If you want to use Vim on your own machine, you might want to check out the Cygwin .vimrc, which fixes errors such as improper backspace. Thanks to http://jmcpherson.org/vimrc.html for an easy reference on common .vimrc settings! set cindent
This option automatically indents your code to conform to C standards set tabstop=4
set shiftwidth=4
This sets the tab to 4 spaces, as opposed to the default of 8. It will also set the hanging indent to 4 spaces, so a new line will be tabbed in 4 spaces as well. set showmatch
This will flash your cursor to a matching bracket whenever you close a brace. Very useful for making sure your expressions are balanced! set expandtab
This changes from "hard tabs" (the \t character) to "soft tabs" (A number of spaces are inserted) set vb t_vb=
This disables the error bell. You're welcome. There are many, many options in Vim. Fortunately, the in-program help is very detailed: :help [command]
There are also several websites with detailed samples. Keep in mind that Cycle has several options set for you already (such as syntax highlighting). |