Penguin
Annotated edit history of VimHowto version 11, including all changes. View license author blame.
Rev Author # Line
1 SamJansen 1 !! SamJansen's Introduction To Vim
2
3 This is of course not supposed to be a comprehensive guide, just enough information to be reasonably proficient with Vim.
4
5 Everything in Vim can be configured on a user basis by editing the [.vimrc] file in the users home directory.
6
7 ----
8 ! The Basics
9
10 Vim and Vi distinguish themselves by being text editors with modes. When startded up, Vim will be in 'normal' mode. Starting Vim is just like any other text editor:
11
12 vim myfile.c
13
14 To begin typing, simply hit 'i' on your keyboard. Alternatively, you can probably press your Insert key. Pressing one of these keys puts you in insert mode. This mode is simply for entering text, hit the Escape key once you are finished typing.
15
16 After leaving insert mode, Vim returns back to normal mode. The key to normal mode is the colon key, ':'. Pressing the colon key allows commands to be typed into Vim. Some simple commands follow:
17
18 :w[[rite] Write, or save the current file.
6 MisterHyde 19 :q[[uit] Quit Vim (will not exit if there are unsaved changes)
1 SamJansen 20 :e[[dit] <file> Edit/load a new file.
21 :help <topic> Open up a help window displaying topic.
22
23 The above commands are usually just abbreviated ':w' and such, there isn't really any point in typing ':write'.
8 SamJansen 24
25 When navigating the help, use Ctrl-] to follow a hyperlink and Ctrl-T to go back.
10 DaveKurtz 26 %%%
27 Hyperlinks are denoted by the surrounding 'bars' e.g. |bars|
28 %%%
29 If the mouse is enabled (via ":set mouse=a") you may also double-click the left mouse button on a tag between |bars|.
1 SamJansen 30
31 The commands can also be forced by appending a '!'. So ':q!' will force-quit, leaving Vim and ignoring the possibility of changes in the current file. Some can also be combined; a common combination is ':wq', quiting and saving the file at the same time.
32
33 ----
34 ! Selecting Text
35
36 All the above will work in either Vim or Vi and is enough to get started. The next logical step in learning Vim, though, are selections: we want to be able to cut and paste!
37
38 When in 'normal' mode, pressing 'v' puts you in 'visual' mode. Now navigating by the cursor keys allows you to select text. Once some text is selected, you can press various keys to perform some operation on this selection. Pressing ':' still works, many operations can be performed this way. Some basic keys to press (without the colon) are given here:
39
40 y 'Yank', or copy the current selection.
41 c Cut the current selection.
42 d Delete selection.
43 > Indent the current selection.
44 < De-indent the current selection.
45
46 While in normal mode, pressing 'p' will paste.
5 JohnMcPherson 47
48 ----
49 ! Search and replace
50
51 In normal mode,
52 :s/(search regex)/(replace string)/
53 to replace text on the current line, or
54 :%s/regex/replace/
55 for the whole buffer.
1 SamJansen 56
57 ----
58 ! Navigating Multiple Files
59
60 It is possible to open multiple files in the command line with something like so:
61
62 vim *.c sam.h
63
64 And also possible to open a lot of files via the ':edit' command. Navigating through these files is done by a few simple commands:
65
66 :bn Short for 'buffer next', switches to the next buffer.
67 :bp Short for 'buffer previous', switches to the previous buffer.
68 :b<number> Switches to buffer no. <number>.
69 :b <name> Switches to the buffer named <name>. Note that tab completion can be used here.
3 SamJansen 70
71 ----
72 ! Windows
73
74 Also, it can be quite useful to have multiple 'windows' open in vim to edit/view multiple files. Vim can split both vertically and horizontally. Accessing the windows functions requires using the 'ctrl-w' combination. Some of the combinations below work if you keep holding ctrl down, for example '<ctrl-w><ctrl-w>' also navigates to the next window.
75
76 In normal mode:
77
78 <ctrl-w>n Create a new window with a new buffer, splitting the screen with a horizontal line.
79 <ctrl-w>s Create a new window of the current buffer, splitting the screen with a horizontal line.
80 <ctrl-w>v Create a new window of the current buffer, splitting the screen with a vertical line.
81 <ctrl-w>c Close the current window.
82 <ctrl-w>o Close all the other windows (all windows but the currently selected one).
83 <ctrl-w>w Navigate to the next window.
1 SamJansen 84
85 ----
86 ! More Stuff
87
88 There are many, many ways to be more productive in Vim. This is just a short and very basic introduction. Listed here are a few commands that may come in handy.
89
90 * Look at the help for the 's', or 'substitute' command. This command works similar to 'sed', except it can just work on your selection/range/motion.
4 SamJansen 91 * :<number> goes to line no. <number>, :$ is the end of file. Alternatively, gg is the start of the file and G is the end of the file.
92 * To read read from stdin invoke Vim like so: 'vim -'. This is very useful to pipe stuff into Vim for later editing.
3 SamJansen 93 * Best of all are the scripts and useful tips at [http://vim.sf.net].
7 AristotlePagaltzis 94 * See also: ViNotes
9 DanielLawson 95 * LinuxGazette has a series of [Vim tutorials|http://www.linuxgazette.com/node/view/9039]
11 AristotlePagaltzis 96
97 ----
98 CategoryHowto