Вы находитесь на странице: 1из 32

<home><Linux help><directory><who?

><contact><search>

< The Vim commands cheat sheet - 1.2 > A Vim cheat sheet, listing some useful, essential and most often used Vim commands. Author: Nana Lngstedt < nana.langstedt at gmail.com > tuXfile created: 18 January 2003 Last modified: 5 September 2009 contents

Working with files Moving around in the file Inserting and overwriting text Deleting text Entering visual mode Editing blocks of text Undo and redo Search Replace

back to

Tips and cheat sheets Linux help tuXfiles home < The list of Vim commands > Working with files Vim command :e filename :w filename :q :q! :wq :x Action Open a new file. You can use the Tab key for automatic file name completion, just like at the shell command prompt. Save changes to a file. If you don't specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, specify the file name. Quit Vim. If you have unsaved changes, Vim refuses to exit. Exit Vim without saving changes. Write the file and exit. Almost the same as :wq, write the file and exit if you've made changes to the file. If you haven't made any changes to the file, Vim exits without writing the file.

Moving around in the file These Vim commands and keys work both in command mode and visual mode. Vim command j or Up Arrow k or Down Arrow Action Move the cursor up one line. Down one line.

h or Left Arrow l or Right Arrow e E b B 0 ^ $ H M L :n

Left one character. Right one character. To the end of a word. To the end of a whitespace-delimited word. To the beginning of a word. To the beginning of a whitespace-delimited word. To the beginning of a line. To the first non-whitespace character of a line. To the end of a line. To the first line of the screen. To the middle line of the screen. To the the last line of the screen. Jump to line number n. For example, to jump to line 42, you'd type :42

Inserting and overwriting text Vim command i I a A o O C r R The ESC key Deleting text Vim command x X dd or :d Action Delete characters under the cursor. Delete characters before the cursor. Delete the current line. Action Insert before cursor. Insert to the start of the current line. Append after cursor. Append to the end of the current line. Open a new line below and insert. Open a new line above and insert. Change the rest of the current line. Overwrite one character. After overwriting the single character, go back to command mode. Enter insert mode but replace characters rather than inserting. Exit insert/overwrite mode and go back to command mode.

Entering visual mode Vim command v V Action Start highlighting characters. Use the normal movement keys and commands to select text for highlighting. Start highlighting lines.

The ESC key

Exit visual mode and return to command mode.

Editing blocks of text Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when you haven't selected any text. Vim command ~ > (V) < (V) c (V) y (V) d (V) yy or :y or Y dd or :d p P Undo and redo Vim command u U Ctrl + r Search Vim command /pattern n N Replace Vim command :rs/foo/bar/a Action Substitute foo with bar. r determines the range and a determines the arguments. Action Search the file for pattern. Scan for next search match in the same direction. Scan for next search match but opposite direction. Action Undo the last action. Undo all the latest changes that were made to the current line. Redo. Action Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor. Shift right (indent). Shift left (de-indent). Change the highlighted text. Yank the highlighted text. In Windows terms, "copy the selected text to clipboard." Delete the highlighted text. In Windows terms, "cut the selected text to clipboard." Yank the current line. You don't need to highlight it first. Delete the current line. Again, you don't need to highlight it first. Put the text you yanked or deleted. In Windows terms, "paste the contents of the clipboard". Put characters after the cursor. Put lines below the current line. Put characters before the cursor. Put lines above the current line.

The range (r) can be nothing number % Work on current line only. Work on the line whose number you give. The whole file.

Arguments (a) can be g Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.

i I c Examples :452s/foo/bar/ :s/foo/bar/g :%s/foo/bar/g :%s/foo/bar/gi :%s/foo/bar/gc :%s/foo/bar/c

Ignore case for the search pattern. Don't ignore case. Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution.

Replace the first occurrence of the word foo with bar on line number 452. Replace every occurrence of the word foo with bar on current line. Replace every occurrence of the word foo with bar in the whole file. The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on. Confirm every substitution. For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.

Linux help > Tips and cheat sheets > The Vim commands cheat sheet

Copyright 2001 - 2011 Nana Lngstedt

For those tasks I use most of the time block selection. Put your cursor on the first # character, press Ctrl - q , and go down until the last commented line and press x , that will delete all the # characters vertically. For commenting a block of text is almost the same, go to the first line you want to comment, press
Ctrlq , then select until the last line, and press I#Esc , that will insert a # character on all selected lines.

Cursor movement

h - move left j - move down k - move up l - move right w - jump by start of words (punctuation considered words) W - jump by words (spaces separate words) e - jump to end of words (punctuation considered words) E - jump to end of words (no punctuation) b - jump backward by words (punctuation considered words) B - jump backward by words (no punctuation) 0 - (zero) start of line ^ - first non-blank character of line $ - end of line G - Go To command (prefix with number - 5G goes to line 5)

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Insert Mode - Inserting/Appending text


i - start insert mode at cursor I - insert at the beginning of the line a - append after the cursor A - append at the end of the line o - open (append) blank line below current line (no need to press return) O - open blank line above current line ea - append at end of word Esc - exit insert mode

Editing

r - replace a single character (does not use insert mode) J - join line below to the current one cc - change (replace) an entire line cw - change (replace) to the end of word c$ - change (replace) to the end of line s - delete character at cursor and subsitute text S - delete line at cursor and substitute text (same as cc) xp - transpose two letters (delete and paste, technically) u - undo . - repeat last command

Marking text (visual mode)


v - start visual mode, mark lines, then do command (such as y-yank) V - start Linewise visual mode o - move to other end of marked area Ctrl+v - start visual block mode O - move to Other corner of block aw - mark a word ab - a () block (with braces) aB - a {} block (with brackets) ib - inner () block iB - inner {} block Esc - exit visual mode

Visual commands

> - shift right < - shift left y - yank (copy) marked text d - delete marked text ~ - switch case

Cut and Paste


yy - yank (copy) a line 2yy - yank 2 lines yw - yank word y$ - yank to end of line p - put (paste) the clipboard after cursor P - put (paste) before cursor dd - delete (cut) a line dw - delete (cut) the current word x - delete (cut) current character

Exiting

:w - write (save) the file, but don't exit :wq - write (save) and quit :q - quit (fails if anything has changed) :q! - quit and throw away changes

Search/Replace

/pattern - search for pattern ?pattern - search backward for pattern n - repeat search in same direction N - repeat search in opposite direction :%s/old/new/g - replace all old with new throughout file :%s/old/new/gc - replace all old with new throughout file with confirmations

Working with multiple files


:e filename - Edit a file in a new buffer :bnext (or :bn) - go to next buffer :bprev (of :bp) - go to previous buffer :bd - delete a buffer (close a file) :sp filename - Open a file in a new buffer and split window ctrl+ws - Split windows ctrl+ww - switch between windows ctrl+wq - Quit a window ctrl+wv - Split windows vertically

Another good vim commands cheatsheet and a vi introduction using the "cheat sheet" method

Help

FAQ

Both

Vim documentation: windows


main help file

*windows.txt*

For Vim version 7.3. VIM REFERENCE MANUAL

Last change: 2010 Aug 15 by Bram Moolenaar

Editing with multiple windows and buffers.

*windows* *buffers*

The commands which have been added to use multiple windows and buffers are explained here. Additionally, there are explanations for commands that work differently when used in combination with more than one window. The basics are explained in chapter 7 and 8 of the user manual |usr_07.txt| |usr_08.txt|. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Introduction Starting Vim Opening and closing a window Moving cursor to other windows Moving windows around Window resizing Argument and buffer list commands Do a command in all buffers or windows Tag or file name under the cursor The preview window Using hidden buffers Special kinds of buffers |windows-intro| |windows-starting| |opening-window| |window-move-cursor| |window-moving| |window-resize| |buffer-list| |list-repeat| |window-tag| |preview-window| |buffer-hidden| |special-buffers|

{Vi does not have any of these commands} {not able to use multiple windows when the |+windows| feature was disabled at compile time} {not able to use vertically split windows when the |+vertsplit| feature was disabled at compile time} =========================================================================== === 1. Introduction Summary: A buffer is the in-memory text of a file. A window is a viewport on a buffer. A tab page is a collection of windows. A window is a viewport onto a buffer. You can use multiple windows on one buffer, or several windows on different buffers. A buffer is a file loaded into memory for editing. remains The original file *windows-intro* *window*

unchanged until you write the buffer to the file. A buffer can be in one of three states: *active-buffer* The buffer is displayed in a window. If there is a file for this buffer, it has been read into the buffer. The buffer may have modified since then and thus be different from the file. hidden: it active buffer, you just can't see it. *inactive-buffer* inactive: The buffer is not displayed and does not contain anything. Options for the buffer are remembered if the file was once loaded. It can contain marks from the |viminfo| file. But the buffer doesn't contain text. In a table: state active hidden inactive displayed in window yes no no loaded yes yes no ":buffers" shows 'a' 'h' '' '' The buffer is not displayed. has been read into the buffer. *hidden-buffer* If there is a file for this buffer, Otherwise it's the same as an

active: been

Note: All CTRL-W commands can also be executed with |:wincmd|, for those places where a Normal mode command can't be used or is inconvenient. The main Vim window can hold several split windows. There are also tab pages |tab-page|, each of which can hold multiple windows. =========================================================================== === 2. Starting Vim By default, Vim starts with one window, just like Vi. The "-o" and "-O" arguments to Vim can be used to open a window for each file in the argument list. The "-o" argument will split the windows horizontally; the "-O" argument will split the windows vertically. If both "-o" and "-O" are given, the last one encountered will be used to determine the split orientation. For example, this will open three windows, split horizontally: vim -o file1 file2 file3 "-oN", where N is a decimal number, opens N windows split horizontally. If there are more file names than windows, only N windows are opened and some files do not get a window. If there are more windows than file names, the *windows-starting*

last few windows will be editing empty buffers. Similarly, "-ON" opens N windows split vertically, with the same restrictions. If there are many file names, the windows will become very small. You might want to set the 'winheight' and/or 'winwidth' options to create a workable situation. Buf/Win Enter/Leave |autocommand|s are not executed when opening the new windows and reading the files, that's only done when they are really entered. *status-line* The 'laststatus' option

A status line will be used to tells when the last window also has 'laststatus' = 0 'laststatus' = 1 'laststatus' = 2

separate windows.

a status line: never a status line status line if there is more than one window always a status line

You can change the contents of the status line with the 'statusline' option. This option can be local to the window, so that you can have a different status line in each window. Normally, inversion is used to display the status line. This can be changed with the 's' character in the 'highlight' option. For example, "sb" sets it to bold characters. If no highlighting is used for the status line ("sn"), the '^' character is used for the current window, and '=' for other windows. If the mouse is supported and enabled with the 'mouse' option, a status line can be dragged to resize windows. Note: If you expect your status line to be in reverse video and it isn't, check if the 'highlight' option contains "si". In version 3.0, this meant to invert the status line. Now it should be "sr", reverse the status line, as "si" now stands for italic! If italic is not available on your terminal, the status line is inverted anyway; you will only see this problem on terminals that have termcap codes for italics. =========================================================================== === 3. Opening and closing a window window* *E36* CTRL-W s CTRL-W S CTRL-W CTRL-S :[N]sp[lit] [++opt] [+cmd] *opening-

*CTRL-W_s* *CTRL-W_S* *CTRL-W_CTRL-S* *:sp* *:split*

Split current window in two. on the same file. half window

The result is two viewports

Make new window N high (default is to use Reduces the current

the height of the current window). option

height to create room (and others, if the 'equalalways' is set, 'eadirection' isn't "hor", and one of them is higher than the current or the new window). Note: CTRL-S does not work on all terminals and might block further input, use CTRL-Q to get going again. Also see |++opt| and |+cmd|. CTRL-W CTRL-V CTRL-W v *CTRL-W_CTRL-V* *CTRL-W_v*

:[N]vs[plit] [++opt] [+cmd] [file] *:vs* *:vsplit* Like |:split|, but split vertically. The windows will be spread out horizontally if 1. a width was not specified, 2. 'equalalways' is set, 3. 'eadirection' isn't "ver", and 4. one of the other windows is wider than the current or new window. Note: In other places CTRL-Q does the same as CTRL-V, but here it doesn't! CTRL-W n CTRL-W CTRL_N *CTRL-W_n* *CTRL-W_CTRL-N*

:[N]new [++opt] [+cmd] *:new* Create a new window and start editing an empty file in it. Make new window N high (default is to use half the existing height). Reduces the current window height to create room (and others, if the 'equalalways' option is set and 'eadirection' isn't "hor"). Also see |++opt| and |+cmd|. If 'fileformats' is not empty, the first format given will be used for the new buffer. If 'fileformats' is empty, the 'fileformat' of the current buffer is used. This can be overridden with the |++opt| argument. Autocommands are executed in this order: 1. WinLeave for the current window 2. WinEnter for the new window 3. BufLeave for the current buffer 4. BufEnter for the new buffer This behaves like a ":split" first, and then a ":e" command.

:[N]vne[w] [++opt] [+cmd] [file] *:vne* *:vnew* Like |:new|, but split vertically. If 'equalalways' is set and 'eadirection' isn't "ver" the windows will be spread out horizontally, unless a width was specified. :[N]new [++opt] [+cmd] {file} :[N]sp[lit] [++opt] [+cmd] {file} *:split_f* Create a new window and start editing file {file} in it. If [+cmd] is given, execute the command when the file has been loaded |+cmd|. Also see |++opt|. Make new window N high (default is to use half the existing height). Reduces the current window height to create room (and others, if the 'equalalways' option is set). :[N]sv[iew] [++opt] [+cmd] {file} *:sv* *:sview* *splitview* Same as ":split", but set 'readonly' option for this buffer. :[N]sf[ind] [++opt] [+cmd] {file} *:sf* *:sfind* *splitfind* Same as ":split", but search for {file} in 'path' like in |:find|. Doesn't split if {file} is not found. CTRL-W CTRL-^ CTRL-W ^ file. *CTRL-W_CTRL-^* *CTRL-W_^* Does ":split #", split window in two and edit alternate When a count is given, it becomes ":split #N", split window and edit buffer N. Note that the 'splitbelow' and 'splitright' options influence where a new window will appear. *:vert* *:vertical* :vert[ical] {cmd} Execute {cmd}. If it contains a command that splits a window, it will be split vertically. Doesn't work for |:execute| and |:normal|. :lefta[bove] {cmd} *:lefta* *:leftabove*

:abo[veleft] {cmd} *:abo* *:aboveleft* Execute {cmd}. If it contains a command that splits a window, it will be opened left (vertical split) or above (horizontal split) the current window. Overrules 'splitbelow' and 'splitright'. Doesn't work for |:execute| and |:normal|. :rightb[elow] {cmd} *:rightb* *:rightbelow*

:bel[owright] {cmd} *:bel* *:belowright* Execute {cmd}. If it contains a command that splits a window, it will be opened right (vertical split) or below (horizontal split) the current window. Overrules 'splitbelow' and 'splitright'. Doesn't work for |:execute| and |:normal|. *:topleft* *E442* :to[pleft] {cmd} Execute {cmd}. If it contains a command that splits a window, it will appear at the top and occupy the full width of the Vim window. When the split is vertical the window appears at the far left and occupies the full height of the Vim window. Doesn't work for |:execute| and |:normal|. *:botright* :bo[tright] {cmd} Execute {cmd}. If it contains a command that splits a window, it will appear at the bottom and occupy the full width of the Vim window. When the split is vertical the window appears at the far right and occupies the full height of the Vim window. Doesn't work for |:execute| and |:normal|. These command modifiers can be combined to make a vertically split window occupy the full height. Example: :vertical topleft edit tags Opens a vertically split, full-height window on the "tags" file at the far left of the Vim window. Closing a window CTRL-W q CTRL-W CTRL-Q :q[uit] *CTRL-W_q* *CTRL-W_CTRL-Q* Quit current window. When quitting the last window (not counting a help window), exit Vim. When 'hidden' is set, and there is only one window for the current buffer, it becomes hidden. When 'hidden' is not set, and there is only one window for current buffer, and the buffer was changed, the command fails. (Note: CTRL-Q does not work on all terminals) :q[uit]! buffer, last Quit current window. If this was the last window for a When quitting the

the

any changes to that buffer are lost.

window (not counting help windows), exit Vim. of the buffer are lost, even when 'hidden' is set. CTRL-W c :clo[se][!]

The contents

*CTRL-W_c* *:clo* *:close* Close current window. When the 'hidden' option is set, or when the buffer was changed and the [!] is used, the buffer becomes hidden (unless there is another window editing it). When there is only one window in the current tab page and there is another tab page, this closes the current tab |tab-page|. This command fails when: *E444* - There is only one window on the screen. - When 'hidden' is not set, [!] is not used, the buffer has changes, and there is no other window on this buffer. Changes to the buffer are not written and won't get lost,

page.

so this is a "safe" command. CTRL-W CTRL-C current window, but that does not work, because the CTRL-C cancels the command. *:hide* Quit current window, unless it is the last window on the screen. The buffer becomes hidden (unless there is another window editing it or 'bufhidden' is "unload" or "delete"). If the window is the last one in the current tab page the page is closed. |tab-page| The value of 'hidden' is irrelevant for this command. Changes to the buffer are not written and won't get lost, so this is a "safe" command. :hid[e] {cmd} Execute {cmd} with 'hidden' is set. The previous value of 'hidden' is restored after {cmd} has been executed. Example: :hide edit Makefile This will edit "Makefile", and hide the current buffer if has any changes. CTRL-W o CTRL-W CTRL-O :on[ly][!] other windows become hidden. *CTRL-W_o* *E445* *CTRL-W_CTRL-O* *:on* *:only* Make the current window the only one on the screen. All windows are closed. When the 'hidden' option is set, all buffers in closed *CTRL-W_CTRL-C* You might have expected that CTRL-W CTRL-C closes the

:hid[e]

tab

it

When 'hidden' is not set, and the 'autowrite' option is set, modified buffers are written. Otherwise, windows that have buffers that are modified are not removed, unless the [!] is given, then they become hidden. But modified buffers are never abandoned, so changes cannot get lost. =========================================================================== === 4. Moving cursor to other windows CTRL-W <Down> CTRL-W CTRL-J CTRL-W j cursor *window-move-cursor* *CTRL-W_<Down>* *CTRL-W_CTRL-J* *CTRL-W_j* Move cursor to Nth window below current one. Uses the position to select between alternatives. CTRL-W <Up> CTRL-W CTRL-K CTRL-W k cursor *CTRL-W_<Up>* *CTRL-W_CTRL-K* *CTRL-W_k* Move cursor to Nth window above current one. Uses the position to select between alternatives. CTRL-W <Left> CTRL-W CTRL-H CTRL-W <BS> CTRL-W h *CTRL-W_<Left>* *CTRL-W_CTRL-H* *CTRL-W_<BS>* *CTRL-W_h* Move cursor to Nth window left of current one. Uses the cursor position to select between alternatives. *CTRL-W_<Right>* *CTRL-W_CTRL-L* *CTRL-W_l* Move cursor to Nth window right of current one. Uses the cursor position to select between alternatives. *CTRL-W_w* *CTRL-W_CTRL-W* Without count: move cursor to window below/right of the current one. If there is no window below or right, go to top-left window. With count: go to Nth window (windows are numbered from top-left to bottom-right). To obtain the window number see |bufwinnr()| and |winnr()|. When N is larger than the of windows go to the last window. *CTRL-W_W* Without count: move cursor to window above/left of current one. If there is no window above or left, go to bottom-

CTRL-W <Right> CTRL-W CTRL-L CTRL-W l

CTRL-W w CTRL-W CTRL-W

number

CTRL-W W right

window. CTRL-W t CTRL-W CTRL-T CTRL-W b CTRL-W CTRL-B CTRL-W p CTRL-W CTRL-P

With count: go to Nth window, like with CTRL-W w.

*CTRL-W_t* *CTRL-W_CTRL-T* Move cursor to top-left window. *CTRL-W_b* *CTRL-W_CTRL-B* Move cursor to bottom-right window. *CTRL-W_p* *CTRL-W_CTRL-P* Go to previous (last accessed) window. *CTRL-W_P* *E441* When there is no preview window this

CTRL-W P is feature}

Go to preview window.

an error. {not available when compiled without the |+quickfix| If Visual mode is active and the new window is not for the same buffer, the Visual mode is ended. If the window is on the same buffer, the cursor position is set to keep the same Visual area selected. *:winc* *:wincmd* These commands can also be executed with ":wincmd": :[count]winc[md] {arg} Like executing CTRL-W [count] {arg}. Example: :wincmd j Moves to the window below the current one. This command is useful when a Normal mode cannot be used (for the |CursorHold| autocommand event). Or when a Normal mode command is inconvenient. The count can also be a window number. Example: :exe nr . "wincmd w" This goes to window "nr". =========================================================================== === 5. Moving windows around CTRL-W r CTRL-W CTRL-R becomes remains in the same window. This only works within the row or column of windows that the current window is in. *CTRL-W_R* *window-moving*

*CTRL-W_r* *CTRL-W_CTRL-R* *E443* Rotate windows downwards/rightwards. The first window the second one, the second one becomes the third one, etc. The last window becomes the first window. The cursor

CTRL-W R becomes The

Rotate windows upwards/leftwards.

The second window

the first one, the third one becomes the second one, etc. first window becomes the last window. in the same window. This only works within the row or column of windows that the current window is in. CTRL-W x CTRL-W CTRL-X there *CTRL-W_x* *CTRL-W_CTRL-X* Without count: Exchange current window with next one. If is no next window, exchange with previous window. With count: Exchange current window with Nth window (first window is 1). The cursor is put in the other window. When vertical and horizontal window splits are mixed, the exchange is only done in the row or column of windows that the current window is in. The following commands can be used to change the window layout. For example, when there are two vertically split windows, CTRL-W K will change that in horizontally split windows. CTRL-W H does it the other way around. *CTRL-W_K* Move the current window to be at the very top, using the width of the screen. This works like closing the current window and then creating another one with ":topleft split", except that the current window contents is used for the new window. *CTRL-W_J* Move the current window to be at the very bottom, using the full width of the screen. This works like closing the window and then creating another one with ":botright split", except that the current window contents is used for the new window. *CTRL-W_H* Move the current window to be at the far left, using the full height of the screen. This works like closing the current window and then creating another one with ":vert topleft split", except that the current window is used for the new window. {not available when compiled without the |+vertsplit| feature} *CTRL-W_L* The cursor remains

CTRL-W K full

CTRL-W J current

CTRL-W H

contents

CTRL-W L full

Move the current window to be at the far right, using the height of the screen. This works like closing the current window and then creating another one with ":vert botright split", except that the current window contents is used for the new window. {not available when compiled without the |+vertsplit|

feature} *CTRL-W_T* Move the current window to a new tab page. This fails if there is only one window in the current tab page. When a count is specified the new tab page will be opened before the tab page with this index. Otherwise it comes the current tab page. =========================================================================== === 6. Window resizing *window-resize*

CTRL-W T

after

CTRL-W = windows

*CTRL-W_=* Make all windows (almost) equally high and wide, but use 'winheight' and 'winwidth' for the current window. Windows with 'winfixheight' set keep their height and with 'winfixwidth' set keep their width.

:res[ize] -N CTRL-W -

*:res* *:resize* *CTRL-W_-* Decrease current window height by N (default 1). If used after |:vertical|: decrease width by N. *CTRL-W_+* Increase current window height by N (default 1). If used after |:vertical|: increase width by N.

:res[ize] +N CTRL-W + :res[ize] [N] CTRL-W CTRL-_ CTRL-W _ z{nr}<CR>

*CTRL-W_CTRL-_* *CTRL-W__* Set current window height to N (default: highest possible). Set current window height to {nr}. *CTRL-W_<* Decrease current window width by N (default 1). *CTRL-W_>* Increase current window width by N (default 1).

CTRL-W <

CTRL-W >

:vertical res[ize] [N] *:vertical-resize* *CTRL-W_bar* CTRL-W | Set current window width to N (default: widest possible). You can also resize a window by dragging a status line up or down with the

mouse. Or by dragging a vertical separator line left or right. This only works if the version of Vim that is being used supports the mouse and the 'mouse' option has been set to enable it. The option 'winheight' ('wh') is used to set the minimal window height of the current window. This option is used each time another window becomes the current window. If the option is '0', it is disabled. Set 'winheight' to a very large value, e.g., '9999', to make the current window always fill all available space. Set it to a reasonable value, e.g., '10', to make editing in the current window comfortable. The equivalent 'winwidth' ('wiw') option is used to set the minimal width of the current window. When the option 'equalalways' ('ea') is set, all the windows are automatically made the same size after splitting or closing a window. If you don't set this option, splitting a window will reduce the size of the current window and leave the other windows the same. When closing a window, the extra lines are given to the window above it. The 'eadirection' option limits the direction in which the 'equalalways' option is applied. The default "both" resizes in both directions. When the value is "ver" only the heights of windows are equalized. Use this when you have manually resized a vertically split window and want to keep this width. Likewise, "hor" causes only the widths of windows to be equalized. The option 'cmdheight' ('ch') is used to set the height of the commandline. If you are annoyed by the |hit-enter| prompt for long messages, set this option to 2 or 3. If there is only one window, resizing that window will also change the command line height. If there are several windows, resizing the current window will also change the height of the window below it (and sometimes the window above it). The minimal height and width of a window is set with 'winminheight' and 'winminwidth'. These are hard values, a window will never become smaller. =========================================================================== === 7. Argument and buffer list commands args list 1. :[N]argument [N] 2. :[N]next [file ..] 3. :[N]Next [N] buffer list 11. :[N]buffer [N] 12. :[N]bnext [N] 13. :[N]bNext [N] *buffer-list* meaning to arg/buf N to Nth next arg/buf to Nth previous arg/buf

4. 5. 6. 7.

:[N]previous [N] :rewind / :first :last :all

14. 15. 16. 17. 18. 19.

:[N]bprevious [N] :brewind / :bfirst :blast :ball :unhide :[N]bmod [N]

to Nth previous arg/buf to first arg/buf to last arg/buf edit all args/buffers edit all loaded buffers to Nth modified buf

split & args list split & buffer list meaning 21. :[N]sargument [N] 31. :[N]sbuffer [N] split + to arg/buf N 22. :[N]snext [file ..] 32. :[N]sbnext [N] split + to Nth next arg/buf 23. :[N]sNext [N] 33. :[N]sbNext [N] split + to Nth previous arg/buf 24. :[N]sprevious [N] 34. :[N]sbprevious [N] split + to Nth previous arg/buf 25. :srewind / :sfirst 35. :sbrewind / :sbfirst split + to first arg/buf 26. :slast 36. :sblast split + to last arg/buf 27. :sall 37. :sball edit all args/buffers 38. :sunhide edit all loaded buffers 39. :[N]sbmod [N] split + to Nth modified buf 40. :args 41. :buffers list of arguments list of buffers

The meaning of [N] depends on the command: [N] is number of buffers to go forward/backward on ?2, ?3, and ?4 [N] is an argument number, defaulting to current argument, for 1 and 21 [N] is a buffer number, defaulting to current buffer, for 11 and 31 [N] is a count for 19 and 39 Note: ":next" is an exception, because it must accept a list of file names for compatibility with Vi. The argument list and multiple windows The current position in the argument list can be different for each window. Remember that when doing ":e file", the position in the argument list stays the same, but you are not editing the file at that position. To indicate this, the file message (and the title, if you have one) shows "(file (N) of M)", where "(N)" is the current position in the file list, and "M" the number of files in the file list. All the entries in the argument list are added to the buffer list. you can also get to them with the buffer list commands, like ":bnext". Thus,

:[N]al[l][!] [N] *:al* *:all* *:sal* *:sall* :[N]sal[l][!] [N] Rearrange the screen to open one window for each argument. All other windows are closed. When a count is given, this is the maximum number of windows to open. With the |:tab| modifier open a tab page for each argument. When there are more arguments than 'tabpagemax' further ones become split windows in the last tab page. When the 'hidden' option is set, all buffers in closed windows become hidden.

When 'hidden' is not set, and the 'autowrite' option is set, modified buffers are written. Otherwise, windows that have buffers that are modified are not removed, unless the [!] is given, then they become hidden. But modified buffers are never abandoned, so changes cannot get lost. [N] is the maximum number of windows to open. 'winheight' also limits the number of windows opened ('winwidth' if |:vertical| was prepended). Buf/Win Enter/Leave autocommands are not executed for the new windows here, that's only done when they are really entered. :[N]sa[rgument][!] [++opt] [+cmd] [N] *:sa* *:sargument* Short for ":split | argument [N]": split window and go to Nth argument. But when there is no such argument, the window is not split. Also see |++opt| and |+cmd|. :[N]sn[ext][!] [++opt] [+cmd] [file ..] *:sn* *:snext* Short for ":split | [N]next": split window and go to Nth next argument. But when there is no next file, the window is not split. Also see |++opt| and |+cmd|. :[N]spr[evious][!] [++opt] [+cmd] [N] *:spr* *:sprevious*

:[N]sN[ext][!] [++opt] [+cmd] [N] *:sN* *:sNext* Short for ":split | [N]Next": split window and go to Nth previous argument. But when there is no previous file, the window is not split. Also see |++opt| and |+cmd|. *:sre* *:srewind* :sre[wind][!] [++opt] [+cmd] Short for ":split | rewind": split window and go to first argument. But when there is no argument list, the window is not split. Also see |++opt| and |+cmd|. *:sfir* *:sfirst* :sfir[st] [++opt] [+cmd] Same as ":srewind". *:sla* *:slast* :sla[st][!] [++opt] [+cmd] Short for ":split | last": split window and go to last argument. But when there is no argument list, the window is not split. Also see |++opt| and |+cmd|.

*:dr* *:drop* :dr[op] [++opt] [+cmd] {file} .. Edit the first {file} in a window. - If the file is already open in a window change to that window. - If the file is not open in a window edit the file in the current window. If the current buffer can't be YXXYabandon|ed, the window is split first. The |argument-list| is set, like with the |:next| command. The purpose of this command is that it can be used from a program that wants Vim to edit another file, e.g., a debugger. When using the |:tab| modifier each argument is opened in a tab page. The last window is used if it's empty. Also see |++opt| and |+cmd|. {only available when compiled with a GUI} =========================================================================== === 8. Do a command in all buffers or windows *list-repeat*

:windo {cmd}

*:windo* Execute {cmd} in each window. It works like doing this: CTRL-W t :{cmd} CTRL-W w :{cmd} etc. This only operates in the current tab page. When an error is detected on one window, further windows will not be visited. The last window (or where an error occurred) becomes the current window. {cmd} can contain '|' to concatenate several {cmd} must not open or close windows or reorder

commands. them. {not in Vi} {not available when compiled without the |+listcmds| feature} Also see |:tabdo|, |:argdo| and |:bufdo|. *:bufdo* Execute {cmd} in each buffer in the buffer list. It works like doing this: :bfirst :{cmd} :bnext :{cmd} etc. When the current file can't be |abandon|ed and the is not present, the command fails. When an error is detected on one buffer, further buffers will not be visited. Unlisted buffers are skipped. The last buffer (or where an error occurred) becomes

:bufdo[!] {cmd}

[!]

the current buffer. {cmd} can contain '|' to concatenate several commands. {cmd} must not delete buffers or add buffers to the buffer list. Note: While this command is executing, the Syntax autocommand event is disabled by adding it to 'eventignore'. This considerably speeds up editing each buffer. {not in Vi} {not available when compiled without the |+listcmds| feature} Also see |:tabdo|, |:argdo| and |:windo|. Examples: :windo set nolist nofoldcolumn | normal zn This resets the 'list' option and disables folding in all windows. :bufdo set fileencoding= | update This resets the 'fileencoding' in each buffer and writes it if this changed the buffer. The result is that all buffers will use the 'encoding' encoding (if conversion works properly). =========================================================================== === 9. Tag or file name under the cursor *window-tag*

*:sta* *:stag* :sta[g][!] [tagname] Does ":tag[!] [tagname]" and splits the window for the found tag. See also |:tag|. CTRL-W ] CTRL-W CTRL-] as a window N high. *CTRL-W_g]* Use identifier under cursor *CTRL-W_]* *CTRL-W_CTRL-]* Use identifier under cursor Make new

Split current window in two.

tag and jump to it in the new upper window.

CTRL-W g ] as a

Split current window in two.

tag and perform ":tselect" on it in the new upper window. Make new window N high. *CTRL-W_g_CTRL-]* CTRL-W g CTRL-] Split current window in two. Use identifier under cursor as a tag and perform ":tjump" on it in the new upper window. Make new window N high.

CTRL-W f CTRL-W CTRL-F not

*CTRL-W_f* *CTRL-W_CTRL-F* Split current window in two. Edit file name under cursor. Like ":split gf", but window isn't split if the file does exist. Uses the 'path' variable as a list of directory names where

to look for the file. Also the path for current file is used to search for the file name. If the name is a hypertext link that looks like "type://machine/path", only "/path" is used. If a count is given, the count'th matching file is edited. {not available when the |+file_in_path| feature was disabled at compile time} CTRL-W F Split current window in two. and jump to the line number following the file name. See |gF| for details on how the line number is obtained. {not available when the |+file_in_path| feature was disabled at compile time} CTRL-W gf cursor. Like "tab split" and "gf", but the new tab page isn't created if the file does not exist. {not available when the |+file_in_path| feature was disabled at compile time} CTRL-W gF *CTRL-W_gF* Open a new tab page and edit the file name under the cursor and jump to the line number following the file name. Like "tab split" and "gF", but the new tab page isn't created if the file does not exist. {not available when the |+file_in_path| feature was at compile time} Also see |CTRL-W_CTRL-I|: open window for an included file that includes the keyword under the cursor. =========================================================================== === 10. The preview window *preview-window* *CTRL-W_gf* Open a new tab page and edit the file name under the *CTRL-W_F* Edit file name under cursor

disabled

The preview window is a special window to show (preview) another file. It is normally a small window used to show an include file or definition of a function.

{not available when compiled without the |+quickfix| feature} There can be only one preview window (per tab page). It is created with one of the commands below. The 'previewheight' option can be set to specify the height of the preview window when it's opened. The 'previewwindow' option is set in the preview window to be able to recognize it. The 'winfixheight' option is set to have it keep the same height when opening/closing other windows. *:pta* *:ptag* :pta[g][!] [tagname] Does ":tag[!] [tagname]" and shows the found tag in a "Preview" window without changing the current buffer or cursor position. If a "Preview" window already exists, it is reused (like a help window is). If a new one is opened, 'previewheight' is used for the height of the window. See also |:tag|. See below for an example. |CursorHold-example| Small difference from |:tag|: When [tagname] is equal to the already displayed tag, the position in the matching tag list is not reset. This makes the CursorHold example work after a |:ptnext|. CTRL-W z CTRL-W CTRL-Z *:pclose* :pc[lose][!] 'hidden' is used, the buffer becomes hidden (unless there is another window editing it). The command fails if any "Preview" buffer cannot be closed. See also |:close|. *CTRL-W_z* *CTRL-W_CTRL-Z* *:pc* Close any "Preview" window currently open. When the

option is set, or when the buffer was changed and the [!]

*:pp* *:ppop* :[count]pp[op][!] Does ":[count]pop[!]" in the preview window. See |:pop| and |:ptag|. {not in Vi} CTRL-W } N is not given, 'previewheight' is used. *CTRL-W_}* Use identifier under cursor as a tag and perform a :ptag on it. Make the new Preview window (if required) N high. If

CTRL-W g } on

*CTRL-W_g}* Use identifier under cursor as a tag and perform a :ptjump it. Make the new Preview window (if required) N high. If

N is not given, 'previewheight' is used. *:ped* *:pedit* :ped[it][!] [++opt] [+cmd] {file} Edit {file} in the preview window. The preview window is opened like with |:ptag|. The current window and cursor position isn't changed. Useful example: :pedit +/fputc /usr/include/stdio.h *:ps* *:psearch* :[range]ps[earch][!] [count] [/]pattern[/] Works like |:ijump| but shows the found match in the preview window. The preview window is opened like with |:ptag|. The current window and cursor position isn't changed. Useful example: :psearch popen Like with the |:ptag| command, you can use this to automatically show information about the word under the cursor. This is less clever than using |:ptag|, but you don't need a tags file and it will also find matches in system include files. Example: :au! CursorHold *.[ch] nested exe "silent! psearch " . expand("<cword>") Warning: This can be slow. Example *CursorHold-example*

:au! CursorHold *.[ch] nested exe "silent! ptag " . expand("<cword>") This will cause a ":ptag" to be executed for the keyword under the cursor, when the cursor hasn't moved for the time set with 'updatetime'. The "nested" makes other autocommands be executed, so that syntax highlighting works in the preview window. The "silent!" avoids an error message when the tag could not be found. Also see |CursorHold|. To disable this again: :au! CursorHold A nice addition is to highlight the found tag, avoid the ":ptag" when there is no word under the cursor, and a few other things: :au! CursorHold *.[ch] nested call PreviewWord() :func PreviewWord() : if &previewwindow " don't do this in the preview window : return : endif : let w = expand("<cword>") " get the word under cursor : if w =~ '\a' " if the word contains a letter :

: " Delete any existing highlight before showing another tag : silent! wincmd P " jump to preview window : if &previewwindow " if we really get there... : match none " delete existing highlight : wincmd p " back to old window : endif : : " Try displaying a matching tag for the word under the cursor : try : exe "ptag " . w : catch : return : endtry : : silent! wincmd P " jump to preview window : if &previewwindow " if we really get there... : if has("folding") : silent! .foldopen " don't want a closed fold : endif : call search("$", "b") " to end of previous line : let w = substitute(w, '\\', '\\\\', "") : call search('\<\V' . w . '\>') " position cursor on match : " Add a match highlight to the word at this position : hi previewWord term=bold ctermbg=green guibg=green : exe 'match previewWord "\%' . line(".") . 'l\%' . col(".") . 'c\k*"' : wincmd p " back to old window : endif : endif :endfun =========================================================================== === 11. Using hidden buffers *buffer-hidden*

A hidden buffer is not displayed in a window, but is still loaded into memory. This makes it possible to jump from file to file, without the need to read or write the file every time you get another buffer in a window. {not available when compiled without the |+listcmds| feature} *:buffer-!* If the option 'hidden' ('hid') is set, abandoned buffers are kept for all commands that start editing another file: ":edit", ":next", ":tag", etc. The commands that move through the buffer list sometimes make the current buffer hidden although the 'hidden' option is not set. This happens when a buffer is modified, but is forced (with '!') to be removed from a window, and 'autowrite' is off or the buffer can't be written. You can make a hidden buffer not hidden by starting to edit it with any command. Or by deleting it with the ":bdelete" command. The 'hidden' is global, it is used for all buffers. The 'bufhidden' option can be used to make an exception for a specific buffer. It can take these values:

<empty> hide unload 'hidden' delete

Use the value of 'hidden'. Hide this buffer, also when 'hidden' is not set. Don't hide but unload this buffer, also when is set. Delete the buffer. *hidden-quit* while there is a hidden, modified buffer, you will Vim will make that buffer the current buffer. You this buffer (":wq") or quit without writing more hidden, modified buffers!

When you try to quit Vim get an error message and can then decide to write (":q!"). Be careful: there may be

A buffer can also be unlisted. This means it exists, but it is not in the list of buffers. |unlisted-buffer|

:files[!] :buffers[!] :ls[!] Show all buffers. Example:

*:files* *:buffers* *:ls* 1 #h "/test/text" 2u "asdf" 3 %a+ "version.c" line 1 line 0 line 1

When the [!] is included the list will show unlisted buffers (the term "unlisted" is a bit confusing then...). Each buffer has a unique number. change, so you can always go to a specific buffer with ":buffer N" or "N CTRL-^", where N is the buffer number. Indicators (chars in the same column are mutually exclusive): u % # a h = + x an unlisted buffer (only displayed when [!] is used) |unlisted-buffer| the buffer in the current window the alternate buffer for ":e #" and CTRL-^ an active buffer: it is loaded and visible a hidden buffer: It is loaded, but currently not displayed in a window |hidden-buffer| a buffer with 'modifiable' off a readonly buffer a modified buffer a buffer with read errors That number will not

*:bad* *:badd* :bad[d] [+lnum] {fname} Add file name {fname} to the buffer list, without loading it. If "lnum" is specified, the cursor will be positioned at that line when the buffer is first entered. Note that other

commands after the + will be ignored. :[N]bd[elete][!] *:bd* *:bdel* *:bdelete* *E516* :bd[elete][!] [N] Unload buffer [N] (default: current buffer) and delete it from the buffer list. If the buffer was changed, this fails, unless when [!] is specified, in which case changes are lost. The file remains unaffected. Any windows for this buffer are closed. If buffer [N] is the current buffer, another buffer will be displayed instead. This is the most recent entry in the jump list that points into a loaded buffer. Actually, the buffer isn't completely deleted, it is removed from the buffer list |unlisted-buffer| and option values, variables and mappings/abbreviations for the buffer are cleared. :bdelete[!] {bufname} *E93* *E94* Like ":bdelete[!] [N]", but buffer given by name. Note that a buffer whose name is a number cannot be referenced by that name; use the buffer number instead. Insert a backslash before a space in a buffer name. :bdelete[!] N1 N2 ... Do ":bdelete[!]" for buffer N1, N2, etc. The arguments can be buffer numbers or buffer names (but not buffer names that are a number). Insert a backslash before a space in a buffer name. :N,Mbdelete[!] Do ":bdelete[!]" for all buffers in the range N to M |inclusive|. :[N]bw[ipeout][!] *:bw* *:bwipe* *:bwipeout* *E517* :bw[ipeout][!] {bufname} :N,Mbw[ipeout][!] :bw[ipeout][!] N1 N2 ... Like |:bdelete|, but really delete the buffer. Everything related to the buffer is lost. All marks in this buffer become invalid, option settings are lost, etc. Don't use this unless you know what you are doing. :[N]bun[load][!] :bun[load][!] [N] Unload buffer allocated for remains in the buffer If the buffer *:bun* *:bunload* *E515* [N] (default: current buffer). The memory this buffer will be freed. The buffer list. was changed, this fails, unless when [!] is

specified, in which case the changes are lost. Any windows for this buffer are closed. If buffer [N] is the current buffer, another buffer will be displayed instead. This is the most recent entry in the jump list that points into a loaded buffer. :bunload[!] {bufname} Like ":bunload[!] [N]", but buffer given by name. Note that a buffer whose name is a number cannot be referenced by that name; use the buffer number instead. Insert a backslash before a space in a buffer name. :N,Mbunload[!] Do ":bunload[!]" for all buffers in the range N to M |inclusive|. :bunload[!] N1 N2 ... Do ":bunload[!]" for buffer N1, N2, etc. The arguments can be buffer numbers or buffer names (but not buffer names that are a number). Insert a backslash before a space in a buffer name. :[N]b[uffer][!] [N] *:b* *:bu* *:buf* *:buffer* *E86* Edit buffer [N] from the buffer list. If [N] is not given, the current buffer remains being edited. See |:buffer-!| for [!]. This will also edit a buffer that is not in the buffer list, without setting the 'buflisted' flag. :[N]b[uffer][!] {bufname} Edit buffer for {bufname} from the buffer list. See |:buffer-!| for [!]. This will also edit a buffer that is not in the buffer list, without setting the 'buflisted' flag. :[N]sb[uffer] [N] *:sb* *:sbuffer* Split window and edit buffer [N] from the buffer list. If [N] is not given, the current buffer is edited. Respects the "useopen" setting of 'switchbuf' when splitting. This will also edit a buffer that is not in the buffer list, without setting the 'buflisted' flag. :[N]sb[uffer] {bufname} Split window and edit buffer for {bufname} from the buffer list. This will also edit a buffer that is not in the buffer list, without setting the 'buflisted' flag. Note: If what you want to do is split the buffer, make a copy under another name, you can do it this way: :w foobar | sp # :[N]bn[ext][!] [N] *:bn* *:bnext* *E87*

Go to [N]th next buffer in buffer list. one.

[N] defaults to

Wraps around the end of the buffer list. See |:buffer-!| for [!]. If you are in a help buffer, this takes you to the next help buffer (if there is one). normal (non-help) buffer, this takes you to the next normal buffer. This is so that if you have invoked help, it doesn't get in the way when you're browsing code/text buffers. The next three commands also work like this. *:sbn* *:sbnext* :[N]sbn[ext] [N] Split window and go to [N]th next buffer in buffer list. Wraps around the end of the buffer list. Uses 'switchbuf' :[N]bN[ext][!] [N] *:bN* *:bNext* *:bp* *:bprevious* *E88* :[N]bp[revious][!] [N] Go to [N]th previous buffer in buffer list. [N] defaults to one. Wraps around the start of the buffer list. See |:buffer-!| for [!] and 'switchbuf'. :[N]sbN[ext] [N] *:sbN* *:sbNext* *:sbp* *:sbprevious* :[N]sbp[revious] [N] Split window and go to [N]th previous buffer in buffer list. Wraps around the start of the buffer list. Uses 'switchbuf'. *:br* *:brewind* Go to first buffer in buffer list. If the buffer list is empty, go to the first unlisted buffer. See |:buffer-!| for [!]. *:bf* *:bfirst* :bf[irst] Same as ":brewind". *:sbr* *:sbrewind* Split window and go to first buffer in buffer list. If the buffer list is empty, go to the first unlisted buffer. Respects the 'switchbuf' option. *:sbf* *:sbfirst* :sbf[irst] Same as ":sbrewind". *:bl* *:blast* If the buffer list is Similarly, if you are in a

:br[ewind][!]

:sbr[ewind]

:bl[ast][!]

Go to last buffer in buffer list.

empty, go to the last unlisted buffer. See |:buffer-!| for [!]. *:sbl* *:sblast* Split window and go to last buffer in buffer list. If the buffer list is empty, go to the last unlisted buffer. Respects 'switchbuf' option.

:sbl[ast]

:[N]bm[odified][!] [N] *:bm* *:bmodified* *E84* Go to [N]th next modified buffer. Note: this command also finds unlisted buffers. If there is no modified buffer the command fails. :[N]sbm[odified] [N] *:sbm* *:sbmodified* Split window and go to [N]th next modified buffer. Respects 'switchbuf' option. Note: this command also finds buffers not in the buffer list. :[N]unh[ide] [N] *:unh* *:unhide* *:sun* *:sunhide* :[N]sun[hide] [N] Rearrange the screen to open one window for each loaded buffer in the buffer list. When a count is given, this is the maximum number of windows to open. :[N]ba[ll] [N] *:ba* *:ball* *:sba* *:sball* :[N]sba[ll] [N] Rearrange the screen to open one window for each buffer in the buffer list. When a count is given, this is the maximum number of windows to open. 'winheight' also limits the number of windows opened ('winwidth' if |:vertical| was prepended). Buf/Win Enter/Leave autocommands are not executed for the new windows here, that's only done when they are really entered. When the |:tab| modifier is used new windows are opened in a new tab, up to 'tabpagemax'. Note: All the commands above that start editing another buffer, keep the 'readonly' flag as it was. This differs from the ":edit" command, which sets the 'readonly' flag each time the file is read. =========================================================================== === 12. Special kinds of buffers *special-buffers*

Instead of containing the text of a file, buffers can also be used for other purposes. A few options can be set to change the behavior of a buffer: 'bufhidden' what happens when the buffer is no longer displayed

'buftype' 'swapfile' 'buflisted'

in a window. what kind of a buffer this is whether the buffer will have a swap file buffer shows up in the buffer list

A few useful kinds of a buffer: quickfix Used to contain the error list or the location list. See |:cwindow| and |:lwindow|. This command sets the 'buftype' option to "quickfix". You are not supposed to change this! 'swapfile' is off. Contains a help file. Will only be created with the

help |:help|

command. The flag that indicates a help buffer is internal and can't be changed. The 'buflisted' option will be reset for a help buffer. directory explorer Displays directory contents. plugin. Can be used by a file

The buffer is created with these settings: :setlocal buftype=nowrite :setlocal bufhidden=delete :setlocal noswapfile The buffer name is the name of the directory and is adjusted when using the |:cd| command. scratch kept Contains text that can be discarded at any time. It is

when closing the window, it must be deleted explicitly. Settings: :setlocal buftype=nofile :setlocal bufhidden=hide :setlocal noswapfile The buffer name can be used to identify the buffer, if you give it a meaningful name. *unlisted-buffer* The buffer is not in the buffer list. It is not used for normal editing, but to show a help file, remember a file or marks. option, thus it doesn't completely delete the buffer. :setlocal nobuflisted top - main help file Settings: The ":bdelete" command will also set this

unlisted name

Help

FAQ

Both

Вам также может понравиться