Vim Bits 2
26 Jun 2002
In the last part we went over basic Vim commands, modes, and movement. In this part we'll learn about buffers, windows, and cutting/copying/pasting. Note: unless otherwise noted, commands are to be entered in Normal/Command mode. So if you try something and it doesn't work, hit Escape and try again.
Moving On
Quickies
1. I tried to open a new file without saving my changes to the old one and Vim said "E37: No write since last change (use ! to override)". What does the E37 mean?
E37 is one of many error IDs that Vim uses to identify errors. If the error message isn't entirely clear to you, type ":help error-id". For example, if you're not sure why you couldn't open the new file, type ":help E37".
2. Is there any way to show the command I'm typing in as I type it?
Type ":set showcmd" or put "set showcmd" in your .vimrc. The command will appear in the ruler (the bottom line) as you type it.
3. How can I cancel a command I've started typing in?
Hit Ctrl-C.
Buffers and Windows
1. What is a buffer?
From the Vim manual: "A buffer is a file loaded into memory for editing. The original file remains unchanged until you write the buffer to the file."
2. What is a window?
From the Vim manual: "A window is a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers."
3. How do I open a file into a new buffer?
Type ":e filename". If you haven't saved your changes to the current file, you'll have to either write them first (":w" followed by the command again) or force Vim to ignore the changes for now (":e! filename").
4. Can I open more than one file at a time?
At your shell prompt type "vim filename-list" -- e.g. "vim *.c". Or, if you're already in Vim, you can type ":e filename-list".
5. How do I move back and forth between the files I've opened?
":next" will move to the next buffer; ":previous" will move to the previous. These will stop at each end of the buffer list. If you want to wrap around, use ":bn" and ":bp".
6. Can I move to the first/last buffer without going through all the rest?
Use ":first" and ":last".
6. Vim complains that I haven't saved my changes, so it won't let me switch to another buffer. What do I do?
":w" will save your changes. You can also use ":wnext" and ":wprevious", which will write the file to disk before moving to another buffer. If you like Vim to save your changes in cases like this, you can put "set autowrite" in your .vimrc. (See ":help autowrite" for more information.)
7. How do I see which files I have open?
Type ":args". The filename in brackets (e.g. "[foobar.c]") is the one you are currently editing. If you type ":args filename-list", Vim will open the files listed. When this happens, the files you were previously editing become hidden, so they won't show up with an ":args" command anymore.
8. How do I see which buffers I have open?
Type ":ls". To understand the output, read through ":help ls". (":buffers" and ":files" do the same thing. Use whichever you like best.)
9. What's the difference between :args and :ls?
":args" shows whatever is in its argument list. The argument list can be modified, however, making some buffers inactive (which means you won't be able to get to them with ":next" and ":previous"). ":ls" will list all the buffers in memory, inactive or not, excepted for unlisted ones. (Use ":ls!" to show unlisted buffers. See ":help unlisted-buffers" for more information.)
10. If a buffer's inactive or hidden or unlisted, then how on earth do I get to it?
Type ":b #" where # is the number of the buffer you want to open. ":bn" and ":bp" will still cycle through all the buffers, by the way.
11. How do I delete a buffer?
Type ":bd" to delete the current buffer. ":bd #" will delete buffer number #. This doesn't actually delete a buffer -- it makes it unlisted (see #9). For the most part ":bd" will be all you need, so don't worry about that. (Besides, it's a nice safeguard in case you didn't really want to delete that buffer.)
12. How do I really delete a buffer?
Type ":bw". But beware -- you probably don't want to use this. Use ":bd" instead. Really.
13. Do I have to use these darn buffer numbers?
No -- you can refer to a buffer by name instead. (The name is what shows up in quotes when you type ":ls".)
14. How do I copy the contents of another file into the current buffer?
Type ":r filename".
15. How do I copy the output of a command into the current buffer?
Type ":r !command". For example, ":r !date +'\%-m-\%-e-\%y: [\%A]'" will insert something that looks like this: "6-26-02: [Wednesday]". ":r" always puts its output on a new line, by the way.
16. How do I get more than one window?
Type ":split". This will split the screen horizontally, loading the current buffer into the new window. "vsplit" will split it vertically. The default behavior is to put the new window above or to the left of the current one, depending on if you're doing a horizontal or a vertical split. To change this, use the "splitbelow" and "splitright" options.
17. Can I open a file and have it load in a new window?
Type ":split filename" or ":vsplit filename".
18. What if I want to create a new file but load it in a new window?
Type ":new" or ":vnew".
19. How do I close a window?
Type ":close". You can also use ":q", but that has the disadvantage of exiting Vim if you're on the last window.
20. Can I close all the windows at once?
":qall" will quit Vim even if you have multiple windows open. Use ":wall" to save your changes first and then quit. Or, if you don't want to save your changes, use ":qall!".
20. Can I get rid of all the windows except the one I'm working in?
Type ":only".
21. Can I make the new window be a certain height?
Prefix the "split" with a number (e.g. ":3split" will create a new window 3 lines high).
22. Can I open a bunch of files from the shell and have them open in new windows?
Type "vim -o filename-list". If you want the windows split vertically, type "vim -O filename-list". E.g. "vim -o *.txt"
23. How do I move between windows?
Type Ctrl-W and then one of j/k/h/l or the arrow keys. (That is, "Ctrl-W j" will move the cursor down a window; "Ctrl-W <Right>" will move right a window, etc.) "Ctrl-W w" cycles through the windows. "Ctrl-W t" moves the cursor to the top-left window; "Ctrl-W b" moves the cursor to the bottom-right window. "Ctrl-W p" moves it to the last accessed window. Type ":help window-move-cursor" for more details.
24. Can I resize a window?
"Ctrl-W +" and "Ctrl-W -" will increase and decrease the size of the current window. You can also use the mouse -- just click on the statusline (separating the two windows) and drag to where you want it to be. This only works if you have the "mouse" option set -- put "set mouse=a" in your .vimrc and you should be fine). (Note: if you use the "mouse" option, you won't be able to use the X Windows clipboard unless you hold down Shift. This applies to both selecting text and pasting.)
25. Can I set all the windows to have the same size?
Type "Ctrl-W =".
26. I accidentally put a window in the wrong place -- can I move it to where I need it to go?
"Ctrl-W K" will move the current window to the very top. "Ctrl-W J", "Ctrl-W H", and "Ctrl-W L" will move it to the very bottom, left, and right, respectively. You can also use "Ctrl-W r" and "Ctrl-W R" to rotate the windows.
27. I've opened some files with :args and I want to create a new window for each one. Is there an easy way to do this?
Type ":all". If you want the windows split vertically, use ":vall".
28. I want to compare two files side-by-side -- is there a way to have the other window scroll whenever I scroll the one I'm working in?
Type ":set scrollbind" in both windows. See ":help scroll-binding" for more details. If you're trying to do a diff-style comparison, type "vimdiff file1 file2". This will open up the two files in vertically split windows with the scrollbind option set, and even more, it will fold up the files so that you can see exactly where the differences are. (Folding will be covered in a future installment, by the way.) You can achieve the same effect as vimdiff by opening the first file and typing ":vertical diffsplit file2". (If you want a horizontal split, use ":diffsplit file2" instead.)
29. I have a command I'd like to run on all the windows. Can I do that?
":windo" is what you want. Read ":help windo".
30. I have a command I'd like to run on all the buffers I have open. Can I do that?
":bufdo" is the answer. Read ":help bufdo". An example is the following, which will convert all the buffers to the Unix file format (this is handy when you have a bunch of DOS-formatted text files): ":bufdo set fileformat=unix"
31. Can I get a statusline even when I only have one window?
Put "set laststatus=2" in your .vimrc. Read ":help laststatus" to see the other options.
32. Can I change the format of my statusline?
Read ":help statusline" to see the available options. I have the following in my .vimrc:
set statusline=[%02n]\ %F%(\ %r%y%m%)\ %=%c%V,\ %l/%L\ \ %P
%02n puts the buffer number (zero-padded to two digits) in brackets on the left. %F displays the full filename (including path). The parentheses following %F create a grouping; if none of the options in the group are present, then the entire thing will disappear. The three options in the group (%r, %y, and %m) are for the read-only flag ([RO]), the file type ([html], for example), and the modified flag ([+]). %= separates the left-justified half from the right-justified half. %c is the column number, and %V is the "virtual" column number (for when you have tabs). %l displays the current line number and %L displays the total number of lines in the file. %P is the percentage of the way through the file. (There is a slight difference between %p and %P -- %P is more window-based -- but you can use whichever you like.) Spaces need to be escaped with a preceding backslash. Make sure you don't have quotes around the statusline.
33. I have more questions!
Read the buffer FAQ (tip #135) on the Vim Online page.
Cutting, copying, and pasting
1. I've been copying and pasting using X Windows/gdm/whatever and it's a pain. How do I do it the right way?
First of all you must select the text you want. Use Visual mode for this (see the first part of this article or read ":help visual-mode").
2. Okay, how do I cut it?
Type "d". This will delete the selected text and place it in a register.
3. What's a register?
A clipboard, basically. Vim uses it for storage when you cut or copy a selection of text. There are lots of registers -- see ":help registers" for the complete list. They are named with a quote (") and a single character -- e.g. "a, "0, "_, etc. Commands using registers will be given in single quotes to keep things from getting horribly messed up. :)
4. How do I copy instead of cut?
Type "y". Vim's name for copying is "yanking."
5. Okay, I've cut/copied my text. How do I paste it?
Move to the location where you want to put the text and type "p". ("p" actually stands for "put," not "paste," but that doesn't really matter.)
6. I've got some text I need to copy, but I already have a selection in the register and I don't want to lose it. What can I do?
Vim has ten "quote_number" registers (e.g. "0, "1, "2, etc.) that it uses for yanking and deleting text. It rotates them, so you can copy the second selection without worrying about losing your original selection. See ":help quote_number" for a fuller explanation. "0 is the default for yanking and "1 is the default for deleting. If you want, you can yank/delete and put the text in one of the named registers ("a through "z) -- for example, just type '"ay' and your text will go into the "a register.
7. How do I paste text from another register?
Prefix the "p" command with the name of the register. For example, '"ap' will paste text from the "a register. '"1p' will paste text from the "1 register.
8. How can I see what's in each register?
Type ":reg". If you just want to see what's in a particular register (without listing all the others), put its name after :reg (e.g. ':reg "a').
9. Can I delete the whole line without having to select it by hand?
Type "dd".
10. What about yanking a whole line?
Type "yy".
11. Can I yank/delete multiple lines at once?
Put the number of lines before the command. E.g. "3yy" will yank three lines (the current one and the two following it). "100dd" will delete 100 lines. If you want to put the lines in a different register, put the register name after the line count (e.g. '5"bdd' will delete five lines and place their contents in register "b).
12. Can I delete/yank from my current position to the end of the line?
Use "d$" or "y$". You can also use "D", but take note that "Y" is not mapped to "y$" by default. (The default is for "Y" to be a synonym for "yy".) Put "map Y y$" in your .vimrc if you want "Y" to behave like "y$".
13. When I paste text from the X Windows clipboard, the formatting ends up all weird. Can I fix this?
Put the following lines in your .vimrc:
nnoremap \tp :set invpaste paste?<CR>
nmap <F4> \tp
imap <F4> <C-O>\tp
set pastetoggle=<F4>
With the above keymappings, you can toggle paste mode by hitting F4. (If you would rather not use a keymapping, ":set paste" will turn paste mode on. You can turn it off with ":set nopaste".)
Coming in future parts:
Search and replace, typing in non-ASCII characters, formatting text, maps and abbreviations, using Vim for programming, Vim scripting, folding, .vimrc, and much, much more.

