| 1 |
vim:tw=75:wrap:fo=tcqn2:sw=4:sts=4:et |
| 2 |
|
| 3 |
This Vim FAQ is created from the questions and answers posted to the |
| 4 |
[email protected] user mailing list and the comp.editors newsgroup. There are |
| 5 |
several ways to solve a problem in Vim. This FAQ gives one of those several |
| 6 |
possibilities. You can explore the other ways using the information and |
| 7 |
links given in this FAQ. The credit for the answers in this FAQ goes to |
| 8 |
Peppe, Benji, Charles Campbell and numerous others. |
| 9 |
|
| 10 |
INDEX |
| 11 |
|
| 12 |
SECTION 1 - GENERAL INFORMATION |
| 13 |
1.1. What is Vim? |
| 14 |
1.2. Who wrote Vim? |
| 15 |
1.3. Is Vim compatible with Vi? |
| 16 |
1.4. What are some of the improvements of Vim over Vi? |
| 17 |
1.5. Is Vim free? |
| 18 |
|
| 19 |
SECTION 2 - RESOURCES |
| 20 |
2.1. Where can I learn more about Vim? |
| 21 |
2.2. Is there a mailing list available? |
| 22 |
2.3. Is there an archive available for the Vim mailing lists? |
| 23 |
2.4. Where can I get the Vim user manual in HTML/PDF/PS format? |
| 24 |
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a |
| 25 |
problem with my setup or with Vim? |
| 26 |
2.6. Where can I report bugs? |
| 27 |
2.7. Where can the FAQ be found? |
| 28 |
2.8. What if I don't find an answer in this FAQ? |
| 29 |
2.9. I have a patch for implementing a Vim feature. Where do I send the |
| 30 |
patch? |
| 31 |
2.10. I have a Vim tip or developed a new Vim |
| 32 |
syntax/indent/filetype/compiler plugin or developed a new script |
| 33 |
or a colorscheme. Is there a public website where I can upload |
| 34 |
this? |
| 35 |
|
| 36 |
SECTION 3 - AVAILABILITY |
| 37 |
3.1. What is the latest version of Vim? |
| 38 |
3.2. Where can I find the latest version of Vim? |
| 39 |
3.3. What platforms does it run on? |
| 40 |
3.4. Where can I download the latest version of the Vim runtime files? |
| 41 |
|
| 42 |
SECTION 4 - HELP |
| 43 |
4.1. How do I use the help files? |
| 44 |
4.2. How do I search for a keyword in the Vim help files? |
| 45 |
4.3. I am getting an error message E123, what did I do wrong? |
| 46 |
4.4. Where can I read about the various modes in Vim? |
| 47 |
4.5. How do I generate the Vim help tags file after adding a new Vim |
| 48 |
help file? |
| 49 |
4.6. Can I use compressed versions of the help files? |
| 50 |
|
| 51 |
SECTION 5 - EDITING A FILE |
| 52 |
5.1. How do I load a file in Vim for editing? |
| 53 |
5.2. How do I save the current file in another name (save as) and edit |
| 54 |
a new file? |
| 55 |
5.3. How do I change the current directory to the directory of the |
| 56 |
current file? |
| 57 |
5.4. How do I write a file without the line feed (EOL) at the end of |
| 58 |
the file? |
| 59 |
5.5. How do I configure Vim to open a file at the last edited location? |
| 60 |
5.6. When editing a file in Vim, which is being changed by an external |
| 61 |
application, Vim opens a warning window (like the confirm dialog) |
| 62 |
each time a change is detected. How do I disable this warning? |
| 63 |
5.7. How do I edit a file whose name is under the cursor? |
| 64 |
5.8. How do I reload/re-edit the current file? |
| 65 |
5.9. How do I autosave a file periodically? |
| 66 |
5.10. How do I open a file in read-only mode? |
| 67 |
5.11. How do I open a file for editing without saving the modifications |
| 68 |
to the current file? |
| 69 |
|
| 70 |
SECTION 6 - EDITING MULTIPLE FILES |
| 71 |
6.1. How do I open multiple files at once from within Vim? |
| 72 |
6.2. How do I switch between multiple files/buffers in Vim? |
| 73 |
6.3. How do I open several files in Vim, with each file in a separate |
| 74 |
window? |
| 75 |
6.4. How do I configure Vim to autoload several files at once similar |
| 76 |
to "work-sets" or "projects"? |
| 77 |
6.5. Is it possible to open multiple top level windows in a single |
| 78 |
instance of Vim similar to Nedit or emacs? |
| 79 |
6.6. How do I browse/explore directories from within Vim? |
| 80 |
6.7. How do I edit files over a network using ftp/scp/rcp/http? |
| 81 |
|
| 82 |
SECTION 7 - BACKUP |
| 83 |
7.1. When I edit and save files, Vim creates a file with the same name |
| 84 |
as the original file and a "~" character at the end. How do I stop |
| 85 |
Vim from creating this file (or) How do I disable the Vim backup |
| 86 |
file feature? |
| 87 |
7.2. How do I configure Vim to store all the backup files in a |
| 88 |
particular directory? |
| 89 |
7.3. When I save a file with Vim, the file permissions are changed. |
| 90 |
How do I configure Vim to save a file without changing the file |
| 91 |
permissions? |
| 92 |
|
| 93 |
SECTION 8 - BUFFERS |
| 94 |
8.1. I have made some modifications to a buffer. How do I edit another |
| 95 |
buffer without saving the modified buffer and also without losing |
| 96 |
the modifications? |
| 97 |
8.2. How do I configure Vim to auto-save a modified buffer when |
| 98 |
switching to another buffer? |
| 99 |
8.3. How do I replace the buffer in the current window with a blank |
| 100 |
buffer? |
| 101 |
8.4. Is there a keyboard shortcut to load a buffer by the buffer |
| 102 |
number? |
| 103 |
8.5. How do I open all the current buffers in separate windows? |
| 104 |
8.6. How do I close (delete) a buffer without exiting Vim? |
| 105 |
8.7. I have several buffers opened with ":e filename". How do I close |
| 106 |
one of the buffers without exiting Vim? |
| 107 |
8.8. When I use the command ":%bd" to delete all the buffers, not all |
| 108 |
the buffers are deleted. Why? |
| 109 |
8.9. How do I display the buffer number of the current buffer/file? |
| 110 |
8.10. How do I delete a buffer without closing the window in which the |
| 111 |
buffer is displayed? |
| 112 |
8.11. How do I map the tab key to cycle through and open all the |
| 113 |
buffers? |
| 114 |
|
| 115 |
SECTION 9 - WINDOWS |
| 116 |
9.1. What is the difference between a Vim window and a buffer? |
| 117 |
9.2. How do I increase the width of a Vim window? |
| 118 |
9.3. How do I zoom into or out of a window? |
| 119 |
9.4. How do I execute an ex command on all the open buffers or open |
| 120 |
windows or all the files in the argument list? |
| 121 |
|
| 122 |
SECTION 10 - MOTION |
| 123 |
10.1. How do I jump to the beginning (first line) or end (last line) of |
| 124 |
a file? |
| 125 |
10.2. In insert mode, when I press the <Esc> key to go to command mode, |
| 126 |
the cursor moves one character to the left (except when the cursor |
| 127 |
is on the first character of the line). Is it possible to change |
| 128 |
this behavior to keep the cursor at the same column? |
| 129 |
10.3. How do I configure Vim to maintain the horizontal cursor position |
| 130 |
when scrolling with the <Page Up>, <Page Down>, etc keys? |
| 131 |
10.4. Some lines in a file are more than the screen width and they are |
| 132 |
all wrapped. When I use the j, k keys to move from one line to the |
| 133 |
next, the cursor is moved to the next line in the file instead of |
| 134 |
the next line on the screen. How do I move from one screen line to |
| 135 |
the next? |
| 136 |
10.5. What is the definition of a sentence, paragraph and section in |
| 137 |
Vim? |
| 138 |
10.6. How do I jump to beginning or end of a sentence, paragraph or a |
| 139 |
section? |
| 140 |
10.7. I have lines in a file that extends beyond the right extent of the |
| 141 |
screen. How do I move the Vim view to the right to see the text |
| 142 |
off the screen? |
| 143 |
10.8. How do I scroll two or more buffers simultaneously? |
| 144 |
10.9. When I use my arrow keys, Vim changes modes, inserts weird |
| 145 |
characters in my document but doesn't move the cursor properly. |
| 146 |
What's going on? |
| 147 |
10.10. How do I configure Vim to move the cursor to the end of the |
| 148 |
previous line, when the left arrow key is pressed and the cursor |
| 149 |
is currently at the beginning of a line? |
| 150 |
10.11. How do I configure Vim to stay only in insert mode (modeless |
| 151 |
editing)? |
| 152 |
10.12. How do I display some context lines when scrolling text? |
| 153 |
10.13. How do I go back to previous cursor locations? |
| 154 |
|
| 155 |
SECTION 11 - SEARCHING TEXT |
| 156 |
11.1. After I searched for a text with a pattern, all the matched text |
| 157 |
stays highlighted. How do I turn off the highlighting |
| 158 |
temporarily/permanently? |
| 159 |
11.2. How do I enter a carriage return character in a search pattern? |
| 160 |
11.3. How do I search for the character ^M? |
| 161 |
11.4. How can I search/replace characters that display as '~R', '~S', |
| 162 |
etc.? |
| 163 |
11.5. How do I highlight all the non-printable characters in a file? |
| 164 |
11.6. How do I search for whole words in a file? |
| 165 |
11.7. How do I search for the current word under the cursor? |
| 166 |
11.8. How do I search for a word without regard to the case (uppercase |
| 167 |
or lowercase)? |
| 168 |
11.9. How do I search for words that occur twice consecutively? |
| 169 |
11.10. How do I count the number of times a particular word occurs in a |
| 170 |
buffer? |
| 171 |
11.11. How do I place the cursor at the end of the matched word when |
| 172 |
searching for a pattern? |
| 173 |
11.12. How do I search for an empty line? |
| 174 |
11.13. How do I search for a line containing only a single character? |
| 175 |
11.14. How do I search and replace a string in multiple files? |
| 176 |
11.15. I am using the ":s" substitute command in a mapping. When a |
| 177 |
search for a pattern fails, the map terminates. I would like the |
| 178 |
map to continue processing the next command, even if the |
| 179 |
substitute command fails. How do I do this? |
| 180 |
11.16. How do I search for the n-th occurrence of a character in a |
| 181 |
line? |
| 182 |
11.17. How do I replace a tab (or any other character) with a hard |
| 183 |
return (newline) character? |
| 184 |
11.18. How do I search for a character by its ASCII value? |
| 185 |
11.19. How do I search for long lines? |
| 186 |
11.20. How do I display all the lines in the current buffer that |
| 187 |
contain a specified pattern? |
| 188 |
11.21. How do I search for a text string that spans multiple lines? |
| 189 |
11.22. How do I search for a pattern only within a range of lines |
| 190 |
in a buffer? |
| 191 |
|
| 192 |
SECTION 12 - CHANGING TEXT |
| 193 |
12.1. How do I delete all the trailing white space characters (SPACE |
| 194 |
and TAB) at the end of all the lines in a file? |
| 195 |
12.2. How do I replace all the occurrences of multiple consecutive |
| 196 |
space characters to a single space? |
| 197 |
12.3. How do I reduce a range of empty lines into one line only? |
| 198 |
12.4. How do I delete all blank lines in a file? How do I remove all |
| 199 |
the lines containing only space characters? |
| 200 |
12.5. How do I copy/yank the current word? |
| 201 |
12.6. How do I yank text from one position to another position within a |
| 202 |
line, without yanking the entire line? |
| 203 |
12.7. When I yank some text into a register, how do I append the text |
| 204 |
to the current contents of the register? |
| 205 |
12.8. How do I yank a complete sentence that spans over more than one |
| 206 |
line? |
| 207 |
12.9. How do I yank all the lines containing a pattern into a buffer? |
| 208 |
12.10. How do I delete all the lines in a file that does not contain a |
| 209 |
pattern? |
| 210 |
12.11. How do I add a line before each line with "pattern" in it? |
| 211 |
12.12. Is there a way to operate on a line if the previous line |
| 212 |
contains a particular pattern? |
| 213 |
12.13. How do I execute a command on all the lines containing a |
| 214 |
pattern? |
| 215 |
12.14. Can I copy the character above the cursor to the current cursor |
| 216 |
position? |
| 217 |
12.15. How do I insert a blank line above/below the current line |
| 218 |
without entering insert mode? |
| 219 |
12.16. How do I insert the name of current file into the current |
| 220 |
buffer? |
| 221 |
12.17. How do I insert the contents of a Vim register into the current |
| 222 |
buffer? |
| 223 |
12.18. How do I move the cursor past the end of line and insert some |
| 224 |
characters at some columns after the end of the line? |
| 225 |
12.19. How to replace the word under the cursor (say: junk) with |
| 226 |
"foojunkbar" in Vim? |
| 227 |
12.20. How do I replace a particular text in all the files in a |
| 228 |
directory? |
| 229 |
12.21. I have some numbers in a file. How do I increment or decrement |
| 230 |
the numbers in the file? |
| 231 |
12.22. How do I reuse the last used search pattern in a ":substitute" |
| 232 |
command? |
| 233 |
12.23. How do I change the case of a string using the ":substitute" |
| 234 |
command? |
| 235 |
12.24. How do I enter characters that are not present in the keyboard? |
| 236 |
12.25. Is there a command to remove any or all digraphs? |
| 237 |
12.26. In insert mode, when I press the backspace key, it erases only |
| 238 |
the characters entered in this instance of insert mode. How do I |
| 239 |
erase previously entered characters in insert mode using the |
| 240 |
backspace key? |
| 241 |
12.27. I have a file which has lines longer than 72 characters |
| 242 |
terminated with "+" and wrapped to the next line. How can I |
| 243 |
quickly join the lines? |
| 244 |
12.28. How do I paste characterwise yanked text into separate lines? |
| 245 |
12.29. How do I change the case (uppercase, lowercase) of a word or |
| 246 |
a character or a block of text? |
| 247 |
12.30. How do I enter ASCII characters that are not present in the |
| 248 |
keyboard? |
| 249 |
12.31. How do I replace non-printable characters in a file? |
| 250 |
12.32. How do I remove duplicate lines from a buffer? |
| 251 |
12.33. How do I prefix all the lines in a file with the corresponding |
| 252 |
line numbers? |
| 253 |
12.34. How do I exchange (swap) two characters or words or lines? |
| 254 |
12.35. How do I change the characters used as word delimiters? |
| 255 |
|
| 256 |
SECTION 13 - COMPLETION IN INSERT MODE |
| 257 |
13.1. How do I complete words or lines in insert mode? |
| 258 |
13.2. How do I complete file names in insert mode? |
| 259 |
13.3. I am using CTRL-P/CTRL-N to complete words in insert mode. How do |
| 260 |
I complete words that occur after the just completed word? |
| 261 |
|
| 262 |
SECTION 14 - TEXT FORMATTING |
| 263 |
14.1. How do I format a text paragraph so that a new line is inserted |
| 264 |
at the end of each wrapped line? |
| 265 |
14.2. How do I format long lines in a file so that each line contains |
| 266 |
less than 'n' characters? |
| 267 |
14.3. How do I join short lines to the form a paragraph? |
| 268 |
14.4. How do I format bulleted and numbered lists? |
| 269 |
14.5. How do I indent lines in insert mode? |
| 270 |
14.6. How do I format/indent an entire file? |
| 271 |
14.7. How do I increase or decrease the indentation of the current |
| 272 |
line? |
| 273 |
14.8. How do I indent a block/group of lines? |
| 274 |
14.9. When I indent lines using the > or < key, the standard 8-tabstops |
| 275 |
are used instead of the current 'tabstop' setting. Why? |
| 276 |
14.10. How do I turn off the automatic indentation of text? |
| 277 |
14.11. How do I configure Vim to automatically set the 'textwidth' |
| 278 |
option to a particular value when I edit mails? |
| 279 |
14.12. Is there a way to make Vim auto-magically break lines? |
| 280 |
14.13. I am seeing a lot of ^M symbols in my file. I tried setting the |
| 281 |
'fileformat' option to 'dos' and then 'unix' and then 'mac'. |
| 282 |
None of these helped. How can I hide these symbols? |
| 283 |
14.14. When I paste some text into a Vim buffer from another |
| 284 |
application, the alignment (indentation) of the new text is |
| 285 |
messed up. How do I fix this? |
| 286 |
14.15. When there is a very long wrapped line (wrap is "on") and a line |
| 287 |
doesn't fit entirely on the screen it is not displayed at all. |
| 288 |
There are blank lines beginning with '@' symbol instead of |
| 289 |
wrapped line. If I scroll the screen to fit the line the '@' |
| 290 |
symbols disappear and the line is displayed again. What Vim |
| 291 |
setting control this behavior? |
| 292 |
14.16. How do I convert all the tab characters in a file to space |
| 293 |
characters? |
| 294 |
14.17. What Vim options can I use to edit text that will later go to a |
| 295 |
word processor? |
| 296 |
14.18. How do I join lines without adding or removing any space |
| 297 |
characters? |
| 298 |
|
| 299 |
SECTION 15 - VISUAL MODE |
| 300 |
15.1. How do I do rectangular block copying? |
| 301 |
15.2. How do I delete or change a column of text in a file? |
| 302 |
15.3. How do I apply an ex-command on a set of visually selected lines? |
| 303 |
15.4. How do I execute an ex command on a column of text selected in |
| 304 |
Visual block mode? |
| 305 |
15.5. How do I select the entire file in visual mode? |
| 306 |
15.6. When I visually select a set of lines and press the > key to |
| 307 |
indent the selected lines, the visual mode ends. How can I |
| 308 |
reselect the region for further operation? (or) How do I |
| 309 |
re-select the last selected visual area again? |
| 310 |
15.7. How do I jump to the beginning/end of a visually selected region? |
| 311 |
15.8. When I select text with mouse and then press : to enter an ex |
| 312 |
command, the selected text is replaced with the : character. How |
| 313 |
do I execute an ex command on a text selected using the mouse |
| 314 |
similar to the text selected using the visual mode? |
| 315 |
15.9. When I select a block of text using the mouse, Vim goes into |
| 316 |
selection mode instead of Visual mode. Why? |
| 317 |
|
| 318 |
SECTION 16 - COMMAND-LINE MODE |
| 319 |
16.1. How do I use the name of the current file in the command mode or |
| 320 |
an ex command line? |
| 321 |
16.2. How do I edit the text in the Vim command-line effectively? |
| 322 |
16.3. How do I switch from Vi mode to Ex mode? |
| 323 |
16.4. How do I copy the output from an ex-command into a buffer? |
| 324 |
16.5. When I press the tab key to complete the name of a file in the |
| 325 |
command mode, if there are more than one matching file names, |
| 326 |
then Vim completes the first matching file name and displays a |
| 327 |
list of all matching filenames. How do I configure Vim to only |
| 328 |
display the list of all the matching filenames and not complete |
| 329 |
the first one? |
| 330 |
16.6. How do I copy text from a buffer to the command line and from the |
| 331 |
command line to a buffer? |
| 332 |
16.7. How do I put a command onto the command history without executing |
| 333 |
it? |
| 334 |
16.8. How do I increase the height of the command-line? |
| 335 |
|
| 336 |
SECTION 17 - VIMINFO |
| 337 |
17.1. When I invoke Vim, I get error messages about illegal characters |
| 338 |
in the viminfo file. What should I do to get rid of these |
| 339 |
messages? |
| 340 |
17.2. How do I disable the viminfo feature? |
| 341 |
17.3. How do I save and use Vim marks across Vim sessions? |
| 342 |
|
| 343 |
SECTION 18 - REMOTE EDITING |
| 344 |
18.1. How do I open a file with existing instance of gvim? What |
| 345 |
happened to the Vim 5.x OpenWithVim.exe and SendToVim.exe files? |
| 346 |
18.2. How do I send a command to a Vim server to write all buffers to |
| 347 |
disk? |
| 348 |
18.3. Where can I get the documentation about the Vim remote server |
| 349 |
functionality? |
| 350 |
|
| 351 |
SECTION 19 - OPTIONS |
| 352 |
19.1. How do I configure Vim in a simple way? |
| 353 |
19.2. How do I toggle the value of an option? |
| 354 |
19.3. How do I set an option that affects only the current |
| 355 |
buffer/window? |
| 356 |
19.4. How do I use space characters for a Vim option value? |
| 357 |
19.5. Can I add (embed) Vim option settings to the contents of a file? |
| 358 |
19.6. How do I display the line numbers of all the lines in a file? |
| 359 |
19.7. How do I change the width of the line numbers displayed using the |
| 360 |
"number" option? |
| 361 |
19.8. How do I display (view) all the invisible characters like space, |
| 362 |
tabs and newlines in a file? |
| 363 |
19.9. How do I configure Vim to always display the current line and |
| 364 |
column number? |
| 365 |
19.10. How do I display the current Vim mode? |
| 366 |
19.11. How do I configure Vim to show pending/partial commands on the |
| 367 |
status line? |
| 368 |
19.12. How do I configure the Vim status line to display different |
| 369 |
settings/values? |
| 370 |
19.13. How do I configure Vim to display status line always? |
| 371 |
19.14. How do I make a Vim setting persistent across different Vim |
| 372 |
invocations/instances/sessions? |
| 373 |
19.15. Why do I hear a beep (why does my window flash) about 1 second |
| 374 |
after I hit the Escape key? |
| 375 |
19.16. How do I make the 'c' and 's' commands display a '$' instead of |
| 376 |
deleting the characters I'm changing? |
| 377 |
19.17. How do I remove more than one flag using a single ":set" command |
| 378 |
from a Vim option? |
| 379 |
|
| 380 |
SECTION 20 - MAPPING KEYS |
| 381 |
20.1. How do I know what a key is mapped to? |
| 382 |
20.2. How do list all the user-defined key mappings? |
| 383 |
20.3. How do I unmap a key? |
| 384 |
20.4. I am not able to create a mapping for the <xxx> key. What is |
| 385 |
wrong? |
| 386 |
20.5. How do I map the numeric keypad keys? |
| 387 |
20.6. How do I create a mapping that works only in visual mode? |
| 388 |
20.7. In a Vim script, how do I know which keys to use for my mappings, |
| 389 |
so that the mapped key will not collide with an already used key? |
| 390 |
20.8. How do I map the escape key? |
| 391 |
20.9. How do I map a key to perform nothing? |
| 392 |
20.10. I want to use the Tab key to indent a block of text and |
| 393 |
Shift-Tab key to unindent a block of text. How do I map the keys |
| 394 |
to do this? This behavior is similar to textpad, visual studio, |
| 395 |
etc. |
| 396 |
20.11. In my mappings the special characters like <CR> are not |
| 397 |
recognized. How can I configure Vim to recognize special |
| 398 |
characters? |
| 399 |
20.12. How do I use the '|' to separate multiple commands in a map? |
| 400 |
20.13. If I have a mapping/abbreviation whose ending is the beginning of |
| 401 |
another mapping/abbreviation, how do I keep the first from |
| 402 |
expanding into the second one? |
| 403 |
20.14. Why does it take a second or more for Vim to process a key, |
| 404 |
sometimes when I press a key? |
| 405 |
20.15. How do I map a key to run an external command using a visually |
| 406 |
selected text? |
| 407 |
20.16. How do I map the Ctrl-I key while still retaining the |
| 408 |
functionality of the <Tab> key? |
| 409 |
|
| 410 |
SECTION 21 - ABBREVIATIONS |
| 411 |
21.1. How do I auto correct misspelled words? |
| 412 |
21.2. How do I create multi-line abbreviations? |
| 413 |
21.3. When my abbreviations are expanded, an additional space character |
| 414 |
is added at the end of the expanded text. How do I avoid this |
| 415 |
character? |
| 416 |
21.4. How do I insert the current date/time stamp into the file? |
| 417 |
21.5. How do I prevent an abbreviation from expanding in insert mode? |
| 418 |
|
| 419 |
SECTION 22 - RECORD AND PLAYBACK |
| 420 |
22.1. How do I repeat an editing operation (insertion, deletion, paste, |
| 421 |
etc)? |
| 422 |
22.2. How I record and repeat a set of key sequences? |
| 423 |
22.3. How do I edit/modify a recorded set of key sequences? |
| 424 |
22.4. How do I write recorded key sequences to a file? |
| 425 |
22.5. I am using register 0 to record my key sequences (i.e. q0 .... |
| 426 |
q). In the recorded key sequences, I am yanking some text. After |
| 427 |
the first replay of the recorded key sequence, I am no longer |
| 428 |
able to play it back. |
| 429 |
|
| 430 |
SECTION 23 - AUTOCOMMANDS |
| 431 |
23.1. How do I execute a command when I try to modify a read-only file? |
| 432 |
23.2. How do I execute a command every time when entering a buffer? |
| 433 |
23.3. How do I execute a command every time when entering a window? |
| 434 |
23.4. From an autocmd, how can I determine the name of the file or the |
| 435 |
buffer number for which the autocommand is executed? |
| 436 |
23.5. How do I automatically save all the changed buffers whenever Vim |
| 437 |
loses focus? |
| 438 |
23.6. How do I execute/run a function when Vim exits to do some |
| 439 |
cleanup? |
| 440 |
|
| 441 |
SECTION 24 - SYNTAX HIGHLIGHT |
| 442 |
24.1. How do I turn off/on syntax highlighting? |
| 443 |
24.2. How do I change the background and foreground colors used by Vim? |
| 444 |
24.3. How do I change the highlight colors to suit a dark/light |
| 445 |
background? |
| 446 |
24.4. How do I change the color of the line numbers displayed when the |
| 447 |
":set number" command is used? |
| 448 |
24.5. How do I change the background color used for a Visually selected |
| 449 |
block? |
| 450 |
24.6. How do I highlight the special characters (tabs, trailing spaces, |
| 451 |
end of line, etc) displayed by the 'list' option? |
| 452 |
24.7. How do I specify a colorscheme in my .vimrc/.gvimrc file, so that |
| 453 |
Vim uses the specified colorscheme everytime? |
| 454 |
24.8. Vim syntax highlighting is broken. When I am editing a file, some |
| 455 |
parts of the file is not syntax highlighted or syntax highlighted |
| 456 |
incorrectly. |
| 457 |
24.9. Is there a built-in function to syntax-highlight the |
| 458 |
corresponding matching bracket? |
| 459 |
24.10. How do I turn off the C comment syntax highlighting? |
| 460 |
24.11. How do I add my own syntax extensions to the standard syntax |
| 461 |
files supplied with Vim? |
| 462 |
24.12. How do I replace a standard syntax file that comes with the Vim |
| 463 |
distribution with my own syntax file? |
| 464 |
24.13. How do I highlight all the characters after a particular column? |
| 465 |
24.14. How do I convert a source file (.c, .h, etc) with the Vim syntax |
| 466 |
highlighting into a HTML file? |
| 467 |
24.15. How do I list the definition of all the current highlight |
| 468 |
groups? |
| 469 |
|
| 470 |
SECTION 25 - VIM SCRIPT WRITING |
| 471 |
25.1. How do I list the names of all the scripts sourced by Vim? |
| 472 |
25.2. How do I debug Vim scripts? |
| 473 |
25.3. How do I locate the script/plugin which sets a Vim option? |
| 474 |
25.4. I am getting some error/informational messages from Vim (possibly |
| 475 |
when running a script), the messages are cleared immediately. How |
| 476 |
do I display the messages again? |
| 477 |
25.5. How do I save and restore a plugin specific information across |
| 478 |
Vim invocations? |
| 479 |
25.6. How do I start insert mode from a Vim function? |
| 480 |
25.7. How do I change the cursor position from within a Vim function? |
| 481 |
25.8. How do I check the value of an environment variable in the .vimrc |
| 482 |
file? |
| 483 |
25.9. How do I check whether an environment variable is set or not from |
| 484 |
a Vim function? |
| 485 |
25.10. How do I call/use the Vim built-in functions? |
| 486 |
25.11. I am using some normal mode commands in my Vim script. How do I |
| 487 |
avoid using the user-defined mappings for these normal mode |
| 488 |
commands and use the standard Vim functionality for these normal |
| 489 |
mode commands? |
| 490 |
25.12. How do I get a visually selected text into a Vim variable or |
| 491 |
register? |
| 492 |
25.13. I have some text in a Vim variable 'myvar'. I would like to use |
| 493 |
this variable in a ":s" substitute command to replace a text |
| 494 |
'mytext'. How do I do this? |
| 495 |
25.14. A Vim variable (bno) contains a buffer number. How do I use this |
| 496 |
variable to open the corresponding buffer? |
| 497 |
25.15. How do I store the value of a Vim option into a Vim variable? |
| 498 |
25.16. I have copied and inserted some text into a buffer from a Vim |
| 499 |
function. How do I indent the inserted text from the Vim |
| 500 |
function? |
| 501 |
25.17. How do I get the character under the cursor from a Vim script? |
| 502 |
25.18. How do I get the name of the current file without the extension? |
| 503 |
25.19. How do I get the basename of the current file? |
| 504 |
25.20. How do I get the output from a Vim function into the current |
| 505 |
buffer? |
| 506 |
25.21. How do I call external programs from a Vim function? |
| 507 |
25.22. How do I get the return status of a program executed using the |
| 508 |
":!" command? |
| 509 |
25.23. How do I determine whether the current buffer is modified or |
| 510 |
not? |
| 511 |
25.24. I would like to use the carriage return character in a normal |
| 512 |
command from a Vim script. How do I specify the carriage return |
| 513 |
character? |
| 514 |
25.25. How do I split long lines in a Vim script? |
| 515 |
25.26. When I try to "execute" my function using the "execute 'echo |
| 516 |
Myfunc()'" command, the cursor is moved to the top of the |
| 517 |
current buffer. Why? |
| 518 |
25.27. How do I source/execute the contents of a register? |
| 519 |
25.28. After calling a Vim function or a mapping, when I press the 'u' |
| 520 |
key to undo the last change, Vim undoes all the changes made by |
| 521 |
the mapping/function. Why? |
| 522 |
25.29. How can I call a function defined with s: (script local |
| 523 |
function) from another script/plugin? |
| 524 |
25.30. Is it possible to un-source a sourced script? In otherwords, |
| 525 |
reverse all the commands executed by sourcing a script. |
| 526 |
|
| 527 |
SECTION 26 - PLUGINS |
| 528 |
26.1. How do I set different options for different types of files? |
| 529 |
26.2. I have downloaded a Vim plugin or a syntax file or a indent file, |
| 530 |
or a color scheme or a filetype plugin from the web. Where should |
| 531 |
I copy these files so that Vim will find them? |
| 532 |
26.3. How do I extend an existing filetype plugin? |
| 533 |
26.4. How do I turn off loading the Vim plugins? |
| 534 |
26.5. How do I turn on/off loading the filetype plugins? |
| 535 |
26.6. How do I override settings made in a file type plugin in the |
| 536 |
global ftplugin directory for all the file types? |
| 537 |
26.7. How do I disable the Vim directory browser plugin? |
| 538 |
26.8. How do I set the filetype option for files with names matching a |
| 539 |
particular pattern or depending on the file extension? |
| 540 |
|
| 541 |
SECTION 27 - EDITING PROGRAM FILES |
| 542 |
27.1. How do I enable automatic indentation for C/C++ files? |
| 543 |
27.2. How do I configure the indentation used for C/C++ files? |
| 544 |
27.3. How do I turn off the automatic indentation feature? |
| 545 |
27.4. How do I change the number of space characters used for the |
| 546 |
automatic indentation? |
| 547 |
27.5. I am editing a C program using Vim. How do I display the |
| 548 |
definition of a macro or a variable? |
| 549 |
27.6. I am editing a C program using Vim. How do I jump to the |
| 550 |
beginning or end of a code block from within the block? |
| 551 |
27.7. Is there a way to turn off the "//" comment auto-insertion |
| 552 |
behavior for C++ files? If I'm sitting on a line beginning with |
| 553 |
"//", then I open a new line above or below it, Vim automatically |
| 554 |
inserts new "//" chars. |
| 555 |
27.8. How do I add the comment character '#' to a set of lines at the |
| 556 |
beginning of each line? |
| 557 |
27.9. How do I edit a header file with the same name as the |
| 558 |
corresponding C source file? |
| 559 |
27.10. How do I automatically insert comment leaders while typing |
| 560 |
comments? |
| 561 |
|
| 562 |
SECTION 28 - QUICKFIX |
| 563 |
28.1. How do I build programs from Vim? |
| 564 |
28.2. When I run the make command in Vim I get the errors listed as the |
| 565 |
compiler compiles the program. When it finishes this list |
| 566 |
disappears and I have to use the :clist command to see the error |
| 567 |
message again. Is there any other way to see these error |
| 568 |
messages? |
| 569 |
|
| 570 |
SECTION 29 - FOLDING |
| 571 |
29.1. How do I extend the Vim folding support? |
| 572 |
29.2. When I enable folding by setting the 'foldmethod' option, all the |
| 573 |
folds are closed. How do I prevent this? |
| 574 |
29.3. How do I control how many folds will be opened when I start |
| 575 |
editing a file? |
| 576 |
29.4. How do I open and close folds using the mouse? |
| 577 |
29.5. How do I change the text displayed for a closed fold? |
| 578 |
29.6. How do I store and restore manually created folds across |
| 579 |
different Vim invocations? |
| 580 |
|
| 581 |
SECTION 30 - VIM WITH EXTERNAL APPLICATIONS |
| 582 |
30.1. Can I run a shell inside a Vim window? |
| 583 |
30.2. How do I pass the word under the cursor to an external command? |
| 584 |
30.3. How do I get the output of a shell command into a Vim buffer? |
| 585 |
30.4. How do I pipe the contents of the current buffer to an external |
| 586 |
command and replace the contents of the buffer with the output |
| 587 |
from the command? |
| 588 |
30.5. How do I sort a section of my file? |
| 589 |
30.6. Is there a step-by-step guide for using Vim with slrn? |
| 590 |
30.7. How do I use Vim as a pager? |
| 591 |
30.8. How do I view Unix man pages from inside Vim? |
| 592 |
30.9. How do I change the diff command used by the Vim diff support? |
| 593 |
30.10. How do I use the Vim diff mode without folding? |
| 594 |
|
| 595 |
SECTION 31 - GUI VIM |
| 596 |
31.1. How do I create buffer specific menus? |
| 597 |
31.2. How do I change the font used by GUI Vim? |
| 598 |
31.3. When starting GUI Vim, how do I specify the location of the GVIM |
| 599 |
window? |
| 600 |
31.4. How do I add a horizontal scrollbar in GVim? |
| 601 |
31.5. How do I make the scrollbar appear in the left side by default? |
| 602 |
31.6. How do I remove the Vim menubar? |
| 603 |
31.7. I am using GUI Vim. When I press the ALT key and a letter, the |
| 604 |
menu starting with that letter is selected. I don't want this |
| 605 |
behavior as I want to map the ALT-<key> combination. How do I do |
| 606 |
this? |
| 607 |
31.8. Is it possible to scroll the text by dragging the scrollbar so |
| 608 |
that the cursor stays in the original location? |
| 609 |
31.9. How do I get gvim to start browsing files in a particular |
| 610 |
directory when using the ":browse" command? |
| 611 |
31.10. For some questions, like when a file is changed outside of Vim, |
| 612 |
Vim displays a GUI dialog box. How do I replace this GUI dialog |
| 613 |
box with a console dialog box? |
| 614 |
31.11. I am trying to use GUI Vim as the editor for my xxx application. |
| 615 |
When the xxx application launches GUI Vim to edit a file, the |
| 616 |
control immediately returns to the xxx application. How do I |
| 617 |
start GUI Vim, so that the control returns to the xxx |
| 618 |
application only after I quit Vim? |
| 619 |
31.12. Why does the "Select Font" dialog doesn't show all the fonts |
| 620 |
installed in my system? |
| 621 |
31.13. How do I use the mouse in Vim command-line mode? |
| 622 |
31.14. When I use the middle mouse button to scroll text, it pastes the |
| 623 |
last copied text. How do I disable this behavior? |
| 624 |
31.15. How do I change the location and size of a GUI Vim window? |
| 625 |
|
| 626 |
SECTION 32 - VIM ON UNIX |
| 627 |
32.1. I am running Vim in a xterm. When I press the CTRL-S key, Vim |
| 628 |
freezes. What should I do now? |
| 629 |
32.2. I am seeing weird screen update problems in Vim. What can I do to |
| 630 |
solve this screen/display update problems? |
| 631 |
32.3. I am using the terminal/console version of Vim. In insertmode, |
| 632 |
When I press the backspace key, the character before the cursor |
| 633 |
is not erased. How do I configure Vim to do this? |
| 634 |
32.4. I am using Vim in a xterm. When I quit Vim, the screen contents |
| 635 |
are restored back to the original contents. How do I disable |
| 636 |
this? |
| 637 |
32.5. When I start Vim, it takes quite a few seconds to start. How do I |
| 638 |
minimize the startup time? |
| 639 |
32.6. How can I make the cursor in gvim in unix stop blinking? |
| 640 |
32.7. How do I change the menu font on GTK Vim? |
| 641 |
32.8. How do I prevent <Ctrl-Z> from suspending Vim? |
| 642 |
32.9. When I kill the xterm running Vim, the Vim process continues to |
| 643 |
run and takes up a lot of CPU (99%) time. Why is this happening? |
| 644 |
32.10. How do I get the Vim syntax highlighting to work in a Unix |
| 645 |
terminal? |
| 646 |
|
| 647 |
SECTION 33 - VIM ON MS-WINDOWS |
| 648 |
33.1. In MS-Windows, CTRL-V doesn't start the blockwise visual mode. |
| 649 |
What happened? |
| 650 |
33.2. When I press the CTRL-Y key, it acts like the CTRL-R key. How do |
| 651 |
I configure Vim to treat CTRL-Y as CTRL-Y? |
| 652 |
33.3. How do I start GUI Vim in a maximized window always? |
| 653 |
33.4. After doing some editing operations, Vim freezes. The cursor |
| 654 |
becomes an empty rectangle. I am not able enter any characters. |
| 655 |
What is happening? |
| 656 |
33.5. I am using Windows XP, the display speed of maximized GVim is |
| 657 |
very slow. What can I do to speed the display updates? |
| 658 |
33.6. What are the recommended settings for using Vim with cygwin? |
| 659 |
33.7. I am trying to use GNU diff with Vim diff mode. When I run the |
| 660 |
diff from command line, it works. When I try to use the diff with |
| 661 |
Vim it doesn't work. What should I do now? |
| 662 |
33.8. Is it possible to use Vim as an external editor for MS-Windows |
| 663 |
Outlook email client? |
| 664 |
33.9. I am using Vim to edit HTML files. How do I start internet |
| 665 |
explorer with the current file to preview the HTML file? |
| 666 |
33.10. I would like to use Vim with Microsoft Visual Studio. How do I |
| 667 |
do this? |
| 668 |
33.11. Where do I place the _vimrc and _gvimrc files? |
| 669 |
33.12. Everytime I save a file, Vim warns about the file being changed |
| 670 |
outside of Vim. Why? |
| 671 |
|
| 672 |
SECTION 34 - PRINTING |
| 673 |
34.1. How do I print a file along with line numbers for all the lines? |
| 674 |
34.2. How do I print a file with the Vim syntax highlighting colors? |
| 675 |
|
| 676 |
SECTION 35 - BUILDING VIM FROM SOURCE |
| 677 |
35.1. How do I build Vim from the sources on a Unix system? |
| 678 |
35.2. How do I install Vim in my home directory or a directory other |
| 679 |
than the default installation directory in Unix? |
| 680 |
35.3. How do I build Vim from the sources on a MS-Windows system? |
| 681 |
35.4. The Vim help, syntax, indent files are missing from my Vim |
| 682 |
installation. How do I install these files? |
| 683 |
35.5. I have built Vim from the source and installed the Vim package |
| 684 |
using "make install". Do I need to keep the Vim source directory? |
| 685 |
35.6. How do I determine the Vim features which are enabled at compile |
| 686 |
time? |
| 687 |
35.7. Can I build Vim without the GUI support? |
| 688 |
35.8. When building Vim on a Unix system, I am getting "undefined |
| 689 |
reference to term_set_winsize' error. How do I resolve this |
| 690 |
error? |
| 691 |
35.9. Vim configure keeps complaining about the lack of gtk-config |
| 692 |
while trying to use GTK 2.03. This is correct, since in GTK 2 |
| 693 |
they moved to using the generic pkg-config. I can get pkg-config |
| 694 |
to list the various includes and libs for gtk, but for some |
| 695 |
reason the configure script still isn't picking this up. |
| 696 |
|
| 697 |
SECTION 36 - VARIOUS |
| 698 |
36.1. How do I edit binary files with Vim? |
| 699 |
36.2. How do I disable the visual error flash and the error beep? |
| 700 |
36.3. How do I display the ascii value of a character displayed in a |
| 701 |
buffer? |
| 702 |
36.4. Can I use zero as a count for a Vim command? |
| 703 |
36.5. How do I disable the Vim welcome screen? |
| 704 |
36.6. How do I avoid the "hit enter to continue" prompt? |
| 705 |
36.7. How do I invoke Vim from command line to run a group of commands |
| 706 |
on a group of files? |
| 707 |
36.8. How do I use a normal mode command from insert mode without |
| 708 |
leaving the insert mode? |
| 709 |
36.9. How do I start Vim in insert mode? |
| 710 |
|
| 711 |
SECTION 37 - UNICODE |
| 712 |
37.1. Is it possible to create Unicode files using Vim? |
| 713 |
37.2. Which Vim settings are particularly important for editing Unicode |
| 714 |
files? |
| 715 |
37.3. What is the 'encoding' option? |
| 716 |
37.4. How does Vim name the various Unicode encodings? |
| 717 |
37.5. How does Vim specify the presence or absence of a byte-order |
| 718 |
mark? |
| 719 |
37.6. What is the 'fileencoding' option? |
| 720 |
37.7. What is the 'fileencodings' option? |
| 721 |
37.8. What is the 'termencoding' option? |
| 722 |
37.9. What is the 'bomb' option? |
| 723 |
37.10. Where can I find an example of a typical use of all these |
| 724 |
options? |
| 725 |
37.11. How can I insert Unicode characters into a file using Vim? |
| 726 |
37.12. How can I know which digraphs are defined and for which |
| 727 |
characters? |
| 728 |
|
| 729 |
|
| 730 |
============================================================================= |
| 731 |
|
| 732 |
SECTION 1 - GENERAL INFORMATION |
| 733 |
|
| 734 |
|
| 735 |
1.1. What is Vim? |
| 736 |
|
| 737 |
Vim stands for Vi IMproved. It used to be Vi IMitation, but there are so |
| 738 |
many improvements that a name change was appropriate. Vim is a text editor |
| 739 |
which includes almost all the commands from the Unix program "Vi" and a lot |
| 740 |
of new ones. All commands can be given with the keyboard. This has the |
| 741 |
advantage that you can keep your fingers on the keyboard and your eyes on |
| 742 |
the screen. For those who want it, there is mouse support and a GUI version |
| 743 |
with scrollbars and menus. |
| 744 |
|
| 745 |
Vim is an editor, not a word processor. A word processor is used mainly to |
| 746 |
do layout of text. This means positioning it, changing the way it appears |
| 747 |
on output. More often than not, the final document is meant to be printed |
| 748 |
or typeset or what have you, in order to present it in a pleasing manner to |
| 749 |
others. Examples of word processors are Microsoft Word, WordPerfect, |
| 750 |
FrameMaker, and AmiPro. |
| 751 |
|
| 752 |
An editor is simply for entering text. Any typesetting or laying out of the |
| 753 |
document is secondary. With an editor, one's main concern is entering text, |
| 754 |
not making the text look good. Examples of editors other than Vim and Vi |
| 755 |
are Emacs, Crisp, Brief, and xedit. And Notepad. |
| 756 |
|
| 757 |
For more information, read |
| 758 |
|
| 759 |
:help intro |
| 760 |
|
| 761 |
|
| 762 |
1.2. Who wrote Vim? |
| 763 |
|
| 764 |
Most of Vim was written by Bram Moolenar, with contributions from too many |
| 765 |
people to mention here. See ":h credits" for a complete list. |
| 766 |
|
| 767 |
Vim is based on Stevie, worked on by Tim Thompson, Tony Andrews and G.R. |
| 768 |
(Fred) Walter. |
| 769 |
|
| 770 |
For more information, read |
| 771 |
|
| 772 |
:help author |
| 773 |
|
| 774 |
|
| 775 |
1.3. Is Vim compatible with Vi? |
| 776 |
|
| 777 |
Yes. Vim is very much compatible with Vi. You can use the "-C" |
| 778 |
command-line flag to start Vim in Vi compatible mode: |
| 779 |
|
| 780 |
$ vim -C |
| 781 |
|
| 782 |
You can also use: |
| 783 |
|
| 784 |
$ vim -u NONE |
| 785 |
|
| 786 |
You can also set the 'compatible' option to enable Vi compatibility: |
| 787 |
|
| 788 |
:set compatible |
| 789 |
|
| 790 |
For more information, read |
| 791 |
|
| 792 |
:help -C |
| 793 |
:help 'compatible' |
| 794 |
:help compatible-default |
| 795 |
|
| 796 |
|
| 797 |
1.4. What are some of the improvements of Vim over Vi? |
| 798 |
|
| 799 |
A short summary of the improvements of Vim over vi is listed below. The |
| 800 |
list shows that Vim is a thoroughly modern and feature-packed editor. |
| 801 |
Standard features of modern editors are implemented, and there is an equal |
| 802 |
emphasis on general power-user features and features for programmers. |
| 803 |
|
| 804 |
Features to modernise Vi: |
| 805 |
|
| 806 |
Multi-level undo |
| 807 |
Allows you to set the number of times you can undo your changes in a |
| 808 |
file buffer. You can also redo an undone change. |
| 809 |
Multiple windows and buffers |
| 810 |
Each file can be displayed in its own window. You can move easily from |
| 811 |
one window to another. Each file opened during a Vim session also has |
| 812 |
an associated buffer and you can easily jump from one to the other. |
| 813 |
Flexible insert mode |
| 814 |
Vim allows you to use the arrow keys while in insert mode to move |
| 815 |
around in the file. No more hitting <Esc>, moving around, then hitting |
| 816 |
`i' or `a'. |
| 817 |
Macros |
| 818 |
Vim has a facility which allows you to record a sequence of typed |
| 819 |
characters and repeat them any number of times. |
| 820 |
Visual mode |
| 821 |
You can highlight sections of text and execute operations on this |
| 822 |
section of text only. |
| 823 |
Block operators |
| 824 |
Allow selection and highlighting of rectangular blocks of text in |
| 825 |
order do execute specific operations on them. |
| 826 |
Online help system |
| 827 |
You can easily find help on any aspect of using Vim. Help is displayed |
| 828 |
in its own window. |
| 829 |
Command-line editing and history |
| 830 |
History allows you to use the arrow keys to repeat or search for a |
| 831 |
command that has already been typed. Allows you to match the beginning |
| 832 |
of a command with the beginning of another similar command in the |
| 833 |
history buffer. You can also edit a command to correct typos or change |
| 834 |
a few values. |
| 835 |
Command line completion. |
| 836 |
Using the <Tab> key, you can complete commands, options, filenames, |
| 837 |
etc. as needed. |
| 838 |
Horizontal scrolling. |
| 839 |
Long lines can be scrolled horizontally (with or without the GUI). |
| 840 |
|
| 841 |
Advanced user features: |
| 842 |
|
| 843 |
Text formatting. |
| 844 |
With two keystrokes, you can format large sections of text, without |
| 845 |
the use of external programs. |
| 846 |
Word completion in Insert mode |
| 847 |
Vim can complete words while you are typing, by matching the current |
| 848 |
word with other similar words in the file. |
| 849 |
Jump tags |
| 850 |
Just like in an internet browser, you can jump back to previous parts |
| 851 |
of the text you were editing, and then forward again. Your brain is |
| 852 |
thus free to edit instead of navigate. |
| 853 |
Automatic commands |
| 854 |
Commands automatically executed when reading or writing a file, |
| 855 |
jumping to another buffer, etc. |
| 856 |
Viminfo |
| 857 |
Allows storing of the command line history, marks and registers in a |
| 858 |
file to be read on startup. Therefore, you can recall old search |
| 859 |
patterns, macros, etc., in a new Vim session. |
| 860 |
Mouse support |
| 861 |
The mouse is supported in an xterm and for MS-DOS. It can be used to |
| 862 |
position the cursor, select the visual area, paste a register, etc. |
| 863 |
Graphical User Interface (GUI) |
| 864 |
Just like any modern editor. Also, it's very easy to add your own |
| 865 |
menus. Of course, console vim is still supported, and very widely |
| 866 |
used. |
| 867 |
Scripting language |
| 868 |
Vim has a powerful scripting language so new commands can be created. |
| 869 |
You can also use Perl, Python, TCL and Ruby to achieve the same thing! |
| 870 |
Plugins |
| 871 |
Extra functionality implemented via vim commands (regular commands or |
| 872 |
the scripting language) that is automatically loaded on startup. |
| 873 |
Examples: file explorer, network editing. More are being developed |
| 874 |
and shared on VimOnline all the time. |
| 875 |
Syntax highlighting for many programming languages |
| 876 |
Syntax highlighting for hundreds of programming languages is |
| 877 |
supported. Support for others can be added. |
| 878 |
Extended regular expressions |
| 879 |
Vim supports extended regular expressions which are similar in |
| 880 |
functionality to that of perl regular expressions. |
| 881 |
|
| 882 |
Programming performance features: |
| 883 |
|
| 884 |
Edit-compile-edit speedup |
| 885 |
You can compile within Vim and automatically jump to the location of |
| 886 |
errors in the source code. |
| 887 |
Indenting for many programming languages |
| 888 |
C, C++, Java, Perl, XML and many other languages can be automatically |
| 889 |
indented by vim while you type. Support for others can be added. |
| 890 |
Searching for words in include files |
| 891 |
Vim allows you to search for a match of the word under the cursor in |
| 892 |
the current and included files. |
| 893 |
Advance text objects |
| 894 |
Instantly select, or delete, or copy, or indent, or format, or change |
| 895 |
case, or ... to all the text between ( and ), or { and }, or < and >, |
| 896 |
or [ and ]. Or a word, sentence, or paragraph. Very powerful. |
| 897 |
Folding |
| 898 |
Certain parts of the text can be "folded" away. The best example is |
| 899 |
the body of a function. You can get an overview of the code, and then |
| 900 |
open the fold of the function whose detail you need to see. |
| 901 |
ctags and cscope integration |
| 902 |
Using these two powerful programs, you can jump to a definition of a |
| 903 |
function from a calling instance of it, and use other tricks to |
| 904 |
navigate source code. |
| 905 |
|
| 906 |
For more information, read |
| 907 |
|
| 908 |
:help vi-differences |
| 909 |
|
| 910 |
|
| 911 |
1.5. Is Vim free? |
| 912 |
|
| 913 |
Vim is Charityware. There are no restrictions on using or copying Vim, but |
| 914 |
the author encourages you to make a donation to charity. A document |
| 915 |
explaining how to do so is included in the distribution. |
| 916 |
|
| 917 |
For more information, read |
| 918 |
|
| 919 |
:help copyright |
| 920 |
|
| 921 |
|
| 922 |
============================================================================= |
| 923 |
|
| 924 |
SECTION 2 - RESOURCES |
| 925 |
|
| 926 |
|
| 927 |
2.1. Where can I learn more about Vim? |
| 928 |
|
| 929 |
You can post your Vim questions to the [email protected] mailing list. You can |
| 930 |
post your Vim development related questions to the [email protected] mailing |
| 931 |
list. Vim does not have a newsgroup of its own. But the appropriate |
| 932 |
newsgroup to post to is comp.editors. |
| 933 |
|
| 934 |
"VimOnline" is a web page that serves as a de facto homepage for vim, |
| 935 |
although the main purpose of it is to gather tips and scripts from |
| 936 |
everywhere. Get involved! The URL is vim.sourceforge.net or vim.sf.net. |
| 937 |
|
| 938 |
Finally, read the Vi FAQ: |
| 939 |
|
| 940 |
http://www.faqs.org/faqs/editor-faq/vi/part1/index.html |
| 941 |
|
| 942 |
For more information, read |
| 943 |
|
| 944 |
:help mail-list |
| 945 |
:help internet |
| 946 |
|
| 947 |
|
| 948 |
2.2. Is there a mailing list available? |
| 949 |
|
| 950 |
There are several: |
| 951 |
|
| 952 |
NAME DESCRIPTION |
| 953 |
|
| 954 |
vim-announce Announcements of new releases |
| 955 |
vim General discussion |
| 956 |
vim-dev Patches, bug reports, development discussions |
| 957 |
vim-mac Macintosh discussion |
| 958 |
vim-fr General discussion in French |
| 959 |
vim-multibyte Multibyte development issues |
| 960 |
vim-vms Development on VMS |
| 961 |
|
| 962 |
Of these, only vim and vim-dev are of general interest. vim-announce is |
| 963 |
read-only to most people, and its messages are sent to the other lists as |
| 964 |
well. The remaining four are very low volume. |
| 965 |
|
| 966 |
To subscribe: send an email to <NAME>[email protected] |
| 967 |
To unsubscribe: send an email to <NAME>[email protected] |
| 968 |
To get help: send an email to <NAME>[email protected] |
| 969 |
|
| 970 |
|
| 971 |
2.3. Is there an archive available for the Vim mailing lists? |
| 972 |
|
| 973 |
Yes. Visit http://www.yahoogroups.com/list/<name>, where name is one of: |
| 974 |
vimannounce, vim, vimdev, vim-fr, vim-mac, vim-multibyte, vim-vms. |
| 975 |
|
| 976 |
Alternatively, visit www.gmane.org to find out about GMANE, which allows |
| 977 |
you to access the mailing lists as though they were newsgroups. This |
| 978 |
offers some convenience to those who wish to browse the history or casually |
| 979 |
observe the current threads. |
| 980 |
|
| 981 |
|
| 982 |
2.4. Where can I get the Vim user manual in HTML/PDF/PS format? |
| 983 |
|
| 984 |
You can download the HTML/PDF/PS format of the Vim user manual from: |
| 985 |
|
| 986 |
http://vimdoc.sourceforge.net/ |
| 987 |
|
| 988 |
|
| 989 |
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a |
| 990 |
problem with my setup or with Vim? |
| 991 |
|
| 992 |
First, you have to determine that the problem is not with your .vimrc or |
| 993 |
.gvimrc or system vimrc or your personal plugin files or in any of your |
| 994 |
setup files. To do this, use |
| 995 |
|
| 996 |
$ vim -N -u NONE -U NONE |
| 997 |
|
| 998 |
This will start Vim in 'nocompatible" mode and will not source your |
| 999 |
personal .vimrc and .gvimrc files. It will also not load your personal |
| 1000 |
plugins. In this invocation of Vim, try to reproduce your problem. If you |
| 1001 |
are not able to reproduce the problem, then the problem is related to some |
| 1002 |
setting in one of your local setup files or plugins. To locate the problem |
| 1003 |
in your setup files, you have to use trial and error and try commenting out |
| 1004 |
the lines in your setup files one by one. You can also use the -V command |
| 1005 |
line argument to Vim to get more debug information and analyze the problem: |
| 1006 |
|
| 1007 |
$ vim -V2 |
| 1008 |
|
| 1009 |
You can increase the value passed to the -V argument to get more debug |
| 1010 |
information. |
| 1011 |
|
| 1012 |
For more information, read |
| 1013 |
|
| 1014 |
:help -u |
| 1015 |
:help -U |
| 1016 |
:help -N |
| 1017 |
:help -V |
| 1018 |
:help 'verbose' |
| 1019 |
:help :verbose |
| 1020 |
:help set-verbose |
| 1021 |
|
| 1022 |
|
| 1023 |
2.6. Where can I report bugs? |
| 1024 |
|
| 1025 |
First collect the required information using the following command: |
| 1026 |
|
| 1027 |
:source $VIMRUNTIME/bugreport.vim |
| 1028 |
|
| 1029 |
Now send the resulting text from the above command to the [email protected] |
| 1030 |
e-mail address. |
| 1031 |
|
| 1032 |
The Vim Development mailing list (see above) is a good place to discuss |
| 1033 |
general bugs. If the bug you find is with syntax highlighting or some |
| 1034 |
other "added feature" (i.e. not directly programmed into vim), attempt to |
| 1035 |
inform the maintainer of that feature. |
| 1036 |
|
| 1037 |
For more information, read |
| 1038 |
|
| 1039 |
:help bug-reports |
| 1040 |
|
| 1041 |
|
| 1042 |
2.7. Where can the FAQ be found? |
| 1043 |
|
| 1044 |
The FAQ can be found at VimOnline (vim.sf.net). Other places will be |
| 1045 |
decided in the future. |
| 1046 |
|
| 1047 |
|
| 1048 |
2.8. What if I don't find an answer in this FAQ? |
| 1049 |
|
| 1050 |
This FAQ covers mainly Vim-specific questions. You may find more |
| 1051 |
information suitable for most Vi clones by reading the Vi FAQ. It is posted |
| 1052 |
regularly on comp.editors. You can also find a copy at |
| 1053 |
|
| 1054 |
http://www.faqs.org/faqs/editor-faq/vi/part1/index.html |
| 1055 |
|
| 1056 |
Also, since Vim has gathered so many features in the last few years, |
| 1057 |
successfully documenting the frequently asked questions here is a |
| 1058 |
near-impossible task. To make it possible, please email the maintainer if |
| 1059 |
you have a good question. A good question is one that you've tried to |
| 1060 |
answer yourself (remember, Vim has great documentation) but struggled. |
| 1061 |
|
| 1062 |
|
| 1063 |
2.9. I have a patch for implementing a Vim feature. Where can I send this |
| 1064 |
patch? |
| 1065 |
|
| 1066 |
You can send your patches to the Vim developer mailing list |
| 1067 |
[email protected]. |
| 1068 |
|
| 1069 |
For more information, read |
| 1070 |
|
| 1071 |
:help vim-dev |
| 1072 |
|
| 1073 |
|
| 1074 |
2.10. I have a Vim tip or developed a new Vim |
| 1075 |
syntax/indent/filetype/compiler plugin or developed a new script or a |
| 1076 |
colorscheme. Is there a public website where I can upload this? |
| 1077 |
|
| 1078 |
Yes. You can use the Vim Online website to upload your plugins/scripts, |
| 1079 |
colorschemes, tips, etc. The site is at http://vim.sourceforge.net |
| 1080 |
|
| 1081 |
|
| 1082 |
============================================================================= |
| 1083 |
|
| 1084 |
SECTION 3 - AVAILABILITY |
| 1085 |
|
| 1086 |
|
| 1087 |
3.1. What is the latest version of Vim? |
| 1088 |
|
| 1089 |
The latest version of Vim is 6.3 released on 8th June 2004. |
| 1090 |
|
| 1091 |
The release-history of different versions of Vim is below: |
| 1092 |
|
| 1093 |
Version 6.3 8th June 2004 |
| 1094 |
Version 6.2 1st June 2003 |
| 1095 |
Version 6.1 24th March 2002 |
| 1096 |
Version 6.0 27th September, 2001 |
| 1097 |
Version 5.8 31st May, 2001 |
| 1098 |
Version 5.7 24th June, 2000 |
| 1099 |
Version 5.6 16th January, 2000 |
| 1100 |
Version 5.5 21st September, 1999 |
| 1101 |
Version 5.4 26th July, 1999 |
| 1102 |
Version 5.3 31st August, 1998 |
| 1103 |
Version 5.2 24th August, 1998 |
| 1104 |
Version 5.1 7th April, 1998 |
| 1105 |
Version 5.0 19th February, 1998 |
| 1106 |
Version 4.6 13th March,1997 |
| 1107 |
Version 4.5 17th October, 1996 |
| 1108 |
Version 4.2 5th July,1996 |
| 1109 |
Version 4.0 21st May, 1996 |
| 1110 |
Version 3.0 16th August, 1994 |
| 1111 |
Version 2.0 21st December, 1993 |
| 1112 |
Version 1.27 23rd April, 1993 |
| 1113 |
Version 1.17 21st April, 1992 |
| 1114 |
|
| 1115 |
|
| 1116 |
3.2. Where can I find the latest version of Vim? |
| 1117 |
|
| 1118 |
You can download the sources for the latest version of Vim from the |
| 1119 |
VimOnline website. The URL for this site is |
| 1120 |
http://vim.sourceforge.net/download.php. |
| 1121 |
|
| 1122 |
|
| 1123 |
3.3. What platforms does it run on? |
| 1124 |
|
| 1125 |
All Unix platforms. |
| 1126 |
All Windows platforms. |
| 1127 |
Amiga, Atari, BeOS, DOS, Macintosh, MachTen, OS/2, RiscOS, VMS. |
| 1128 |
|
| 1129 |
|
| 1130 |
3.4. Where can I download the latest version of the Vim runtime files? |
| 1131 |
|
| 1132 |
You can download the latest version of the Vim runtime files (syntax files, |
| 1133 |
filetype plugins, compiler files, color schemes, documentation, indentation |
| 1134 |
files and keymaps) from the Vim ftp site from the |
| 1135 |
ftp://ftp.vim.org/pub/vim/runtime directory. |
| 1136 |
|
| 1137 |
|
| 1138 |
============================================================================= |
| 1139 |
|
| 1140 |
SECTION 4 - HELP |
| 1141 |
|
| 1142 |
|
| 1143 |
4.1. How do I use the help files? |
| 1144 |
|
| 1145 |
Help can be found for all functions of Vim. In order to use it, use the |
| 1146 |
":help" command. This will bring you to the main help page. On that first |
| 1147 |
page, you will find explanations on how to move around. Basically, you move |
| 1148 |
around in the help pages the same way you would in a read-only document. |
| 1149 |
You can jump to specific subjects by using tags. This can be done in two |
| 1150 |
ways: |
| 1151 |
|
| 1152 |
* Use the "<Ctrl-]>" command while standing on the name of a command or |
| 1153 |
option. This only works when the tag is a keyword. "<Ctrl-LeftMouse>" |
| 1154 |
and "g<LeftMouse>" work just like "<Ctrl-]>". |
| 1155 |
* use the ":tag <subject>" command. This works with all characters. |
| 1156 |
|
| 1157 |
Use "<Ctrl-T>" to jump back to previous positions in the help files. Use |
| 1158 |
":q" to close the help window. |
| 1159 |
|
| 1160 |
If you want to jump to a specific subject on the help pages, use ":help |
| 1161 |
{subject}". If you don't know what to look for, try ":help index" to get a |
| 1162 |
list of all available subjects. Use the standard search keys to locate the |
| 1163 |
information you want. You can abbreviate the ":help" command as ":h". |
| 1164 |
|
| 1165 |
For more information, read |
| 1166 |
|
| 1167 |
:help online-help |
| 1168 |
|
| 1169 |
|
| 1170 |
4.2. How do I search for a keyword in the Vim help files? |
| 1171 |
|
| 1172 |
You can press the CTRL-D key after typing the help keyword to get a list of |
| 1173 |
all the help keywords containing the supplied pattern. You can also use the |
| 1174 |
meta characters like *, \+, etc to specify the help search pattern: |
| 1175 |
|
| 1176 |
:help init<C-D> |
| 1177 |
:help str*()<C-D> |
| 1178 |
:help '*indent<C-D> |
| 1179 |
|
| 1180 |
You can press the Tab key after typing a partial help keyword to expand to |
| 1181 |
the matching keyword. You can continue to press the Tab key to see other |
| 1182 |
keyword matches. |
| 1183 |
|
| 1184 |
From the help window, you can use the ":tag" command to search for |
| 1185 |
keywords. For example, |
| 1186 |
|
| 1187 |
:tselect /window |
| 1188 |
|
| 1189 |
This command will list all the help keywords containing the text "window". |
| 1190 |
You can select one from the list and jump to it. |
| 1191 |
|
| 1192 |
You can use the ":helpgrep" command to search for the given text in all the |
| 1193 |
help files. The quickfix window will be opened with all the matching lines. |
| 1194 |
|
| 1195 |
For more information, read |
| 1196 |
|
| 1197 |
:help c_CTRL-D |
| 1198 |
:help c_<Tab> |
| 1199 |
:help :tselect |
| 1200 |
:help :help |
| 1201 |
:help :helpgrep |
| 1202 |
|
| 1203 |
|
| 1204 |
4.3. I am getting an error message E123, what did I do wrong? |
| 1205 |
|
| 1206 |
You can get more information about the error and the error message using: |
| 1207 |
|
| 1208 |
:help E123 |
| 1209 |
|
| 1210 |
For more information, read |
| 1211 |
|
| 1212 |
:help error-messages |
| 1213 |
|
| 1214 |
|
| 1215 |
4.4. Where can I read about the various modes in Vim? |
| 1216 |
|
| 1217 |
You can get information about the different modes in Vim by reading |
| 1218 |
|
| 1219 |
:help vim-modes |
| 1220 |
|
| 1221 |
|
| 1222 |
4.5. How do I generate the Vim help tags file after adding a new Vim help |
| 1223 |
file? |
| 1224 |
|
| 1225 |
You can use the ":helptags" command to regenerate the Vim help tag file. |
| 1226 |
For example: |
| 1227 |
|
| 1228 |
:cd $VIMRUNTIME/doc |
| 1229 |
:helptags . |
| 1230 |
|
| 1231 |
For more information, read |
| 1232 |
|
| 1233 |
:help :helptags |
| 1234 |
:help add-local-help |
| 1235 |
|
| 1236 |
|
| 1237 |
4.6. Can I use compressed versions of the help files? |
| 1238 |
|
| 1239 |
Yes. You can compress the help files and still be able to view them with |
| 1240 |
Vim. This makes accessing the help files a bit slower and requires the |
| 1241 |
"gzip" utility. Follow these steps to compress and use the Vim help files: |
| 1242 |
|
| 1243 |
- Compress all the help files using "gzip doc/*.txt". |
| 1244 |
|
| 1245 |
- Edit the "doc/tags" file and change the ".txt" to ".txt.gz" using |
| 1246 |
:%s=\(\t.*\.txt\)\t=\1.gz\t= |
| 1247 |
|
| 1248 |
- Add the following line to your vimrc: |
| 1249 |
set helpfile={dirname}/help.txt.gz |
| 1250 |
|
| 1251 |
Where {dirname} is the directory where the help files are. The gzip.vim |
| 1252 |
plugin supplied with the standard Vim distribution will take care of |
| 1253 |
decompressing the files. You must make sure that $VIMRUNTIME is set to |
| 1254 |
where the other Vim files are, when they are not in the same location as |
| 1255 |
the compressed "doc" directory. |
| 1256 |
|
| 1257 |
For more information, read |
| 1258 |
|
| 1259 |
:help gzip-helpfile |
| 1260 |
:help 'helpfile' |
| 1261 |
:help gzip |
| 1262 |
:help $VIMRUNTIME |
| 1263 |
|
| 1264 |
|
| 1265 |
============================================================================= |
| 1266 |
|
| 1267 |
SECTION 5 - EDITING A FILE |
| 1268 |
|
| 1269 |
|
| 1270 |
5.1. How do I load a file in Vim for editing? |
| 1271 |
|
| 1272 |
There are several ways to load a file for editing. The simplest is to |
| 1273 |
use the ":e" (:edit) command: |
| 1274 |
|
| 1275 |
:e <filename> |
| 1276 |
|
| 1277 |
You can also use the ":n" (:next) command to load files into Vim: |
| 1278 |
|
| 1279 |
:n <filename(s)> |
| 1280 |
|
| 1281 |
You can also use the ":args" command to load files into Vim: |
| 1282 |
|
| 1283 |
:args <filename(s)> |
| 1284 |
|
| 1285 |
For more information, read |
| 1286 |
|
| 1287 |
:help usr_07 |
| 1288 |
:help edit-files |
| 1289 |
:help :edit |
| 1290 |
:help :next_f |
| 1291 |
:help :args_f |
| 1292 |
|
| 1293 |
|
| 1294 |
5.2. How do I save the current file in another name (save as) and edit a |
| 1295 |
new file? |
| 1296 |
|
| 1297 |
You can use the ":saveas" command to save the current file in another name: |
| 1298 |
|
| 1299 |
:saveas <newfilename> |
| 1300 |
|
| 1301 |
Alternatively, you can also use the following commands: |
| 1302 |
|
| 1303 |
:w <newfilename> |
| 1304 |
:edit # |
| 1305 |
|
| 1306 |
You can also use the ":file" command, followed by ":w" command: |
| 1307 |
|
| 1308 |
:file <newfilename> |
| 1309 |
:w |
| 1310 |
|
| 1311 |
For more information, read |
| 1312 |
|
| 1313 |
:help 07.7 |
| 1314 |
:help :saveas |
| 1315 |
:help :file_f |
| 1316 |
:help :w |
| 1317 |
|
| 1318 |
|
| 1319 |
5.3. How do I change the current directory to the directory of the current |
| 1320 |
file? |
| 1321 |
|
| 1322 |
You can use the following command to change the current directory to the |
| 1323 |
directory of the current file: |
| 1324 |
|
| 1325 |
:cd %:p:h |
| 1326 |
|
| 1327 |
To automatically change the current directory to the directory of the |
| 1328 |
current file, use the following autocmd: |
| 1329 |
|
| 1330 |
:autocmd BufEnter * cd %:p:h |
| 1331 |
|
| 1332 |
For more information, read |
| 1333 |
|
| 1334 |
:help :cd |
| 1335 |
:help :lcd |
| 1336 |
:help filename-modifiers |
| 1337 |
:help autocommand |
| 1338 |
|
| 1339 |
|
| 1340 |
5.4. How do I write a file without the line feed (EOL) at the end of the |
| 1341 |
file? |
| 1342 |
|
| 1343 |
You can turn off the 'eol' option and turn on the 'binary' option to write |
| 1344 |
a file without the EOL at the end of the file: |
| 1345 |
|
| 1346 |
:set binary |
| 1347 |
:set noeol |
| 1348 |
:w |
| 1349 |
|
| 1350 |
For more information, read |
| 1351 |
|
| 1352 |
:help 'endofline' |
| 1353 |
:help 'binary' |
| 1354 |
:help 23.4 |
| 1355 |
|
| 1356 |
|
| 1357 |
5.5. How do I configure Vim to open a file at the last edited location? |
| 1358 |
|
| 1359 |
Vim stores the cursor position of the last edited location for each buffer |
| 1360 |
in the '"' register. You can use the following autocmd in your .vimrc or |
| 1361 |
.gvimrc file to open a file at the last edited location: |
| 1362 |
|
| 1363 |
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | |
| 1364 |
\ exe "normal g'\"" | endif |
| 1365 |
|
| 1366 |
For more information, read |
| 1367 |
|
| 1368 |
:help '" |
| 1369 |
:help last-position-jump |
| 1370 |
|
| 1371 |
|
| 1372 |
5.6. When editing a file in Vim, which is being changed by an external |
| 1373 |
application, Vim opens a warning window (like the confirm dialog) each |
| 1374 |
time a change is detected. How do I disable this warning? |
| 1375 |
|
| 1376 |
You can set the Vim 'autoread' option to automatically read the file again |
| 1377 |
when it is changed outside of Vim: |
| 1378 |
|
| 1379 |
:set autoread |
| 1380 |
|
| 1381 |
You can also use the following autocommand: |
| 1382 |
|
| 1383 |
autocmd FileChangedShell * |
| 1384 |
\ echohl WarningMsg | |
| 1385 |
\ echo "File has been changed outside of vim." | |
| 1386 |
\ echohl None |
| 1387 |
|
| 1388 |
For more information, read |
| 1389 |
|
| 1390 |
:help 'autoread' |
| 1391 |
:help FileChangedShell |
| 1392 |
:help timestamp |
| 1393 |
:help :checktime |
| 1394 |
|
| 1395 |
|
| 1396 |
5.7. How do I edit a file whose name is under the cursor? |
| 1397 |
|
| 1398 |
You can use the gf command to edit a file whose name is under the cursor. |
| 1399 |
You can use the CTRL-W f command to edit the file in a new window. |
| 1400 |
|
| 1401 |
For more information, read |
| 1402 |
|
| 1403 |
:help gf |
| 1404 |
:help CTRL-W_f |
| 1405 |
:help 'isfname' |
| 1406 |
:help 'path' |
| 1407 |
:help 'suffixesadd' |
| 1408 |
:help 'includeexpr' |
| 1409 |
|
| 1410 |
|
| 1411 |
5.8. How do I reload/re-edit the current file? |
| 1412 |
|
| 1413 |
You can use the ":edit" command, without specifying a file name, to reload |
| 1414 |
the current file. If you have made modifications to the file, you can use |
| 1415 |
":edit!" to force the reload of the current file (you will lose your |
| 1416 |
modifications). |
| 1417 |
|
| 1418 |
For more information, read |
| 1419 |
|
| 1420 |
:help :edit |
| 1421 |
:help :edit! |
| 1422 |
:help 'confirm' |
| 1423 |
|
| 1424 |
|
| 1425 |
5.9. How do I autosave a file periodically? |
| 1426 |
|
| 1427 |
Vim doesn't support auto-saving a file periodically. |
| 1428 |
|
| 1429 |
For more information, read |
| 1430 |
|
| 1431 |
:help 'updatetime' |
| 1432 |
:help CursorHold |
| 1433 |
:help swap-file |
| 1434 |
|
| 1435 |
|
| 1436 |
5.10. How do I open a file in read-only mode? |
| 1437 |
|
| 1438 |
You can open a file in read-only mode using the ":view" command: |
| 1439 |
|
| 1440 |
:view <filename> |
| 1441 |
|
| 1442 |
This command sets the 'readonly' option for the opened buffer. You can also |
| 1443 |
use the "-R" command-line option to open a file in read-only mode: |
| 1444 |
|
| 1445 |
$ vim -R <filename> |
| 1446 |
|
| 1447 |
You can also use the symbolic link executable "view" to open a file in |
| 1448 |
read-only mode from the command-line: |
| 1449 |
|
| 1450 |
$ view <filename> |
| 1451 |
|
| 1452 |
For more information, read |
| 1453 |
|
| 1454 |
:help 07.6 |
| 1455 |
:help 'readonly' |
| 1456 |
:help 'modifiable' |
| 1457 |
:help :view |
| 1458 |
:help :sview |
| 1459 |
:help view |
| 1460 |
:help -R |
| 1461 |
:help -M |
| 1462 |
|
| 1463 |
|
| 1464 |
5.11. How do I open a file for editing without saving the modifications to |
| 1465 |
the current file? |
| 1466 |
|
| 1467 |
You can open a file for editing without saving the modifications to the |
| 1468 |
current file and without losing the changes using one of the following |
| 1469 |
methods: |
| 1470 |
|
| 1471 |
:split <new_filename> |
| 1472 |
:new <new_filename> |
| 1473 |
|
| 1474 |
You can also set the 'hidden' option and edit a new file: |
| 1475 |
|
| 1476 |
:set hidden |
| 1477 |
: e <new_filename> |
| 1478 |
|
| 1479 |
If you want to discard the changes made to the current file and load |
| 1480 |
another file for editing, then you can use the following command: |
| 1481 |
|
| 1482 |
:e! <new_filename> |
| 1483 |
|
| 1484 |
For more information, read |
| 1485 |
|
| 1486 |
:help :edit!_f |
| 1487 |
:help 'hidden' |
| 1488 |
:help :split |
| 1489 |
:help :new |
| 1490 |
|
| 1491 |
|
| 1492 |
============================================================================= |
| 1493 |
|
| 1494 |
SECTION 6 - EDITING MULTIPLE FILES |
| 1495 |
|
| 1496 |
|
| 1497 |
6.1. How do I open multiple files at once from within Vim? |
| 1498 |
|
| 1499 |
There are several ways to open multiple files at once from within Vim. You |
| 1500 |
can use the ":next" command to specify a group of files: |
| 1501 |
|
| 1502 |
:next f1.txt f2.txt |
| 1503 |
:next *.c |
| 1504 |
|
| 1505 |
You can use the :args command to specify a group of files as arguments: |
| 1506 |
|
| 1507 |
:args f1.txt f2.txt |
| 1508 |
:args *.c |
| 1509 |
|
| 1510 |
After loading the files, you can use the ":next" and ":prev" command to |
| 1511 |
switch between the files. |
| 1512 |
|
| 1513 |
For more information, read |
| 1514 |
|
| 1515 |
:help 07.2 |
| 1516 |
:help :next |
| 1517 |
:help :args_f |
| 1518 |
:help argument-list |
| 1519 |
|
| 1520 |
|
| 1521 |
6.2. How do I switch between multiple files/buffers in Vim? |
| 1522 |
|
| 1523 |
There are several ways to switch between multiple files. You can use the |
| 1524 |
":buffer" command to switch between multiple files. For example, |
| 1525 |
|
| 1526 |
:buffer file1 |
| 1527 |
:buffer file2 |
| 1528 |
|
| 1529 |
You can also use the CTRL-^ key to switch between buffers. By specifying a |
| 1530 |
count before pressing the key, you can edit the buffer with that number. |
| 1531 |
Without the count, you can edit the alternate buffer by pressing CTRL-^ |
| 1532 |
|
| 1533 |
You can also use the ":e #" command to edit a particular buffer: |
| 1534 |
|
| 1535 |
:e #5 |
| 1536 |
|
| 1537 |
For more information, read |
| 1538 |
|
| 1539 |
:help edit-files |
| 1540 |
:help :buffer |
| 1541 |
:help CTRL-^ |
| 1542 |
:help alternate-file |
| 1543 |
:help 22.4 |
| 1544 |
:help 07.3 |
| 1545 |
|
| 1546 |
|
| 1547 |
6.3. How do I open several files in Vim, with each file in a separate |
| 1548 |
window? |
| 1549 |
|
| 1550 |
You can use the -o and -O Vim command line arguments to open multiple files |
| 1551 |
in separate horizontally or vertically split Vim windows. For example: |
| 1552 |
|
| 1553 |
$ vim -o3 f1.txt f2.txt f3.txt |
| 1554 |
|
| 1555 |
The above command will open the files f1.txt, f2.txt and f3.txt in three |
| 1556 |
separate horizontally split Vim windows. |
| 1557 |
|
| 1558 |
$ vim -O3 f1.txt f2.txt f3.txt |
| 1559 |
|
| 1560 |
The above command will open the files f1.txt, f2.txt and f3.txt in three |
| 1561 |
separate vertically split Vim windows. |
| 1562 |
|
| 1563 |
For more information, read |
| 1564 |
|
| 1565 |
:help -o |
| 1566 |
:help -O |
| 1567 |
:help startup-options |
| 1568 |
|
| 1569 |
|
| 1570 |
6.4. How do I configure Vim to autoload several files at once similar to |
| 1571 |
"work-sets" or "projects"? |
| 1572 |
|
| 1573 |
You can use the ":mksession" and the ":mkview" commands to autoload several |
| 1574 |
files in Vim. |
| 1575 |
|
| 1576 |
The ":mksession" command creates a Vim script that restores the current |
| 1577 |
editing session. You can use the ":source" command to source the file |
| 1578 |
produced by the mksession command. |
| 1579 |
|
| 1580 |
The ":mkview" command creates a Vim script that restores the contents of |
| 1581 |
the current window. You can use the ":loadview" command to load the view |
| 1582 |
for the current file. |
| 1583 |
|
| 1584 |
For more information, read |
| 1585 |
|
| 1586 |
:help 21.4 |
| 1587 |
:help 21.5 |
| 1588 |
:help views-sessions |
| 1589 |
:help 'sessionoptions' |
| 1590 |
:help :mksession |
| 1591 |
:help :source |
| 1592 |
:help v:this_session |
| 1593 |
:help :mkview |
| 1594 |
:help :loadview |
| 1595 |
:help 'viewdir' |
| 1596 |
:help buffers |
| 1597 |
|
| 1598 |
|
| 1599 |
6.5. Is it possible to open multiple top level windows in a single instance |
| 1600 |
of Vim similar to Nedit or emacs? |
| 1601 |
|
| 1602 |
No. It is currently not possible to open multiple top-level windows in a |
| 1603 |
single instance of Vim. This feature is in the todo list. |
| 1604 |
|
| 1605 |
|
| 1606 |
6.6. How do I browse/explore directories from within Vim? |
| 1607 |
|
| 1608 |
You can use the explorer.vim plugin, supplied with the standard Vim |
| 1609 |
installation, to browse/explore directories from within Vim. You can start |
| 1610 |
the file explorer using one of the following commands: |
| 1611 |
|
| 1612 |
:e <directory> |
| 1613 |
:Explore |
| 1614 |
:SExplore |
| 1615 |
|
| 1616 |
From the file explorer, you can browse through directories, rename, delete |
| 1617 |
and edit files. |
| 1618 |
|
| 1619 |
For more information, read |
| 1620 |
|
| 1621 |
:help file-explorer |
| 1622 |
:help 22.1 |
| 1623 |
|
| 1624 |
|
| 1625 |
6.7. How do I edit files over a network using ftp/scp/rcp/http? |
| 1626 |
|
| 1627 |
You can use the netrw.vim plugin, supplied with the standard Vim package, |
| 1628 |
to edit files over a network using ftp/scp/rcp/http. Using this plugin, Vim |
| 1629 |
will transparently load and save the files over ftp/scp/rcp/http. For |
| 1630 |
example, to edit a file over ftp, you can use the following command: |
| 1631 |
|
| 1632 |
$ vim ftp://machine/path |
| 1633 |
|
| 1634 |
For more information, read |
| 1635 |
|
| 1636 |
:help netrw.vim |
| 1637 |
|
| 1638 |
|
| 1639 |
============================================================================= |
| 1640 |
|
| 1641 |
SECTION 7 - BACKUP |
| 1642 |
|
| 1643 |
|
| 1644 |
7.1. When I edit and save files, Vim creates a file with the same name as |
| 1645 |
the original file and a "~" character at the end. How do I stop Vim |
| 1646 |
from creating this file? (or) How do I disable the Vim backup file |
| 1647 |
feature? |
| 1648 |
|
| 1649 |
You have set the 'backup' option, so Vim creates a backup file when saving |
| 1650 |
the original file. You can stop Vim from creating the backup file, by |
| 1651 |
clearing the option: |
| 1652 |
|
| 1653 |
:set nobackup |
| 1654 |
|
| 1655 |
Note that, by default this option is turned off. You have explicitly |
| 1656 |
enabled the 'backup' option in one of the initialization files. You may |
| 1657 |
also have to turn off the 'writebackup' option: |
| 1658 |
|
| 1659 |
:set nowritebackup |
| 1660 |
|
| 1661 |
For more information, read |
| 1662 |
|
| 1663 |
:help 07.4 |
| 1664 |
:help backup-table |
| 1665 |
:help 'backup' |
| 1666 |
:help 'writebackup' |
| 1667 |
:help 'backupskip' |
| 1668 |
:help 'backupdir' |
| 1669 |
:help 'backupext' |
| 1670 |
:help 'backupcopy' |
| 1671 |
:help backup |
| 1672 |
|
| 1673 |
|
| 1674 |
7.2. How do I configure Vim to store all the backup files in a particular |
| 1675 |
directory? |
| 1676 |
|
| 1677 |
You can configure Vim to store all the backup files in a particular |
| 1678 |
directory using the 'backupdir' option. For example, to store all the |
| 1679 |
backup files in the ~/backup directory, you can use the following command: |
| 1680 |
|
| 1681 |
:set backupdir=~/backup |
| 1682 |
|
| 1683 |
For more information, read |
| 1684 |
|
| 1685 |
:help 07.4 |
| 1686 |
:help 'backupdir' |
| 1687 |
:help backup |
| 1688 |
|
| 1689 |
|
| 1690 |
7.3. When I save a file with Vim, the file permissions are changed. |
| 1691 |
How do I configure Vim to save a file without changing the file |
| 1692 |
permissions? |
| 1693 |
|
| 1694 |
This may happen, if the 'backupcopy' option is set to 'no' or 'auto'. Note |
| 1695 |
that the default value for this option is set in such a way that this will |
| 1696 |
correctly work in most of the cases. If the default doesn't work for you, |
| 1697 |
try setting the 'backupcopy' option to 'yes' to keep the file permission |
| 1698 |
when saving a file: |
| 1699 |
|
| 1700 |
:set backupcopy=yes |
| 1701 |
|
| 1702 |
This applies, only if you have configured Vim to make a backup whenever |
| 1703 |
overwriting a file. By default, Vim will not backup files. |
| 1704 |
|
| 1705 |
For more information, read |
| 1706 |
|
| 1707 |
:help 'backupcopy' |
| 1708 |
:help backup |
| 1709 |
:help 'backup' |
| 1710 |
:help 'writebackup' |
| 1711 |
|
| 1712 |
|
| 1713 |
============================================================================= |
| 1714 |
|
| 1715 |
SECTION 8 - BUFFERS |
| 1716 |
|
| 1717 |
|
| 1718 |
8.1. I have made some modifications to a buffer. How do I edit another |
| 1719 |
buffer without saving the modified buffer and also without losing the |
| 1720 |
modifications? |
| 1721 |
|
| 1722 |
You can set the 'hidden' option to edit a file without losing modifications |
| 1723 |
to the current file: |
| 1724 |
|
| 1725 |
:set hidden |
| 1726 |
|
| 1727 |
By setting the 'hidden' option, you can also save the modification history |
| 1728 |
(undo-history) for the buffer. Otherwise, as you switch between files, the |
| 1729 |
undo-history will be lost. |
| 1730 |
|
| 1731 |
For more information, read |
| 1732 |
|
| 1733 |
:help 'hidden' |
| 1734 |
:help hidden-quit |
| 1735 |
:help :hide |
| 1736 |
|
| 1737 |
|
| 1738 |
8.2. How do I configure Vim to auto-save a modified buffer when switching |
| 1739 |
to another buffer? |
| 1740 |
|
| 1741 |
You can set the 'autowrite' option to auto-save a modified buffer when |
| 1742 |
switching to another buffer: |
| 1743 |
|
| 1744 |
:set autowrite |
| 1745 |
|
| 1746 |
For more information, read |
| 1747 |
|
| 1748 |
:help 'autowrite' |
| 1749 |
:help 'autowriteall' |
| 1750 |
:help 'hidden' |
| 1751 |
|
| 1752 |
|
| 1753 |
8.3. How do I replace the buffer in the current window with a blank buffer? |
| 1754 |
|
| 1755 |
You can use the ":enew" command to load an empty buffer in place of the |
| 1756 |
buffer in the current window. |
| 1757 |
|
| 1758 |
For more information, read |
| 1759 |
|
| 1760 |
:help :enew |
| 1761 |
|
| 1762 |
|
| 1763 |
8.4. Is there a keyboard shortcut to load a buffer by the buffer number? |
| 1764 |
|
| 1765 |
You can use the CTRL-^ command to load a buffer by specifying the buffer |
| 1766 |
number. For example, to load buffer number 5, you have to use the 5 CTRL-^ |
| 1767 |
command. |
| 1768 |
|
| 1769 |
For more information, read |
| 1770 |
|
| 1771 |
:help CTRL-^ |
| 1772 |
|
| 1773 |
|
| 1774 |
8.5. How do I open all the current buffers in separate windows? |
| 1775 |
|
| 1776 |
You can use the ":ball" or ":sball" commands to open all the buffers |
| 1777 |
in the buffer list: |
| 1778 |
|
| 1779 |
:ball |
| 1780 |
|
| 1781 |
For more information, read |
| 1782 |
|
| 1783 |
:help :ball |
| 1784 |
|
| 1785 |
|
| 1786 |
8.6. How do I close (delete) a buffer without exiting Vim? |
| 1787 |
|
| 1788 |
You can use the ":bdelete" command to delete a buffer without exiting Vim. |
| 1789 |
For example: |
| 1790 |
|
| 1791 |
:bdelete file1 |
| 1792 |
|
| 1793 |
For more information, read |
| 1794 |
|
| 1795 |
:help :bdelete |
| 1796 |
:help :bwipeout |
| 1797 |
|
| 1798 |
|
| 1799 |
8.7. I have several buffers opened with :e filename. How do I close one of |
| 1800 |
the buffers without exiting Vim? |
| 1801 |
|
| 1802 |
You can use the ":bdelete <buffername>" command to close the buffer. |
| 1803 |
|
| 1804 |
For more information, read |
| 1805 |
|
| 1806 |
:help :bdelete |
| 1807 |
:help :bunload |
| 1808 |
:help :bwipeout |
| 1809 |
|
| 1810 |
|
| 1811 |
8.8. When I use the command ":%bd" to delete all the buffers, not all the |
| 1812 |
buffers are deleted. Why? |
| 1813 |
|
| 1814 |
In the ":%bd" command, the '%' range will be replaced with the starting and |
| 1815 |
ending line numbers in the current buffer. Instead of using '%' as the |
| 1816 |
range, you should specify numbers for the range. For example, to delete all |
| 1817 |
the buffers, you can use the command ":1,9999bd". |
| 1818 |
|
| 1819 |
For more information, read |
| 1820 |
|
| 1821 |
:help :bd |
| 1822 |
|
| 1823 |
|
| 1824 |
8.9. How do I display the buffer number of the current buffer/file? |
| 1825 |
|
| 1826 |
You can use 2<CTRL-G> command to display the buffer number for the current |
| 1827 |
file/buffer. Note the use of count before the CTRL-G command. If the count |
| 1828 |
is greater than 1, then Vim will display the buffer number. |
| 1829 |
|
| 1830 |
You can also use the following command to display the current buffer |
| 1831 |
number: |
| 1832 |
|
| 1833 |
:echo bufnr("%") |
| 1834 |
|
| 1835 |
You can also include the "%n" field to the 'statusline' option to display |
| 1836 |
the current buffer number on the statusline. |
| 1837 |
|
| 1838 |
For more information read, |
| 1839 |
|
| 1840 |
:help CTRL-G |
| 1841 |
:help bufnr() |
| 1842 |
:help :echo |
| 1843 |
:help 'statusline' |
| 1844 |
|
| 1845 |
|
| 1846 |
8.10. How do I delete a buffer without closing the window in which the |
| 1847 |
buffer is displayed? |
| 1848 |
|
| 1849 |
You can use the following command to open the next buffer and delete |
| 1850 |
the current buffer. |
| 1851 |
|
| 1852 |
:bnext | bdelete # |
| 1853 |
|
| 1854 |
For more information read, |
| 1855 |
|
| 1856 |
:help :bnext |
| 1857 |
:help :bdelete |
| 1858 |
:help :buffers |
| 1859 |
|
| 1860 |
|
| 1861 |
8.11. How do I map the tab key to cycle through and open all the buffers? |
| 1862 |
|
| 1863 |
You can use the following two map commands, to map the CTRL-Tab key to open |
| 1864 |
the next buffer and the CTRL-SHIFT-Tab key to open the previous buffer: |
| 1865 |
|
| 1866 |
:nnoremap <C-Tab> :bnext<CR> |
| 1867 |
:nnoremap <S-C-Tab> :bprevious<CR> |
| 1868 |
|
| 1869 |
For more information read, |
| 1870 |
|
| 1871 |
:help :bnext |
| 1872 |
:help :previous |
| 1873 |
|
| 1874 |
|
| 1875 |
============================================================================= |
| 1876 |
|
| 1877 |
SECTION 9 - WINDOWS |
| 1878 |
|
| 1879 |
|
| 1880 |
9.1. What is the difference between a Vim window and a buffer? |
| 1881 |
|
| 1882 |
A Vim buffer is a file loaded into memory for editing. The original file |
| 1883 |
remains unchanged until you write the buffer to the file. A Vim window is a |
| 1884 |
viewport onto a buffer. You can use multiple windows on one buffer or |
| 1885 |
several windows on different buffers. |
| 1886 |
|
| 1887 |
For more information, read |
| 1888 |
|
| 1889 |
:help usr_08.txt |
| 1890 |
:help 22.4 |
| 1891 |
:help windows-intro |
| 1892 |
:help Q_wi |
| 1893 |
|
| 1894 |
|
| 1895 |
9.2. How do I increase the width of a Vim window? |
| 1896 |
|
| 1897 |
You can increase the width of a Vim window using one of the following |
| 1898 |
commands: |
| 1899 |
|
| 1900 |
:vert resize +N |
| 1901 |
:vert resize -N |
| 1902 |
:vert resize N |
| 1903 |
|
| 1904 |
You can also use CTRL-W < or CTRL-W > or CTRL-W | commands. |
| 1905 |
|
| 1906 |
For more information, read |
| 1907 |
|
| 1908 |
:help vertical-resize |
| 1909 |
:help CTRL-W_> |
| 1910 |
:help CTRL-W_< |
| 1911 |
:help window-resize |
| 1912 |
|
| 1913 |
|
| 1914 |
9.3. How do I zoom into or out of a window? |
| 1915 |
|
| 1916 |
You can zoom into a window (close all the windows except the current |
| 1917 |
window) using the "CTRL-W o" command or the ":only" ex command. |
| 1918 |
|
| 1919 |
You can use the "CTRL-W _" command or the ":resize" ex command to increase |
| 1920 |
the current window height to the highest possible without closing other |
| 1921 |
windows. |
| 1922 |
|
| 1923 |
You can use the "CTRL-W |" command or the ":vertical resize" ex command to |
| 1924 |
increase the current window width to the highest possible without closing |
| 1925 |
other windows. |
| 1926 |
|
| 1927 |
You can use the "CTRL-W =" command to make the height and width of all the |
| 1928 |
windows equal. |
| 1929 |
|
| 1930 |
You can also set the following options to get better results with the above |
| 1931 |
commands: |
| 1932 |
|
| 1933 |
Method 1: |
| 1934 |
Set the 'winminheight' option to 0: |
| 1935 |
|
| 1936 |
:set winminheight=0 |
| 1937 |
|
| 1938 |
By default, this option is set to 1. |
| 1939 |
This option controls the minimum height of an inactive window (when it is |
| 1940 |
not the current window). When the 'winminheight' option is set to 0, only |
| 1941 |
the status line will be displayed for inactive windows. |
| 1942 |
|
| 1943 |
Method 2: |
| 1944 |
Set the 'noequalalways' option and set the 'winheight' option to a large |
| 1945 |
value (like 99999): |
| 1946 |
|
| 1947 |
:set noequalalways |
| 1948 |
:set winheight=99999 |
| 1949 |
|
| 1950 |
Now, the active window will always open to its maximum size, while the |
| 1951 |
other windows will stay present, but shrunken to just a status line. |
| 1952 |
|
| 1953 |
With any of the above mentioned methods, you cannot restore the window |
| 1954 |
layout after zooming into a window. If you want to restore the Vim window |
| 1955 |
layout after zooming into a window, you can use the ZoomWin plugin. You can |
| 1956 |
download this plugin from the Vim online website at: |
| 1957 |
|
| 1958 |
http://vim.sourceforge.net/scripts/script.php?script_id=508 |
| 1959 |
|
| 1960 |
For more information, read |
| 1961 |
|
| 1962 |
:help CTRL-W_o |
| 1963 |
:help window-resize |
| 1964 |
:help 'winminheight' |
| 1965 |
:help 'equalalways' |
| 1966 |
:help 'winheight' |
| 1967 |
:help 08.3 |
| 1968 |
|
| 1969 |
|
| 1970 |
9.4. How do I execute an ex command on all the open buffers or open windows |
| 1971 |
or all the files in the argument list? |
| 1972 |
|
| 1973 |
You can use the ":bufdo" command to execute an ex command on all the open |
| 1974 |
buffers. You can use the ":windo" command to execute an ex command on all |
| 1975 |
the open windows. You can use the ":argdo" command to execute an ex |
| 1976 |
command on all the files specified in the argument list. |
| 1977 |
|
| 1978 |
For more information, read |
| 1979 |
|
| 1980 |
:help :windo |
| 1981 |
:help :bufdo |
| 1982 |
:help :argdo |
| 1983 |
:help 26.3 |
| 1984 |
|
| 1985 |
|
| 1986 |
============================================================================= |
| 1987 |
|
| 1988 |
SECTION 10 - MOTION |
| 1989 |
|
| 1990 |
|
| 1991 |
10.1. How do I jump to the beginning (first line) or end (last line) of a |
| 1992 |
file? |
| 1993 |
|
| 1994 |
You can use 'G' command to jump to the last line in the file and the 'gg' |
| 1995 |
command to jump to the first line in the file. |
| 1996 |
|
| 1997 |
For more information, read |
| 1998 |
|
| 1999 |
:help G |
| 2000 |
:help gg |
| 2001 |
|
| 2002 |
|
| 2003 |
10.2. In insert mode, when I press the <Esc> key to go to command mode, the |
| 2004 |
cursor moves one character to the left (except when the cursor is on |
| 2005 |
the first character of the line). Is it possible to change this |
| 2006 |
behavior to keep the cursor at the same column? |
| 2007 |
|
| 2008 |
No. It is not possible to change this behavior. The cursor is *always* |
| 2009 |
positioned on a valid character (unless you have virtual-edit mode |
| 2010 |
enabled). So, if you are appending text to the end of a line, when you |
| 2011 |
return to command mode the cursor *must* drop back onto the last character |
| 2012 |
you typed. For consistency sake, the cursor drops back everywhere, even if |
| 2013 |
you are in the middle of a line. |
| 2014 |
|
| 2015 |
You can use the CTRL-O command in insert mode to execute a single ex |
| 2016 |
command and return back to insert mode without moving the cursor column. |
| 2017 |
|
| 2018 |
For more information, read |
| 2019 |
|
| 2020 |
:help 'virtual' |
| 2021 |
:help i_CTRL-O |
| 2022 |
|
| 2023 |
|
| 2024 |
10.3. How do I configure Vim to maintain the horizontal cursor position when |
| 2025 |
scrolling with the <Page Up>, <Page Down>, etc keys? |
| 2026 |
|
| 2027 |
You can reset the 'startofline' option to keep the cursor at the same |
| 2028 |
horizontal location when scrolling text: |
| 2029 |
|
| 2030 |
:set nostartofline |
| 2031 |
|
| 2032 |
For more information, read |
| 2033 |
|
| 2034 |
:help 'startofline' |
| 2035 |
|
| 2036 |
|
| 2037 |
10.4. Some lines in a file are more than the screen width and they are all |
| 2038 |
wrapped. When I use the j, k keys to move from one line to the next, |
| 2039 |
the cursor is moved to the next line in the file instead of the next |
| 2040 |
line on the screen. How do I move from one screen line to the next? |
| 2041 |
|
| 2042 |
You can use the gj and gk commands to move from one screen line to the |
| 2043 |
next/previous screen line. The j and k commands move the cursor from one |
| 2044 |
file line to the next file line. You can also avoid the line wrapping by |
| 2045 |
resetting the 'wrap' option: |
| 2046 |
|
| 2047 |
:set nowrap |
| 2048 |
|
| 2049 |
For more information, read |
| 2050 |
|
| 2051 |
:help gj |
| 2052 |
:help gk |
| 2053 |
:help 'wrap' |
| 2054 |
|
| 2055 |
You can use the following mappings: |
| 2056 |
|
| 2057 |
:map <Up> gk |
| 2058 |
:imap <Up> <C-o>gk |
| 2059 |
:map <Down> gj |
| 2060 |
:imap <Down> <C-o>gj |
| 2061 |
|
| 2062 |
|
| 2063 |
10.5. What is the definition of a sentence, paragraph and section in Vim? |
| 2064 |
|
| 2065 |
A sentence is defined as ending at a '.', '!' or '?' followed by either the |
| 2066 |
end of a line, or by a space (or two) or tab. Which characters and the |
| 2067 |
number of spaces needed to constitute a sentence ending is determined by |
| 2068 |
the 'joinspaces' and 'cpoptions' options. |
| 2069 |
|
| 2070 |
A paragraph begins after each empty line, and also at each of a set of |
| 2071 |
paragraph macros, specified by the pairs of characters in the 'paragraphs' |
| 2072 |
option. |
| 2073 |
|
| 2074 |
A section begins after a form-feed (<C-L>) in the first column and at each |
| 2075 |
of a set of section macros, specified by the pairs of characters in the |
| 2076 |
'sections' option. |
| 2077 |
|
| 2078 |
For more information, read |
| 2079 |
|
| 2080 |
:help sentence |
| 2081 |
:help 'joinspaces' |
| 2082 |
:help 'cpoptions' | /^\s*j\> |
| 2083 |
:help paragraph |
| 2084 |
:help section |
| 2085 |
:help word |
| 2086 |
|
| 2087 |
|
| 2088 |
10.6. How do I jump to beginning or end of a sentence, paragraph or a |
| 2089 |
section? |
| 2090 |
|
| 2091 |
You can use the following motion commands to jump to the beginning or end |
| 2092 |
of a sentence or a paragraph or a section: |
| 2093 |
|
| 2094 |
motion position where |
| 2095 |
( beginning current sentence |
| 2096 |
) end current sentence |
| 2097 |
{ beginning current paragraph |
| 2098 |
} end current paragraph |
| 2099 |
[] end previous section |
| 2100 |
[[ beginning current section |
| 2101 |
][ end current section |
| 2102 |
]] beginning next section |
| 2103 |
|
| 2104 |
Each of these motions can be preceded by a number which will extend the |
| 2105 |
jump forward (or backward). |
| 2106 |
|
| 2107 |
For more information, read |
| 2108 |
|
| 2109 |
:help object-motions |
| 2110 |
|
| 2111 |
|
| 2112 |
10.7. I have lines in a file that extends beyond the right extent of the |
| 2113 |
screen. How do I move the Vim view to the right to see the text off |
| 2114 |
the screen? |
| 2115 |
|
| 2116 |
You can use one of the following commands to horizontally scroll the screen |
| 2117 |
to the left or right: |
| 2118 |
|
| 2119 |
zl - scroll to the left |
| 2120 |
zh - scroll to the right |
| 2121 |
zL - scroll half a screenwidth to the left |
| 2122 |
zH - scroll half a screenwidth to the right |
| 2123 |
zs - scroll to position the cursor at the start of the screen |
| 2124 |
ze - scroll to position the cursor at the end of the screen |
| 2125 |
|
| 2126 |
You can use the g0 command to move the cursor to the first character of the |
| 2127 |
screen line and the g$ command to move the cursor to the last character of |
| 2128 |
the screen line without scrolling the screen. |
| 2129 |
|
| 2130 |
For more information, read |
| 2131 |
|
| 2132 |
:help scroll-horizontal |
| 2133 |
|
| 2134 |
|
| 2135 |
10.8. How do I scroll two or more buffers simultaneously? |
| 2136 |
|
| 2137 |
You can set the "scrollbind" option for each of the buffer to scroll them |
| 2138 |
simultaneously. |
| 2139 |
|
| 2140 |
For more information, read |
| 2141 |
|
| 2142 |
:help 'scrollbind' |
| 2143 |
:help scroll-binding |
| 2144 |
:help 'scrollopt' |
| 2145 |
|
| 2146 |
|
| 2147 |
10.9. When I use my arrow keys, Vim changes modes, inserts weird characters |
| 2148 |
in my document but doesn't move the cursor properly. What's going on? |
| 2149 |
|
| 2150 |
There are a couple of things that could be going on: either you are using |
| 2151 |
Vim over a slow connection or Vim doesn't understand the key sequence that |
| 2152 |
your keyboard is generating. |
| 2153 |
|
| 2154 |
If you are working over a slow connection (such as a 2400 bps modem), you |
| 2155 |
can try to set the 'timeout' or 'ttimeout' option. These options, combined |
| 2156 |
with the 'timeoutlen' and 'ttimeoutlen' options, may fix the problem. |
| 2157 |
|
| 2158 |
The preceding procedure will not work correctly if your terminal sends key |
| 2159 |
codes that Vim does not understand. In this situation, your best option is |
| 2160 |
to map your key sequence to a matching cursor movement command and save |
| 2161 |
these mappings in a file. You can then ":source" the file whenever you work |
| 2162 |
from that terminal. |
| 2163 |
|
| 2164 |
For more information, read |
| 2165 |
|
| 2166 |
:help 'timeout' |
| 2167 |
:help 'ttimeout' |
| 2168 |
:help 'timeoutlen' |
| 2169 |
:help 'ttimeoutlen' |
| 2170 |
:help :map |
| 2171 |
:help vt100-cursor-keys |
| 2172 |
|
| 2173 |
|
| 2174 |
10.10. How do I configure Vim to move the cursor to the end of the previous |
| 2175 |
line, when the left arrow key is pressed and the cursor is currently |
| 2176 |
at the beginning of a line? |
| 2177 |
|
| 2178 |
You can add the '<' flag to the 'whichwrap' option to configure Vim to move |
| 2179 |
the cursor to the end of the previous line, when the left arrow key is |
| 2180 |
pressed and the cursor is currently at the beginning of a line: |
| 2181 |
|
| 2182 |
:set whichwrap+=< |
| 2183 |
|
| 2184 |
Similarly, to move the cursor the beginning of the next line, when the |
| 2185 |
right arrow key is pressed and the cursor is currently at the end of a |
| 2186 |
line, add the '>' flag to the 'whichwrap' option: |
| 2187 |
|
| 2188 |
:set whichwrap+=> |
| 2189 |
|
| 2190 |
The above will work only in normal and visual modes. To use this in insert |
| 2191 |
and replace modes, add the '[' and ']' flags respectively. |
| 2192 |
|
| 2193 |
For more information, read |
| 2194 |
|
| 2195 |
:help 'whichwrap' |
| 2196 |
:help 05.7 |
| 2197 |
|
| 2198 |
|
| 2199 |
10.11. How do I configure Vim to stay only in insert mode (modeless |
| 2200 |
editing)? |
| 2201 |
|
| 2202 |
You can set the 'insertmode' option to configure Vim to stay only in insert |
| 2203 |
mode: |
| 2204 |
|
| 2205 |
:set insertmode |
| 2206 |
|
| 2207 |
By setting this option, you can use Vim as a modeless editor. If you press |
| 2208 |
the <Esc> key, Vim will not go to the normal mode. To execute a single |
| 2209 |
normal mode command, you can press CTRL-O followed by the normal mode |
| 2210 |
command. To execute more than one normal command, you can use CTRL-L |
| 2211 |
followed by the commands. To return to insert mode, press the <Esc> key. To |
| 2212 |
disable this option, reset the 'insertmode' option: |
| 2213 |
|
| 2214 |
:set noinsertmode |
| 2215 |
|
| 2216 |
You can also start vim using the "evim" command or you can use "vim -y" to |
| 2217 |
use Vim as a modeless editor. |
| 2218 |
|
| 2219 |
You can also start Vim in insert mode using the ":startinsert" ex command. |
| 2220 |
|
| 2221 |
For more information, read |
| 2222 |
|
| 2223 |
:help 'insertmode' |
| 2224 |
:help :startinsert |
| 2225 |
:help :stopinsert |
| 2226 |
:help i_CTRL-O |
| 2227 |
:help i_CTRL-L |
| 2228 |
:help evim |
| 2229 |
:help evim-keys |
| 2230 |
|
| 2231 |
|
| 2232 |
10.12. How do I display some context lines when scrolling text? |
| 2233 |
|
| 2234 |
You can set the 'scrolloff' option to display a minimal number of screen |
| 2235 |
lines (context) above and below the cursor. |
| 2236 |
|
| 2237 |
:set scrolloff=10 |
| 2238 |
|
| 2239 |
For more information, read |
| 2240 |
|
| 2241 |
:help 'scrolloff' |
| 2242 |
:help 'sidescrolloff' |
| 2243 |
|
| 2244 |
|
| 2245 |
10.13. How do I go back to previous cursor locations? |
| 2246 |
|
| 2247 |
You can go back to the cursor location before the latest jump using the '' |
| 2248 |
or `` command. You can use the CTRL-O command to go back to older cursor |
| 2249 |
positions and the CTRL-I command to go to the newer cursor positions in the |
| 2250 |
jump list. |
| 2251 |
|
| 2252 |
For more information, read |
| 2253 |
|
| 2254 |
:help 03.10 |
| 2255 |
:help mark-motions |
| 2256 |
:help jump-motions |
| 2257 |
|
| 2258 |
|
| 2259 |
============================================================================= |
| 2260 |
|
| 2261 |
SECTION 11 - SEARCHING TEXT |
| 2262 |
|
| 2263 |
|
| 2264 |
11.1. After I searched for a text with a pattern, all the matched text |
| 2265 |
stays highlighted. How do I turn off the highlighting |
| 2266 |
temporarily/permanently? |
| 2267 |
|
| 2268 |
The 'hlsearch' option controls whether all the matches for the last |
| 2269 |
searched pattern are highlighted or not. By default, this option is not |
| 2270 |
enabled. If this option is set in a system-wide vimrc file, then you can |
| 2271 |
turn off the search highlighting by using the following command: |
| 2272 |
|
| 2273 |
:set nohlsearch |
| 2274 |
|
| 2275 |
To temporarily turn off the search highlighting, use |
| 2276 |
|
| 2277 |
:nohlsearch |
| 2278 |
|
| 2279 |
You can also clear the search highlighting, by searching for a pattern that |
| 2280 |
is not in the current file (for example, search for the pattern 'asdf'). |
| 2281 |
|
| 2282 |
For more information, read |
| 2283 |
|
| 2284 |
:help 'hlsearch' |
| 2285 |
:help :nohlsearch |
| 2286 |
|
| 2287 |
|
| 2288 |
11.2. How do I enter a carriage return character in a search pattern? |
| 2289 |
|
| 2290 |
You can either use '\r' or <CTRL-V><CTRL-M> to enter a carriage return |
| 2291 |
character in a pattern. In Vim scripts, it is better to use '\r' for the |
| 2292 |
carriage return character. |
| 2293 |
|
| 2294 |
For more information, read |
| 2295 |
|
| 2296 |
:help sub-replace-special |
| 2297 |
|
| 2298 |
|
| 2299 |
11.3. How do I search for the character ^M? |
| 2300 |
|
| 2301 |
You can enter the ^M character in a search command by first pressing the |
| 2302 |
CTRL-V key and then pressing the CTRL-M key. |
| 2303 |
|
| 2304 |
/^V^M |
| 2305 |
|
| 2306 |
You can also use the "\r" character. In Vim scripts, "\r" is preferred. |
| 2307 |
|
| 2308 |
For more information, read |
| 2309 |
|
| 2310 |
:help c_CTRL-V |
| 2311 |
:help using_CTRL-V |
| 2312 |
:help /\r |
| 2313 |
|
| 2314 |
|
| 2315 |
11.4. How can I search/replace characters that display as '~R', '~S', etc.? |
| 2316 |
|
| 2317 |
You can use the 'ga' command to display the ASCII value/code for the |
| 2318 |
special character. For example, let us say the ASCII value is 142. Then you |
| 2319 |
can use the following command to search for the special character: |
| 2320 |
|
| 2321 |
/^V142 |
| 2322 |
|
| 2323 |
where, ^V is entered by pressing CTRL-V. |
| 2324 |
|
| 2325 |
For more information, read |
| 2326 |
|
| 2327 |
:help ga |
| 2328 |
:help using_CTRL_V |
| 2329 |
:help 24.8 |
| 2330 |
|
| 2331 |
|
| 2332 |
11.5. How do I highlight all the non-printable characters in a file? |
| 2333 |
|
| 2334 |
You can use the following commands and search pattern to highlight all the |
| 2335 |
non-printable characters in a file: |
| 2336 |
|
| 2337 |
:set hlsearch |
| 2338 |
/\(\p\|$\)\@!. |
| 2339 |
|
| 2340 |
For more information, read |
| 2341 |
|
| 2342 |
:help /\p |
| 2343 |
:help /bar |
| 2344 |
:help /$ |
| 2345 |
:help /\( |
| 2346 |
:help /\@! |
| 2347 |
:help 'hlsearch' |
| 2348 |
|
| 2349 |
|
| 2350 |
11.6. How do I search for whole words in a file? |
| 2351 |
|
| 2352 |
You can search for whole words in a file using the \< and \> atoms. For |
| 2353 |
example: |
| 2354 |
|
| 2355 |
/\<myword\> |
| 2356 |
|
| 2357 |
The \< atom matches the beginning of the word and the \> atom matches the |
| 2358 |
end of the word. |
| 2359 |
|
| 2360 |
For more information, read |
| 2361 |
|
| 2362 |
:help /\< |
| 2363 |
:help /\> |
| 2364 |
|
| 2365 |
|
| 2366 |
11.7. How do I search for the current word under the cursor? |
| 2367 |
|
| 2368 |
You can press the * key to search forward for the current word under the |
| 2369 |
cursor. To search backward, you can press the # key. Note that only whole |
| 2370 |
keywords will be searched using these commands. |
| 2371 |
|
| 2372 |
For more information, read |
| 2373 |
|
| 2374 |
:help star |
| 2375 |
:help # |
| 2376 |
:help g* |
| 2377 |
:help g# |
| 2378 |
:help 03.8 |
| 2379 |
:help search-commands |
| 2380 |
|
| 2381 |
|
| 2382 |
11.8. How do I search for a word without regard to the case (uppercase or |
| 2383 |
lowercase)? |
| 2384 |
|
| 2385 |
To always ignore case while searching for a pattern, set the 'ignorecase' |
| 2386 |
option: |
| 2387 |
|
| 2388 |
:set ignorecase |
| 2389 |
|
| 2390 |
To ignore case only when searching a particular pattern, use the special \c |
| 2391 |
directive: |
| 2392 |
|
| 2393 |
/\c<pattern> |
| 2394 |
|
| 2395 |
For more information, read |
| 2396 |
|
| 2397 |
:help 'ignorecase' |
| 2398 |
:help /ignorecase |
| 2399 |
:help /\c |
| 2400 |
|
| 2401 |
|
| 2402 |
11.9. How do I search for words that occur twice consecutively? |
| 2403 |
|
| 2404 |
You can use one of the following search commands to locate words that occur |
| 2405 |
twice consecutively: |
| 2406 |
|
| 2407 |
/\(\<\w\+\)\_s\+\1\> |
| 2408 |
/\(\<\k\+\)\_s\+\1\> |
| 2409 |
|
| 2410 |
The main difference is the use of '\w' and '\k', where the latter is based |
| 2411 |
on the 'iskeyword' option which may include accented and other language |
| 2412 |
specific characters. |
| 2413 |
|
| 2414 |
For more information, read |
| 2415 |
|
| 2416 |
:help /\1 |
| 2417 |
:help /\( |
| 2418 |
:help /\) |
| 2419 |
:help /\< |
| 2420 |
:help /\> |
| 2421 |
:help /\w |
| 2422 |
:help /\k |
| 2423 |
:help /\+ |
| 2424 |
:help /\_x |
| 2425 |
:help 'iskeyword' |
| 2426 |
|
| 2427 |
|
| 2428 |
11.10. How do I count the number of times a particular word occurs in a |
| 2429 |
buffer? |
| 2430 |
|
| 2431 |
You can use the following set of commands to count the number of times a |
| 2432 |
particular word occurs in a buffer: |
| 2433 |
|
| 2434 |
:let cnt=0 |
| 2435 |
:g/\<your_word\>/let cnt=cnt+1 |
| 2436 |
:echo cnt |
| 2437 |
|
| 2438 |
This only counts the number of lines where the word occurs. You can also |
| 2439 |
use the following command: |
| 2440 |
|
| 2441 |
:%s/\<word\>/&/g |
| 2442 |
|
| 2443 |
To count the number of alphabetic words in a file, you can use |
| 2444 |
|
| 2445 |
:%s/\a\+/&/g |
| 2446 |
|
| 2447 |
To count the number of words made up of non-space characters, you can use |
| 2448 |
|
| 2449 |
:%s/\S\+/&/g |
| 2450 |
|
| 2451 |
For more information, read |
| 2452 |
|
| 2453 |
:help count-items |
| 2454 |
:help word-count |
| 2455 |
:help v_g_CTRL-G |
| 2456 |
:help 12.5 |
| 2457 |
|
| 2458 |
|
| 2459 |
11.11. How do I place the cursor at the end of the matched word when |
| 2460 |
searching for a pattern? |
| 2461 |
|
| 2462 |
You can use the 'e' offset to the search command to place the cursor at the |
| 2463 |
end of the matched word. For example |
| 2464 |
|
| 2465 |
/mypattern/e |
| 2466 |
|
| 2467 |
For more information about search offsets, read |
| 2468 |
|
| 2469 |
:help search-offset |
| 2470 |
:help / |
| 2471 |
|
| 2472 |
|
| 2473 |
11.12. How do I search for an empty line? |
| 2474 |
|
| 2475 |
You can search for an empty line using: |
| 2476 |
|
| 2477 |
/^$ |
| 2478 |
|
| 2479 |
or |
| 2480 |
|
| 2481 |
/^\s*$ |
| 2482 |
|
| 2483 |
For more information, read |
| 2484 |
|
| 2485 |
:help /^ |
| 2486 |
:help /$ |
| 2487 |
:help /\s |
| 2488 |
:help /* |
| 2489 |
:help search-commands |
| 2490 |
|
| 2491 |
|
| 2492 |
11.13. How do I search for a line containing only a single character? |
| 2493 |
|
| 2494 |
You can search for a line containing only a single character using: |
| 2495 |
|
| 2496 |
/^\s*\a\s*$ |
| 2497 |
|
| 2498 |
For more information, read |
| 2499 |
|
| 2500 |
:help /^ |
| 2501 |
:help /\a |
| 2502 |
:help /\s |
| 2503 |
:help /* |
| 2504 |
:help /$ |
| 2505 |
|
| 2506 |
|
| 2507 |
11.14. How do I search and replace a string in multiple files? |
| 2508 |
|
| 2509 |
You can use the 'argdo' or 'bufdo' or 'windo' commands to execute an ex |
| 2510 |
command on multiple files. For example: |
| 2511 |
|
| 2512 |
:argdo %s/foo/bar/g |
| 2513 |
|
| 2514 |
For more information, read |
| 2515 |
|
| 2516 |
:help :argdo |
| 2517 |
:help :bufdo |
| 2518 |
:help :windo |
| 2519 |
|
| 2520 |
|
| 2521 |
11.15. I am using the ":s" substitute command in a mapping. When a search |
| 2522 |
for a pattern fails, the map terminates. I would like the map to |
| 2523 |
continue processing the next command, even if the substitute command |
| 2524 |
fails. How do I do this? |
| 2525 |
|
| 2526 |
You can use the 'e' flag to the substitute command to continue processing |
| 2527 |
other commands in a map, when a pattern is not found. |
| 2528 |
|
| 2529 |
For more information, read |
| 2530 |
|
| 2531 |
:help :s_flags |
| 2532 |
|
| 2533 |
|
| 2534 |
11.16. How do I search for the n-th occurrence of a character in a line? |
| 2535 |
|
| 2536 |
To search for the n-th occurrence of a character in a line, you can prefix |
| 2537 |
the 'f' command with a number. For example, to search for the 5th |
| 2538 |
occurrence of the character @ in a line, you can use the command 5f@. This |
| 2539 |
assumes the cursor is at the beginning of the line - and that this first |
| 2540 |
character is not the one your are looking for. |
| 2541 |
|
| 2542 |
For more information, read |
| 2543 |
|
| 2544 |
:help f |
| 2545 |
:help F |
| 2546 |
:help t |
| 2547 |
:help T |
| 2548 |
:help ; |
| 2549 |
:help , |
| 2550 |
|
| 2551 |
|
| 2552 |
11.17. How do I replace a tab (or any other character) with a hard return |
| 2553 |
(newline) character? |
| 2554 |
|
| 2555 |
You can replace a tab (or any other character) with a hard return (newline) |
| 2556 |
character using the following command: |
| 2557 |
|
| 2558 |
:s/\t/\r/ |
| 2559 |
|
| 2560 |
Note that in the above command, if you use \n instead of \r, then the tab |
| 2561 |
characters will not be replaced by a new-line character. |
| 2562 |
|
| 2563 |
For more information, read |
| 2564 |
|
| 2565 |
:help sub-replace-special |
| 2566 |
:help NL-used-for-Nul |
| 2567 |
:help CR-used-for-NL |
| 2568 |
|
| 2569 |
|
| 2570 |
11.18. How do I search for a character by its ASCII value? |
| 2571 |
|
| 2572 |
You can search for a character by its ASCII value by pressing CTRL-V |
| 2573 |
followed by the decimal or hexadecimal or octal value of that character in |
| 2574 |
the search "/" command. To determine the ASCII value of a character you |
| 2575 |
can use the ":ascii" or the "ga" command. |
| 2576 |
|
| 2577 |
For more information, read |
| 2578 |
|
| 2579 |
:help i_CTRL-V_digit |
| 2580 |
:help :ascii |
| 2581 |
:help ga |
| 2582 |
|
| 2583 |
|
| 2584 |
11.19. How do I search for long lines? |
| 2585 |
|
| 2586 |
You can search for long lines or lines containing more than a specific |
| 2587 |
number of characters using the Vim regular-expressions in the search |
| 2588 |
command. For example, to search for all the lines containing more than 80 |
| 2589 |
characters, you can use one of the following commands: |
| 2590 |
|
| 2591 |
/^.\{80}.*$ |
| 2592 |
/^.*\%80c.*$ |
| 2593 |
|
| 2594 |
For more information, read |
| 2595 |
|
| 2596 |
:help /\{ |
| 2597 |
:help /\%c |
| 2598 |
|
| 2599 |
|
| 2600 |
11.20. How do I display all the lines in the current buffer that contain a |
| 2601 |
specified pattern? |
| 2602 |
|
| 2603 |
You can use the following command to display all the lines in the current |
| 2604 |
buffer that contain a specified pattern: |
| 2605 |
|
| 2606 |
:g/<pattern>/p |
| 2607 |
|
| 2608 |
For example, the following command will display all the lines in the |
| 2609 |
current buffer that contain "vim": |
| 2610 |
|
| 2611 |
:g/vim/p |
| 2612 |
|
| 2613 |
If you also want the corresponding line numbers, then you can use the |
| 2614 |
following command: |
| 2615 |
|
| 2616 |
:g/<pattern>/# |
| 2617 |
|
| 2618 |
For more information, read |
| 2619 |
|
| 2620 |
:help :global |
| 2621 |
:help :print |
| 2622 |
:help :number |
| 2623 |
|
| 2624 |
|
| 2625 |
11.21. How do I search for a text string that spans multiple lines? |
| 2626 |
|
| 2627 |
You can search for a text string that spans multiple lines using the \_x |
| 2628 |
regular expression atom. For example, to search for the text string "Hello |
| 2629 |
World", you can use the following search command: |
| 2630 |
|
| 2631 |
/Hello\_sWorld |
| 2632 |
|
| 2633 |
This will match the word "Hello" followed by a newline character and then |
| 2634 |
the word "World" at the beginning of the next line. This will also match |
| 2635 |
the word "Hello" immediately followed by a space character and then the |
| 2636 |
word "World". When searching for the "Hello World" string, to include the |
| 2637 |
space characters at the end and beginning of the line, you can use the |
| 2638 |
following search command: |
| 2639 |
|
| 2640 |
/Hello\_s\+World |
| 2641 |
|
| 2642 |
For more information, read |
| 2643 |
|
| 2644 |
:help 27.8 |
| 2645 |
:help pattern-atoms |
| 2646 |
:help /\_ |
| 2647 |
:help pattern-searches |
| 2648 |
|
| 2649 |
|
| 2650 |
11.22. How do I search for a pattern within the specified range of lines |
| 2651 |
in a buffer? |
| 2652 |
|
| 2653 |
You can search for a pattern within a range of lines using the \%>l |
| 2654 |
and \%<l regular expression atoms. |
| 2655 |
|
| 2656 |
For example, to search for the word 'white' between the lines 10 and 30 in |
| 2657 |
a buffer, you can use the following command: |
| 2658 |
|
| 2659 |
/white\%>10l\%<20l |
| 2660 |
|
| 2661 |
For more information, read |
| 2662 |
|
| 2663 |
:help /\%l |
| 2664 |
|
| 2665 |
|
| 2666 |
============================================================================= |
| 2667 |
|
| 2668 |
SECTION 12 - CHANGING TEXT |
| 2669 |
|
| 2670 |
|
| 2671 |
12.1. How do I delete all the trailing white space characters (SPACE and |
| 2672 |
TAB) at the end of all the lines in a file? |
| 2673 |
|
| 2674 |
You can use the ":substitute" command on the entire file to search and |
| 2675 |
remove all the trailing white space characters: |
| 2676 |
|
| 2677 |
:%s/\s\+$// |
| 2678 |
|
| 2679 |
For more information, read |
| 2680 |
|
| 2681 |
:help :% |
| 2682 |
:help :s |
| 2683 |
:help /\s |
| 2684 |
:help /\+ |
| 2685 |
:help /$ |
| 2686 |
|
| 2687 |
|
| 2688 |
12.2. How do I replace all the occurrences of multiple consecutive space |
| 2689 |
characters to a single space? |
| 2690 |
|
| 2691 |
You can use the following command to replace all the occurrences of |
| 2692 |
multiple consecutive space characters to a single space: |
| 2693 |
|
| 2694 |
:%s/ \{2,}/ /g |
| 2695 |
|
| 2696 |
For more information, read |
| 2697 |
|
| 2698 |
:help :% |
| 2699 |
:help :s |
| 2700 |
:help /\{ |
| 2701 |
:help :s_flags |
| 2702 |
|
| 2703 |
|
| 2704 |
12.3. How do I reduce a range of empty lines into one line only? |
| 2705 |
|
| 2706 |
You can use the following command to reduce a range of empty lines into one |
| 2707 |
line only: |
| 2708 |
|
| 2709 |
:v/./.,/./-1join |
| 2710 |
|
| 2711 |
The explanation for this command is below: |
| 2712 |
|
| 2713 |
:v/./ Execute the following command for all lines not |
| 2714 |
containing a character (empty lines). |
| 2715 |
., Use the current line as the start of the range of |
| 2716 |
lines. |
| 2717 |
/./ Use the line containing a character as the last line. |
| 2718 |
-1 Adjust the range of lines to end with the line before |
| 2719 |
the last line. |
| 2720 |
j Join the lines in the range. |
| 2721 |
|
| 2722 |
Note that this will give an error message if the empty lines are at the end |
| 2723 |
of the file. To correct this, you have to add a temporary line at the end |
| 2724 |
of the file, execute the command and then remove the temporary line. |
| 2725 |
|
| 2726 |
For more information, read |
| 2727 |
|
| 2728 |
:help :v |
| 2729 |
:help :join |
| 2730 |
:help cmdline-ranges |
| 2731 |
:help collapse |
| 2732 |
|
| 2733 |
|
| 2734 |
12.4. How do I delete all blank lines in a file? How do I remove all the |
| 2735 |
lines containing only space characters? |
| 2736 |
|
| 2737 |
To remove all blank lines, use the following command: |
| 2738 |
|
| 2739 |
:g/^$/d |
| 2740 |
|
| 2741 |
To remove all lines with only whitespace (spaces or tabs) in them, use the |
| 2742 |
following command: |
| 2743 |
|
| 2744 |
:g/^\s\+$/d |
| 2745 |
|
| 2746 |
To remove all the lines with only whitespace, if anything, use the |
| 2747 |
following command: |
| 2748 |
|
| 2749 |
:g/^\s*$/d |
| 2750 |
|
| 2751 |
|
| 2752 |
12.5. How do I copy/yank the current word? |
| 2753 |
|
| 2754 |
You can use the "yiw" (yank inner word without whitespace) command or the |
| 2755 |
"yaw" (yank a word with whitespace) command to copy/yank the current |
| 2756 |
word. |
| 2757 |
|
| 2758 |
For more information, read |
| 2759 |
|
| 2760 |
:help 04.6 |
| 2761 |
:help 04.8 |
| 2762 |
:help iw |
| 2763 |
:help yank |
| 2764 |
:help text-objects |
| 2765 |
:help objects |
| 2766 |
|
| 2767 |
|
| 2768 |
12.6. How do I yank text from one position to another position within a |
| 2769 |
line, without yanking the entire line? |
| 2770 |
|
| 2771 |
You can specify a motion command with the yank operator (y) to yank text |
| 2772 |
from one position to another position within a line. For example, to yank |
| 2773 |
from the current cursor position till the next letter x, use yfx or Fx or |
| 2774 |
tx or Tx. To yank till the nth column, use n|. To yank till the next |
| 2775 |
occurrence of a 'word', use /word. To do a yank till the nth column on |
| 2776 |
another line, first mark the position using the 'ma' command, go to the |
| 2777 |
start of the yank position, and then yank till the mark using y`a (note the |
| 2778 |
direction of the quote) |
| 2779 |
|
| 2780 |
For more information, read |
| 2781 |
|
| 2782 |
:help yank |
| 2783 |
:help motion.txt |
| 2784 |
:help 4.6 |
| 2785 |
|
| 2786 |
|
| 2787 |
12.7. When I yank some text into a register, how do I append the text to |
| 2788 |
the current contents of the register? |
| 2789 |
|
| 2790 |
When you specify the register for some operation, if you use the upper-case |
| 2791 |
for the register name, then the new text will be appended to the existing |
| 2792 |
contents. For example, if you have some text in the register "a". If you |
| 2793 |
want to append some new text to this, you have to use the "A" register |
| 2794 |
name. If you use the lowercase register name, then the contents of the |
| 2795 |
register will be overwritten with the new text. |
| 2796 |
|
| 2797 |
For more information, read |
| 2798 |
|
| 2799 |
:help quote |
| 2800 |
:help quote_alpha |
| 2801 |
:help 10.1 |
| 2802 |
|
| 2803 |
|
| 2804 |
12.8. How do I yank a complete sentence that spans over more than one line? |
| 2805 |
|
| 2806 |
To yank a complete sentence that spans over more than one line you have to |
| 2807 |
use the yank operator followed by a motion command. For example: |
| 2808 |
|
| 2809 |
y) |
| 2810 |
|
| 2811 |
From inside the sentence you can use 'yi)' to yank the sentence. |
| 2812 |
|
| 2813 |
For more information, read |
| 2814 |
|
| 2815 |
:help yank |
| 2816 |
:help {motion} |
| 2817 |
:help object-motions |
| 2818 |
:help 4.6 |
| 2819 |
|
| 2820 |
|
| 2821 |
12.9. How do I yank all the lines containing a pattern into a buffer? |
| 2822 |
|
| 2823 |
You can use the ":global" command to yank all the lines containing the |
| 2824 |
pattern into a register and then paste the contents of the register into |
| 2825 |
the buffer: |
| 2826 |
|
| 2827 |
:let @a='' |
| 2828 |
:g/mypattern/y A |
| 2829 |
|
| 2830 |
The first command, clears the contents of the register "a". The second |
| 2831 |
command copies all the lines containing "mypattern" into the register "a". |
| 2832 |
Note that the capital letter "A" is used to append the matched lines. Now |
| 2833 |
you can paste the contents of register "a" to a buffer using "ap command. |
| 2834 |
For more information, read |
| 2835 |
|
| 2836 |
:help :g |
| 2837 |
:help :y |
| 2838 |
:help let-register |
| 2839 |
:help quote_alpha |
| 2840 |
:help put |
| 2841 |
:help registers |
| 2842 |
:help :registers |
| 2843 |
|
| 2844 |
|
| 2845 |
12.10. How do I delete all the lines in a file that does not contain a |
| 2846 |
pattern? |
| 2847 |
|
| 2848 |
You can use ":v" command to delete all the lines that does not contain a |
| 2849 |
pattern: |
| 2850 |
|
| 2851 |
:v/pattern/d |
| 2852 |
|
| 2853 |
or |
| 2854 |
|
| 2855 |
:g!/pattern/d |
| 2856 |
|
| 2857 |
For more information, read |
| 2858 |
|
| 2859 |
:help :v |
| 2860 |
:help :g |
| 2861 |
|
| 2862 |
|
| 2863 |
12.11. How do I add a line before each line with "pattern" in it? |
| 2864 |
|
| 2865 |
You can use the following command to add a line before each line with |
| 2866 |
"pattern" in it: |
| 2867 |
|
| 2868 |
:g/pattern/normal Oi<line of text goes here> |
| 2869 |
|
| 2870 |
Alternatively you can yank the line using the Y command and then insert the |
| 2871 |
line using the following command: |
| 2872 |
|
| 2873 |
:g/pattern/put! |
| 2874 |
|
| 2875 |
For more information, read |
| 2876 |
|
| 2877 |
:help :g |
| 2878 |
:help :put |
| 2879 |
:help insert |
| 2880 |
:help 0 |
| 2881 |
|
| 2882 |
|
| 2883 |
12.12. Is there a way to operate on a line if the previous line contains a |
| 2884 |
particular pattern? |
| 2885 |
|
| 2886 |
You can use the ":global" command to operate on a line, if the previous |
| 2887 |
line contains a particular pattern: |
| 2888 |
|
| 2889 |
:g/<pattern>/+{cmd} |
| 2890 |
|
| 2891 |
For more information, read |
| 2892 |
|
| 2893 |
:help :g |
| 2894 |
:help :range |
| 2895 |
|
| 2896 |
|
| 2897 |
12.13. How do I execute a command on all the lines containing a pattern? |
| 2898 |
|
| 2899 |
You can use the ":global" (:g) command to execute a command on all the |
| 2900 |
lines containing a pattern. |
| 2901 |
|
| 2902 |
:g/my pattern/d |
| 2903 |
|
| 2904 |
If you want to use a non-Ex command, then you can use the ":normal" |
| 2905 |
command: |
| 2906 |
|
| 2907 |
:g/my pattern/normal {command} |
| 2908 |
|
| 2909 |
Unless you want the normal mode commands to be remapped, consider using a |
| 2910 |
":normal!" command instead (note the "!"). |
| 2911 |
|
| 2912 |
For more information, read |
| 2913 |
|
| 2914 |
:help :global |
| 2915 |
:help :v |
| 2916 |
:help :normal |
| 2917 |
|
| 2918 |
|
| 2919 |
12.14. Can I copy the character above the cursor to the current cursor |
| 2920 |
position? |
| 2921 |
|
| 2922 |
In Insert mode, you can copy the character above the cursor to the current |
| 2923 |
cursor position by typing <Ctrl-Y>. The same can be done with the |
| 2924 |
characters below the cursor by typing <Ctrl-E>. |
| 2925 |
|
| 2926 |
For more information, read |
| 2927 |
|
| 2928 |
:help i_CTRL-Y |
| 2929 |
:help i_CTRL-E |
| 2930 |
|
| 2931 |
|
| 2932 |
12.15. How do I insert a blank line above/below the current line without |
| 2933 |
entering insert mode? |
| 2934 |
|
| 2935 |
You can use the ":put" ex command to insert blank lines. For example, try |
| 2936 |
|
| 2937 |
:put ='' |
| 2938 |
:put! ='' |
| 2939 |
|
| 2940 |
For more information, read |
| 2941 |
|
| 2942 |
:help :put |
| 2943 |
|
| 2944 |
|
| 2945 |
12.16. How do I insert the name of current file into the current buffer? |
| 2946 |
|
| 2947 |
There are several ways to insert the name of the current file into the |
| 2948 |
current buffer. In insert mode, you can use the <C-R>% or the |
| 2949 |
<C-R>=expand("%") command. In normal mode, you can use the ":put =@%" |
| 2950 |
command. |
| 2951 |
|
| 2952 |
For more information, read |
| 2953 |
|
| 2954 |
:help i_CTRL-R |
| 2955 |
:help expand() |
| 2956 |
:help !! |
| 2957 |
|
| 2958 |
|
| 2959 |
12.17. How do I insert the contents of a Vim register into the current |
| 2960 |
buffer? |
| 2961 |
|
| 2962 |
In insert mode, you can use the <C-R><register> command to insert the |
| 2963 |
contents of <register>. For example, use <C-R>a to insert the contents |
| 2964 |
of register "a" into the current buffer. |
| 2965 |
|
| 2966 |
In normal mode, you can use the ":put <register>" command to insert the |
| 2967 |
contents of <register>. For example, use the ":put d" command to insert |
| 2968 |
the contents of register "d" into the current buffer. |
| 2969 |
|
| 2970 |
For more information, read |
| 2971 |
|
| 2972 |
:help i_CTRL-R |
| 2973 |
:help :put |
| 2974 |
|
| 2975 |
|
| 2976 |
12.18. How do I move the cursor past the end of line and insert some |
| 2977 |
characters at some columns after the end of the line? |
| 2978 |
|
| 2979 |
You can set the "virtualedit" option to move the cursor past the |
| 2980 |
end-of-line and insert characters in a column after the end-of-line. To |
| 2981 |
start the virtual mode, use |
| 2982 |
|
| 2983 |
:set virtualedit=all |
| 2984 |
|
| 2985 |
For more information, read |
| 2986 |
|
| 2987 |
:help 'virtualedit' |
| 2988 |
|
| 2989 |
|
| 2990 |
12.19. How to replace the word under the cursor (say: junk) with |
| 2991 |
"foojunkbar" in Vim? |
| 2992 |
|
| 2993 |
There are several ways to do this. If the word is the first such word on |
| 2994 |
the line, use the following command: |
| 2995 |
|
| 2996 |
:exe "s/".expand("<cword>")."/foo&bar/" |
| 2997 |
|
| 2998 |
Too match specifically you could use a more complex substitution like this: |
| 2999 |
|
| 3000 |
:exe 's/\<'.expand("<cword>").'\%>'.(col(".")-1).'c\>/foo&bar/' |
| 3001 |
|
| 3002 |
You can also use the command: ciwfoo<C-R>"bar<Esc> |
| 3003 |
|
| 3004 |
For more information, read |
| 3005 |
|
| 3006 |
:help :substitute |
| 3007 |
:help expand() |
| 3008 |
:help col() |
| 3009 |
:help /\%c |
| 3010 |
|
| 3011 |
|
| 3012 |
12.20. How do I replace a particular text in all the files in a directory? |
| 3013 |
|
| 3014 |
You can use the "argdo" command to execute the substitute command on all |
| 3015 |
the files specified as arguments: |
| 3016 |
|
| 3017 |
:args * |
| 3018 |
:argdo %s/<your_text>/<replacement_text>/ge | update |
| 3019 |
|
| 3020 |
For more information, read |
| 3021 |
|
| 3022 |
:help :args_f |
| 3023 |
:help :argdo |
| 3024 |
:help :s_flags |
| 3025 |
|
| 3026 |
|
| 3027 |
12.21. I have some numbers in a file. How do I increment or decrement the |
| 3028 |
numbers in the file? |
| 3029 |
|
| 3030 |
You can use the CTRL-A key to increment the number and the CTRL-X key to |
| 3031 |
decrement the number. You can also specify the number to |
| 3032 |
increment/decrement from the number by specifying a count to the key. This |
| 3033 |
works for decimal, octal and hexadecimal numbers. You can change the base |
| 3034 |
used by Vim for this operation by modifying the 'nrformats' option. |
| 3035 |
|
| 3036 |
For more information, read |
| 3037 |
|
| 3038 |
:help 26.2 |
| 3039 |
:help CTRL-A |
| 3040 |
:help CTRL-X |
| 3041 |
:help 'nrformats' |
| 3042 |
|
| 3043 |
|
| 3044 |
12.22. How do I reuse the last used search pattern in a ":substitute" |
| 3045 |
command? |
| 3046 |
|
| 3047 |
To reuse the last used search pattern in a ":substitute" command, don't |
| 3048 |
specify a new search pattern: |
| 3049 |
|
| 3050 |
:s/pattern/newtext/ |
| 3051 |
:s//sometext/ |
| 3052 |
|
| 3053 |
In the second ":s" command, as a search pattern is not specified, the |
| 3054 |
pattern specified in the first ":s" command 'pattern' will be used. |
| 3055 |
|
| 3056 |
If you want to change the search pattern but repeat the substitution |
| 3057 |
pattern you can use the special right hand side, you can use the tilde |
| 3058 |
character: |
| 3059 |
|
| 3060 |
:s/newpattern/~/ |
| 3061 |
|
| 3062 |
For more information, read |
| 3063 |
|
| 3064 |
:help :s |
| 3065 |
:help :& |
| 3066 |
:help :~ |
| 3067 |
:help & |
| 3068 |
:help sub-replace-special |
| 3069 |
|
| 3070 |
|
| 3071 |
12.23. How do I change the case of a string using the ":substitute" |
| 3072 |
command? |
| 3073 |
|
| 3074 |
You can use special characters in the replacement string for a |
| 3075 |
":substitute" command to change the case of the matched string. For |
| 3076 |
example, to change the case of the string "MyString" to all uppercase, you |
| 3077 |
can use the following command: |
| 3078 |
|
| 3079 |
:%s/MyString/\U&/g |
| 3080 |
|
| 3081 |
To change the case to lowercase, you can use the following command: |
| 3082 |
|
| 3083 |
:%s/MyString/\L&/g |
| 3084 |
|
| 3085 |
To change the case of the first character in all the words in the current |
| 3086 |
line to uppercase, you can use the following command: |
| 3087 |
|
| 3088 |
:s/\<\(.\)\(\k*\)\>/\u\1\L\2/g |
| 3089 |
|
| 3090 |
For more information, read |
| 3091 |
|
| 3092 |
:help sub-replace-special |
| 3093 |
:help :substitute |
| 3094 |
:help \U |
| 3095 |
:help \L |
| 3096 |
:help \u |
| 3097 |
|
| 3098 |
|
| 3099 |
12.24. How do I enter characters that are not present in the keyboard? |
| 3100 |
|
| 3101 |
You can use digraphs to enter characters that are not present in the |
| 3102 |
keyboard. You can use the ":digraphs" command to display all the currently |
| 3103 |
defined digraphs. You can add a new digraph to the list using the |
| 3104 |
":digraphs" command. |
| 3105 |
|
| 3106 |
For more information, read |
| 3107 |
|
| 3108 |
:help digraphs |
| 3109 |
:help 'digraphs' |
| 3110 |
:help 24.9 |
| 3111 |
|
| 3112 |
|
| 3113 |
12.25. Is there a command to remove any or all digraphs? |
| 3114 |
|
| 3115 |
No. The digraphs table is defined at compile time. You can only add new |
| 3116 |
ones. Adding a command to remove digraphs is on the todo list. |
| 3117 |
|
| 3118 |
|
| 3119 |
12.26. In insert mode, when I press the backspace key, it erases only the |
| 3120 |
characters entered in this instance of insert mode. How do I erase |
| 3121 |
previously entered characters in insert mode using the backspace |
| 3122 |
key? |
| 3123 |
|
| 3124 |
You can set the 'backspace' option to erase previously entered characters |
| 3125 |
in insert mode: |
| 3126 |
|
| 3127 |
:set backspace=indent,eol,start |
| 3128 |
|
| 3129 |
For more information, read |
| 3130 |
|
| 3131 |
:help 'backspace' |
| 3132 |
:help i_backspacing |
| 3133 |
|
| 3134 |
|
| 3135 |
12.27. I have a file which has lines longer than 72 characters terminated |
| 3136 |
with "+" and wrapped to the next line. How can I quickly join the |
| 3137 |
lines? |
| 3138 |
|
| 3139 |
You can use the ":global" command to search and join the lines: |
| 3140 |
|
| 3141 |
:g/+$/j |
| 3142 |
|
| 3143 |
This will, however, only join every second line. A couple of more complex |
| 3144 |
examples which will join all consecutive lines with a "+" at the end are: |
| 3145 |
|
| 3146 |
:g/*$/,/\(^\|[^+]\)$/j |
| 3147 |
:g/+$/mark a | .,/\(^\|[^+]\)$/s/+$// | 'a,.j |
| 3148 |
|
| 3149 |
For more information, read |
| 3150 |
|
| 3151 |
:help :g |
| 3152 |
:help :j |
| 3153 |
:help :mark |
| 3154 |
|
| 3155 |
|
| 3156 |
12.28. How do I paste characterwise yanked text into separate lines? |
| 3157 |
|
| 3158 |
You can use the ":put" command to paste characterwise yanked text into new |
| 3159 |
lines: |
| 3160 |
|
| 3161 |
:put =@" |
| 3162 |
|
| 3163 |
For more information, read |
| 3164 |
|
| 3165 |
:help :put |
| 3166 |
:help quote_= |
| 3167 |
|
| 3168 |
|
| 3169 |
12.29. How do I change the case (uppercase, lowercase) of a word or a |
| 3170 |
character or a block of text? |
| 3171 |
|
| 3172 |
You can use the "~" command to switch the case of a character. |
| 3173 |
|
| 3174 |
You can change the case of the word under the cursor to uppercase using the |
| 3175 |
"gUiw" or "viwU" command and to lowercase using the "guiw" or "viwu" |
| 3176 |
command. |
| 3177 |
|
| 3178 |
You can switch the case (upper case to lower case and vice versa) of the |
| 3179 |
word under the cursor using the "viw~" or "g~iw" command. |
| 3180 |
|
| 3181 |
You can use the "gUgU" command to change the current line to uppercase and |
| 3182 |
the "gugu" command to change the current line to lowercase. |
| 3183 |
|
| 3184 |
You can use the "g~g~" command to switch the case of the current line. You |
| 3185 |
can use the "g~{motion}" or "{Visual}~" commands to switch the case of a |
| 3186 |
block of text. |
| 3187 |
|
| 3188 |
For more information, read |
| 3189 |
|
| 3190 |
:help case |
| 3191 |
|
| 3192 |
|
| 3193 |
12.30. How do I enter ASCII characters that are not present in the |
| 3194 |
keyboard? |
| 3195 |
|
| 3196 |
You can enter ASCII characters that are not present in the keyboard by |
| 3197 |
pressing CTRL-V and then the ASCII character number. You can also use |
| 3198 |
digraphs to enter special ASCII characters. |
| 3199 |
|
| 3200 |
For more information, read |
| 3201 |
|
| 3202 |
:help i_CTRL-V_digit |
| 3203 |
:help digraphs |
| 3204 |
:help 45.5 |
| 3205 |
|
| 3206 |
|
| 3207 |
12.31. How do I replace non-printable characters in a file? |
| 3208 |
|
| 3209 |
To replace a non-printable character, you have to first determine the ASCII |
| 3210 |
value for the character. You can use the ":ascii" ex command or the "ga" |
| 3211 |
normal-mode command to display the ASCII value of the character under the |
| 3212 |
cursor. |
| 3213 |
|
| 3214 |
You can enter the non-printable character by entering CTRL-V followed by |
| 3215 |
the decimal number 1-255 (with no leading zero), or by x and a hex number |
| 3216 |
00-FF, or by an octal number 0-0377 (with leading zero), or by u and a hex |
| 3217 |
number 0-FFFF, or by U and a hex number 0-7FFFFFFF |
| 3218 |
|
| 3219 |
Another alternative is to use the ":digraphs" ex command to display the |
| 3220 |
digraphs for all characters, together with their value in decimal and |
| 3221 |
alpha. You can enter a non-printable character by entering CTRL-K followed |
| 3222 |
by two alphanumeric characters (a digraph). |
| 3223 |
|
| 3224 |
For more information, read |
| 3225 |
|
| 3226 |
:help :ascii |
| 3227 |
:help i_CTRL-V |
| 3228 |
:help i_CTRL-V_digit |
| 3229 |
:help :digraphs |
| 3230 |
|
| 3231 |
|
| 3232 |
12.32. How do I remove duplicate lines from a buffer? |
| 3233 |
|
| 3234 |
You can use the following user-defined command to remove all the duplicate |
| 3235 |
lines from a buffer: |
| 3236 |
|
| 3237 |
:command -range=% Uniq <line1>,<line2>g/^\%<<line2>l\(.*\)\n\1$/d |
| 3238 |
|
| 3239 |
Add the above command to your .vimrc file and invoke ":Uniq" to remove all |
| 3240 |
the duplicate lines. |
| 3241 |
|
| 3242 |
|
| 3243 |
12.33. How do I prefix all the lines in a file with the corresponding line |
| 3244 |
numbers? |
| 3245 |
|
| 3246 |
You can prefix the lines with the corresponding line number in several |
| 3247 |
ways. Some of them are listed below: |
| 3248 |
|
| 3249 |
:%s/^/\=line('.'). ' ' |
| 3250 |
:%s/^/\=strpart(line(".")." ", 0, 5) |
| 3251 |
:g/^/exec "s/^/".strpart(line(".")." ", 0, 4) |
| 3252 |
|
| 3253 |
For more information, read |
| 3254 |
|
| 3255 |
:help sub-replace-special |
| 3256 |
:help line() |
| 3257 |
:help expr6 |
| 3258 |
:help strpart() |
| 3259 |
:help :execute |
| 3260 |
:help :global |
| 3261 |
|
| 3262 |
|
| 3263 |
12.34. How do I exchange (swap) two characters or words or lines? |
| 3264 |
|
| 3265 |
You can exchange two characters with the "xp" command sequence. The 'x' |
| 3266 |
will delete the character under the cursor and 'p' will paste the just |
| 3267 |
deleted character after the character under the cursor. This will result |
| 3268 |
in exchanging the two characters. |
| 3269 |
|
| 3270 |
You can exchange two words with the "deep" command sequence (start with the |
| 3271 |
cursor in the blank space before the first word). |
| 3272 |
|
| 3273 |
You can exchange two lines with the "ddp" command sequence. The 'dd' will |
| 3274 |
delete the current line and 'p' will paste the just deleted line after the |
| 3275 |
current line. This will result in exchanging the two lines. |
| 3276 |
|
| 3277 |
All of the above operations will change the " unnamed register. |
| 3278 |
|
| 3279 |
You can use the ":m +" ex command to exchange two lines without changing the |
| 3280 |
unnamed register. |
| 3281 |
|
| 3282 |
For more information, read |
| 3283 |
|
| 3284 |
:help x |
| 3285 |
:help p |
| 3286 |
:help dd |
| 3287 |
:help d |
| 3288 |
:help e |
| 3289 |
:help linewise-register |
| 3290 |
:help quotequote |
| 3291 |
:help :move |
| 3292 |
|
| 3293 |
|
| 3294 |
12.35. How do I change the characters used as word delimiters? |
| 3295 |
|
| 3296 |
Vim uses the characters specified by the 'iskeyword' option as word |
| 3297 |
delimitiers. The default setting for this option is "@,48-57,_,192-255". |
| 3298 |
|
| 3299 |
For example, to add ':' as a word delimitier, you can use |
| 3300 |
|
| 3301 |
:set iskeyword+=: |
| 3302 |
|
| 3303 |
To remove '_' as a word delimitier, you can use |
| 3304 |
|
| 3305 |
:set iskeyword-=_ |
| 3306 |
|
| 3307 |
For more information, read |
| 3308 |
|
| 3309 |
:help 'iskeyword' |
| 3310 |
:help word |
| 3311 |
|
| 3312 |
|
| 3313 |
============================================================================= |
| 3314 |
|
| 3315 |
SECTION 13 - COMPLETION IN INSERT MODE |
| 3316 |
|
| 3317 |
|
| 3318 |
13.1. How do I complete words or lines in insert mode? |
| 3319 |
|
| 3320 |
In insert mode, you can complete words using the CTRL-P and CTRL-N keys. |
| 3321 |
The CTRL-N command searches forward for the next matching keyword. |
| 3322 |
The CTRL-P command searches backwards for the next matching keyword. |
| 3323 |
|
| 3324 |
In insert mode, you can use the CTRL-X CTRL-L command sequence to complete |
| 3325 |
lines that starts with the same characters as in the current line before |
| 3326 |
the cursor. To get the next matching line, press the CTRL-P or CTRL-N keys. |
| 3327 |
There are a lot of other keys/ways available to complete words in insert |
| 3328 |
mode. |
| 3329 |
|
| 3330 |
Vim supports completion of the following items: |
| 3331 |
|
| 3332 |
CTRL-X CTRL-F file names |
| 3333 |
CTRL-X CTRL-L whole lines |
| 3334 |
CTRL-X CTRL-D macro definitions (also in included files) |
| 3335 |
CTRL-X CTRL-I current and included files |
| 3336 |
CTRL-X CTRL-K words from a dictionary |
| 3337 |
CTRL-X CTRL-T words from a thesaurus |
| 3338 |
CTRL-X CTRL-] tags |
| 3339 |
CTRL-X CTRL-V Vim command line |
| 3340 |
|
| 3341 |
For more information, read |
| 3342 |
|
| 3343 |
:help 24.3 |
| 3344 |
:help ins-completion |
| 3345 |
|
| 3346 |
|
| 3347 |
13.2. How do I complete file names in insert mode? |
| 3348 |
|
| 3349 |
In insert mode, you can use the CTRL-X CTRL-F command sequence to complete |
| 3350 |
filenames that start with the same characters as in the current line before |
| 3351 |
the cursor. |
| 3352 |
|
| 3353 |
For more information, read |
| 3354 |
|
| 3355 |
:help compl-filename |
| 3356 |
|
| 3357 |
|
| 3358 |
13.3. I am using CTRL-P/CTRL-N to complete words in insert mode. How do I |
| 3359 |
complete words that occur after the just completed word? |
| 3360 |
|
| 3361 |
You can use CTRL-X CTRL-N and CTRL-X CTRL-P keys to complete words that are |
| 3362 |
present after the just completed word. |
| 3363 |
|
| 3364 |
For more information, read |
| 3365 |
|
| 3366 |
:help i_CTRL-X_CTRL-P |
| 3367 |
:help i_CTRL-X_CTRL-N |
| 3368 |
:help ins-completion |
| 3369 |
|
| 3370 |
|
| 3371 |
============================================================================= |
| 3372 |
|
| 3373 |
SECTION 14 - TEXT FORMATTING |
| 3374 |
|
| 3375 |
|
| 3376 |
14.1. How do I format a text paragraph so that a new line is inserted at |
| 3377 |
the end of each wrapped line? |
| 3378 |
|
| 3379 |
You can use the 'gq' command to format a paragraph. This will format the |
| 3380 |
text according to the current 'textwidth' setting. |
| 3381 |
|
| 3382 |
Note that the gq operator can be used with a motion command to operate on a |
| 3383 |
range of text. For example: |
| 3384 |
|
| 3385 |
gqgq - Format the current line |
| 3386 |
gqap - Format current paragraph |
| 3387 |
gq3j - Format the current and the next 3 lines |
| 3388 |
|
| 3389 |
For more information, read |
| 3390 |
|
| 3391 |
:help gq |
| 3392 |
:help formatting |
| 3393 |
:help usr_25.txt |
| 3394 |
:help motion.txt |
| 3395 |
|
| 3396 |
|
| 3397 |
14.2. How do I format long lines in a file so that each line contains less |
| 3398 |
than 'n' characters? |
| 3399 |
|
| 3400 |
You can set the 'textwidth' option to control the number of characters that |
| 3401 |
can be present in a line. For example, to set the maximum width of a line |
| 3402 |
to 70 characters, you can use the following command: |
| 3403 |
|
| 3404 |
set textwidth=70 |
| 3405 |
|
| 3406 |
Now to break the long lines in a file to the length defined by the |
| 3407 |
'textwidth' option, you can use |
| 3408 |
|
| 3409 |
:g/./normal gqq |
| 3410 |
|
| 3411 |
For more information, read |
| 3412 |
|
| 3413 |
:help 'textwidth' |
| 3414 |
:help gq |
| 3415 |
|
| 3416 |
|
| 3417 |
14.3. How do I join short lines to form a paragraph? |
| 3418 |
|
| 3419 |
First, make sure the 'textwidth' option is set to a high value: |
| 3420 |
|
| 3421 |
:set textwidth=99999 |
| 3422 |
|
| 3423 |
Next, join the short lines to form a paragraph using the command: |
| 3424 |
|
| 3425 |
1GgqG |
| 3426 |
|
| 3427 |
The above command will operate on the entire file. To do the formatting on |
| 3428 |
all paragraphs in a specific range, use: |
| 3429 |
|
| 3430 |
:'a,'bg/\S/normal gq} |
| 3431 |
|
| 3432 |
For more information, read |
| 3433 |
|
| 3434 |
:help gq |
| 3435 |
:help G |
| 3436 |
:help gqq |
| 3437 |
|
| 3438 |
|
| 3439 |
14.4. How do I format bulleted and numbered lists? |
| 3440 |
|
| 3441 |
You can configure Vim to format bulleted and numbered lists using the |
| 3442 |
'formatoptions' option. For example, you can format the list of the |
| 3443 |
following format: |
| 3444 |
|
| 3445 |
- this is a test. this is a test. this is a test. this is a test. |
| 3446 |
this is a test. |
| 3447 |
|
| 3448 |
into this format: |
| 3449 |
|
| 3450 |
- this is a test. this is a test. this is a test. this is a test. |
| 3451 |
this is a test. |
| 3452 |
|
| 3453 |
You can use the 'n' flag in the 'formatoptions' to align the text. |
| 3454 |
|
| 3455 |
:set fo+=n |
| 3456 |
|
| 3457 |
With this option, when formatting text, Vim will recognize numbered lists. |
| 3458 |
For this option to work, the 'autoindent' option also must be set. |
| 3459 |
|
| 3460 |
For more information, read |
| 3461 |
|
| 3462 |
:help 'formatoptions' |
| 3463 |
:help fo-table |
| 3464 |
:help format-comments |
| 3465 |
|
| 3466 |
|
| 3467 |
14.5. How do I indent lines in insert mode? |
| 3468 |
|
| 3469 |
In insert mode, you can press the CTRL-T key to insert one shiftwidth of |
| 3470 |
indent at the start of the current line. In insert mode, you can use the |
| 3471 |
CTRL-D key to delete on shiftwidth of indent at the start of the current |
| 3472 |
line. You can also use the CTRL-O >> and CTRL-O << commands to indent the |
| 3473 |
current line in insert mode. |
| 3474 |
|
| 3475 |
For more information, read |
| 3476 |
|
| 3477 |
:help i_CTRL-T |
| 3478 |
:help i_CTRL-D |
| 3479 |
:help i_0_CTRL-D |
| 3480 |
:help i_CTRL-O |
| 3481 |
:help >> |
| 3482 |
:help << |
| 3483 |
|
| 3484 |
|
| 3485 |
14.6. How do I format/indent an entire file? |
| 3486 |
|
| 3487 |
You can format/indent an entire file using the gg=G command, where |
| 3488 |
|
| 3489 |
gg - Goto the beginning of the file |
| 3490 |
= - apply indentation |
| 3491 |
G - till end of file |
| 3492 |
|
| 3493 |
For more information, read |
| 3494 |
|
| 3495 |
:help gg |
| 3496 |
:help = |
| 3497 |
:help G |
| 3498 |
:help 'formatprg' |
| 3499 |
:help C-indenting |
| 3500 |
|
| 3501 |
|
| 3502 |
14.7. How do I increase or decrease the indentation of the current line? |
| 3503 |
|
| 3504 |
You can use the '>>' and '<<' commands to increase or decrease the |
| 3505 |
indentation of the current line. |
| 3506 |
|
| 3507 |
For more information, read |
| 3508 |
|
| 3509 |
:help shift-left-right |
| 3510 |
:help >> |
| 3511 |
:help << |
| 3512 |
:help 'shiftwidth' |
| 3513 |
|
| 3514 |
|
| 3515 |
14.8. How do I indent a block/group of lines? |
| 3516 |
|
| 3517 |
You can visually select the group of lines and press the > or < key to |
| 3518 |
indent/unindent the lines. You can also use the following ex-command to |
| 3519 |
indent the lines |
| 3520 |
|
| 3521 |
:10,20> |
| 3522 |
|
| 3523 |
For more information, read |
| 3524 |
|
| 3525 |
:help shift-left-right |
| 3526 |
:help v_> |
| 3527 |
:help v_< |
| 3528 |
:help :< |
| 3529 |
:help :> |
| 3530 |
|
| 3531 |
|
| 3532 |
14.9. When I indent lines using the > or < key, the standard 8-tabstops are |
| 3533 |
used instead of the current 'tabstop' setting. Why? |
| 3534 |
|
| 3535 |
The number of spaces used when lines are indented using the ">" operator is |
| 3536 |
controlled by the 'shiftwidth' option. The 'tabstop' setting is not used |
| 3537 |
for indentation. To change the amount of spaces used for indentation, use |
| 3538 |
the command: |
| 3539 |
|
| 3540 |
:set shiftwidth=4 |
| 3541 |
|
| 3542 |
For more information, read |
| 3543 |
|
| 3544 |
:help 'shiftwidth' |
| 3545 |
:help >> |
| 3546 |
:help 'softtabstop' |
| 3547 |
|
| 3548 |
|
| 3549 |
14.10. How do I turn off the automatic indentation of text? |
| 3550 |
|
| 3551 |
By default, the automatic indentation of text is not turned on. Check the |
| 3552 |
configuration files (.vimrc, .gvimrc) for settings related to indentation. |
| 3553 |
Make sure the ":filetype indent on" command is not present. If it is |
| 3554 |
present, remove it. Also, depending on your preference, you may also want |
| 3555 |
to check the value of the 'autoindent', 'smartindent', 'cindent' and |
| 3556 |
'indentexpr' options and turn them off as needed. |
| 3557 |
|
| 3558 |
For more information, read |
| 3559 |
|
| 3560 |
:help :filetype-indent-off |
| 3561 |
:help 'autoindent' |
| 3562 |
:help 'smartindent' |
| 3563 |
:help 'cindent' |
| 3564 |
:help 'indentexpr' |
| 3565 |
|
| 3566 |
|
| 3567 |
14.11. How do I configure Vim to automatically set the 'textwidth' option |
| 3568 |
to a particular value when I edit mails? |
| 3569 |
|
| 3570 |
You can use the 'FileType' autocommand to set the 'textwidth' option: |
| 3571 |
|
| 3572 |
autocmd FileType mail set tw=<your_value> |
| 3573 |
|
| 3574 |
For more information, read |
| 3575 |
|
| 3576 |
:help :autocmd |
| 3577 |
:help FileType |
| 3578 |
:help usr_43.txt |
| 3579 |
|
| 3580 |
|
| 3581 |
14.12. Is there a way to make Vim auto-magically break lines? |
| 3582 |
|
| 3583 |
Yes. Set the 'textwidth' option to the preferred length for a line. Then |
| 3584 |
Vim will auto-magically break the newly entered lines. For example: |
| 3585 |
|
| 3586 |
:set textwidth=75 |
| 3587 |
|
| 3588 |
For more information, read |
| 3589 |
|
| 3590 |
:help 'textwidth' |
| 3591 |
:help ins-textwidth |
| 3592 |
:help 'formatoptions' |
| 3593 |
:help fo-table |
| 3594 |
:help formatting |
| 3595 |
|
| 3596 |
|
| 3597 |
14.13. I am seeing a lot of ^M symbols in my file. I tried setting the |
| 3598 |
'fileformat' option to 'dos' and then 'unix' and then 'mac'. None of |
| 3599 |
these helped. How can I hide these symbols? |
| 3600 |
|
| 3601 |
When a file is loaded in Vim, the format of the file is determined as |
| 3602 |
below: |
| 3603 |
|
| 3604 |
- If all the lines end with a new line (<NL>), then the fileformat is |
| 3605 |
'unix'. |
| 3606 |
- If all the lines end with a carriage return (<CR>) followed by a new line |
| 3607 |
(<NL>), then the fileformat is 'dos'. |
| 3608 |
- If all the lines end with carriage return (<CR>), then the fileformat is |
| 3609 |
'mac'. |
| 3610 |
|
| 3611 |
If the file has some lines ending with <CR> and some lines ending with <CR> |
| 3612 |
followed by a <NL>, then the fileformat is set to 'unix'. |
| 3613 |
|
| 3614 |
You can change the format of the current file, by modifying the |
| 3615 |
'fileformat' option and then saving the file: |
| 3616 |
|
| 3617 |
:set fileformat=dos |
| 3618 |
:w |
| 3619 |
|
| 3620 |
To display the format of the current file, use |
| 3621 |
|
| 3622 |
:set fileformat? |
| 3623 |
|
| 3624 |
The above behavior is also controlled by the 'fileformats' option. You can |
| 3625 |
try the following commands: |
| 3626 |
|
| 3627 |
:set fileformats+=unix |
| 3628 |
:e <your_file> |
| 3629 |
:set fileformat=unix |
| 3630 |
:w |
| 3631 |
|
| 3632 |
To remove the carriage return (<CR>) character at the end of all the lines |
| 3633 |
in the current file, you can use the following command: |
| 3634 |
|
| 3635 |
:%s/\r$// |
| 3636 |
|
| 3637 |
To force Vim to use a particular file format, when editing a file, you can |
| 3638 |
use the following command: |
| 3639 |
|
| 3640 |
:e ++ff=dos filename |
| 3641 |
|
| 3642 |
For more information, read |
| 3643 |
|
| 3644 |
:help 'fileformats' |
| 3645 |
:help 'fileformat' |
| 3646 |
:help file-formats |
| 3647 |
:help DOS-format-write |
| 3648 |
:help Unix-format-write |
| 3649 |
:help Mac-format-write |
| 3650 |
:help dos-file-formats |
| 3651 |
:help 23.1 |
| 3652 |
:help ++ff |
| 3653 |
|
| 3654 |
|
| 3655 |
14.14. When I paste some text into a Vim buffer from another application, |
| 3656 |
the alignment (indentation) of the new text is messed up. How do I |
| 3657 |
fix this? |
| 3658 |
|
| 3659 |
The indentation of the text is messed up due to various Vim settings |
| 3660 |
related to indentation (like autoindent, smartindent, textwidth etc). |
| 3661 |
Before pasting text into Vim, you can set the 'paste' option: |
| 3662 |
|
| 3663 |
:set paste |
| 3664 |
|
| 3665 |
After pasting the text, you can turn off the option using: |
| 3666 |
|
| 3667 |
:set nopaste |
| 3668 |
|
| 3669 |
You can also toggle the paste option using: |
| 3670 |
|
| 3671 |
:set paste! |
| 3672 |
|
| 3673 |
If you can access the clipboard through the * register, then you can paste |
| 3674 |
the text without indentation using CTRL-R CTRL-O *. |
| 3675 |
|
| 3676 |
For more information, read |
| 3677 |
|
| 3678 |
:help 'paste' |
| 3679 |
:help 'pastetoggle' |
| 3680 |
:help i_CTRL-R_CTRL_O |
| 3681 |
:help clipboard |
| 3682 |
:help xterm-clipboard |
| 3683 |
:help gui-clipboard |
| 3684 |
|
| 3685 |
|
| 3686 |
14.15. When there is a very long wrapped line (wrap is "on") and a line |
| 3687 |
doesn't fit entirely on the screen it is not displayed at all. There |
| 3688 |
are blank lines beginning with '@' symbol instead of wrapped line. If |
| 3689 |
I scroll the screen to fit the line the '@' symbols disappear and the |
| 3690 |
line is displayed again. What Vim setting control this behavior? |
| 3691 |
|
| 3692 |
You can set the 'display' option to 'lastline' to display as much as |
| 3693 |
possible of the last line in a window instead of displaying the '@' |
| 3694 |
symbols. |
| 3695 |
|
| 3696 |
:set display=lastline |
| 3697 |
|
| 3698 |
For more information, read |
| 3699 |
|
| 3700 |
:help 'display' |
| 3701 |
|
| 3702 |
|
| 3703 |
14.16. How do I convert all the tab characters in a file to space |
| 3704 |
characters? |
| 3705 |
|
| 3706 |
You can use the ":retab" command to update all the tab characters in the |
| 3707 |
current file with the current setting of 'expandtab' and 'tabstop'. For |
| 3708 |
example, to convert all the tabs to white spaces, use |
| 3709 |
|
| 3710 |
:set expandtab |
| 3711 |
:retab |
| 3712 |
|
| 3713 |
For more information, read |
| 3714 |
|
| 3715 |
:help :retab |
| 3716 |
:help 'expandtab' |
| 3717 |
:help 'tabstop' |
| 3718 |
:help 25.3 |
| 3719 |
|
| 3720 |
|
| 3721 |
14.17. What Vim options can I use to edit text that will later go to a word |
| 3722 |
processor? |
| 3723 |
|
| 3724 |
You can set the following options to edit text that will later go into a |
| 3725 |
word processor: |
| 3726 |
|
| 3727 |
:set wrap |
| 3728 |
:set linebreak |
| 3729 |
:set textwidth=0 |
| 3730 |
:set showbreak=>>> |
| 3731 |
|
| 3732 |
You can use the 'gk' and 'gj' commands to move one screen line up and down. |
| 3733 |
For more information, read |
| 3734 |
|
| 3735 |
:help 'wrap' |
| 3736 |
:help 'linebreak' |
| 3737 |
:help 'textwidth' |
| 3738 |
:help 'showbreak' |
| 3739 |
:help gk |
| 3740 |
:help gj |
| 3741 |
|
| 3742 |
|
| 3743 |
14.18. How do I join lines without adding or removing any space characters? |
| 3744 |
|
| 3745 |
By default, when you join lines using the "J" or ":join" command, Vim will |
| 3746 |
replace the line break, leading white space and trailing white space with a |
| 3747 |
single space character. If there are space characters at the end of a line |
| 3748 |
or a line starts with the ')' character, then Vim will not add a space |
| 3749 |
character. |
| 3750 |
|
| 3751 |
To join lines without adding or removing any space characters, you can use |
| 3752 |
the gJ or ":join!" commands. |
| 3753 |
|
| 3754 |
For more information, read |
| 3755 |
|
| 3756 |
:help gJ |
| 3757 |
:help :join |
| 3758 |
:help J |
| 3759 |
:hep 10.5 |
| 3760 |
:help 'joinspaces' |
| 3761 |
:help 'cpoptions' |
| 3762 |
:help 'formatoptions' |
| 3763 |
|
| 3764 |
|
| 3765 |
============================================================================= |
| 3766 |
|
| 3767 |
SECTION 15 - VISUAL MODE |
| 3768 |
|
| 3769 |
|
| 3770 |
15.1. How do I do rectangular block copying? |
| 3771 |
|
| 3772 |
You can do rectangular block copying in Vim using the blockwise visual |
| 3773 |
mode. To start blockwise visual mode use the CTRL-V key. Move the cursor |
| 3774 |
using any of the motion commands and then use the y operator to yank to |
| 3775 |
visually selected text. |
| 3776 |
|
| 3777 |
If CTRL-V does not work as expected, it may have been remapped to CTRL-Q by |
| 3778 |
the mswin.vim script which is often sourced by a vimrc on Windows machines |
| 3779 |
to mimic some common short cuts from other programs. |
| 3780 |
|
| 3781 |
For more information, read |
| 3782 |
|
| 3783 |
:help 04.4 |
| 3784 |
:help blockwise-visual |
| 3785 |
:help visual-mode |
| 3786 |
:help Q_vi |
| 3787 |
|
| 3788 |
|
| 3789 |
15.2. How do I delete or change a column of text in a file? |
| 3790 |
|
| 3791 |
You can use the Vim block-wise visual mode to select the column of text and |
| 3792 |
apply an operator (delete, change, copy, etc) on it. |
| 3793 |
|
| 3794 |
For more information, read |
| 3795 |
|
| 3796 |
:help visual-block |
| 3797 |
:help visual-operators |
| 3798 |
|
| 3799 |
|
| 3800 |
15.3. How do I apply an ex-command on a set of visually selected lines? |
| 3801 |
|
| 3802 |
When you select a range of lines in visual mode, the < register is set to |
| 3803 |
the start of the visual region and the > register is set to the end of the |
| 3804 |
visual region. You can use these registers to specify the range for an ex |
| 3805 |
command. After visually selecting the lines, press ":" to go to the command |
| 3806 |
mode. Vim will automatically insert the visual range '<,'>. You can run |
| 3807 |
any ex-command on the visual range. |
| 3808 |
|
| 3809 |
For more information, read |
| 3810 |
|
| 3811 |
:help v_: |
| 3812 |
:help '< |
| 3813 |
:help '> |
| 3814 |
|
| 3815 |
|
| 3816 |
15.4. How do I execute an ex command on a column of text selected in Visual |
| 3817 |
block mode? |
| 3818 |
|
| 3819 |
All the ex commands operate on whole lines only. If you try to execute an |
| 3820 |
ex command on a column of text selected in visual block mode, Vim will |
| 3821 |
operate on all the selected lines (instead of the selected columns). You |
| 3822 |
can use the vis.vim plugin script from http://vim.sourceforge.net scripts |
| 3823 |
archive to do this. |
| 3824 |
|
| 3825 |
For more information, read |
| 3826 |
|
| 3827 |
:help cmdline-ranges |
| 3828 |
:help 10.3 |
| 3829 |
:help cmdline-lines |
| 3830 |
|
| 3831 |
|
| 3832 |
15.5. How do I select the entire file in visual mode? |
| 3833 |
|
| 3834 |
You can select the entire file in visual mode using ggVG. |
| 3835 |
|
| 3836 |
gg - go to the beginning of the file. |
| 3837 |
V - Start linewise visual mode |
| 3838 |
G - goto the end of the file. |
| 3839 |
|
| 3840 |
For more information, read |
| 3841 |
|
| 3842 |
:help gg |
| 3843 |
:help linewise-visual |
| 3844 |
:help G |
| 3845 |
|
| 3846 |
|
| 3847 |
15.6. When I visually select a set of lines and press the > key to indent |
| 3848 |
the selected lines, the visual mode ends. How can I reselect the |
| 3849 |
region for further operation? (or) How do I re-select the last |
| 3850 |
selected visual area again? |
| 3851 |
|
| 3852 |
You can use the 'gv' command to reselect the last selected visual area. You |
| 3853 |
can also use the marks '< and '> to jump to the beginning or the end of the |
| 3854 |
last selected visual area. |
| 3855 |
|
| 3856 |
For more information, read |
| 3857 |
|
| 3858 |
:help gv |
| 3859 |
:help '< |
| 3860 |
:help '> |
| 3861 |
|
| 3862 |
|
| 3863 |
15.7. How do I jump to the beginning/end of a visually selected region? |
| 3864 |
|
| 3865 |
You can use the 'o' command to jump to the beginning/end of a visually |
| 3866 |
selected region. |
| 3867 |
|
| 3868 |
For more information, read |
| 3869 |
|
| 3870 |
:help v_o |
| 3871 |
|
| 3872 |
|
| 3873 |
15.8. When I select text with mouse and then press : to enter an ex |
| 3874 |
command, the selected text is replaced with the : character. How do I |
| 3875 |
execute an ex command on a text selected using the mouse similar to |
| 3876 |
the text selected using the visual mode? |
| 3877 |
|
| 3878 |
This will happen if you have configured Vim to use select mode instead of |
| 3879 |
Visual mode by setting the 'selectmode' option. Check the value of this |
| 3880 |
option: |
| 3881 |
|
| 3882 |
:set selectmode? |
| 3883 |
|
| 3884 |
This mode is known as selectmode and is similar to the visual mode. This |
| 3885 |
option is also automatically set when you use the "behave mswin" command. |
| 3886 |
Select mode looks like visual mode, but it is similar to the selection mode |
| 3887 |
in MS-Windows. |
| 3888 |
|
| 3889 |
For more information, read |
| 3890 |
|
| 3891 |
:help Select-mode |
| 3892 |
:help 'selectmode' |
| 3893 |
:help 9.4 |
| 3894 |
:help :behave |
| 3895 |
|
| 3896 |
|
| 3897 |
15.9. When I select a block of text using the mouse, Vim goes into |
| 3898 |
selection mode instead of Visual mode. Why? |
| 3899 |
|
| 3900 |
The 'selectmode' option controls whether Select mode will be started when |
| 3901 |
selecting a block of text using the mouse. To start Visual mode when |
| 3902 |
selecting text using mouse, remove the 'mouse' value from the 'selectmode' |
| 3903 |
option: |
| 3904 |
|
| 3905 |
:set selectmode-=mouse |
| 3906 |
|
| 3907 |
Note that by default, the 'selectmode' option will be set to empty, so that |
| 3908 |
always visual mode is used. |
| 3909 |
|
| 3910 |
For more information, read |
| 3911 |
|
| 3912 |
:help 'selectmode' |
| 3913 |
:help Select-mode |
| 3914 |
:help :behave |
| 3915 |
|
| 3916 |
|
| 3917 |
15.10. How do I visually select the last copy/pasted text? |
| 3918 |
|
| 3919 |
You can use the '[ and '] marks to visually select the last copy/pasted |
| 3920 |
text. The '[ mark is set to the beginning of the last changed/yanked text |
| 3921 |
and the '] mark is set to the end of the last changed/yanked text. To |
| 3922 |
visually select this block of text use the command '[v'] |
| 3923 |
|
| 3924 |
For more information, read |
| 3925 |
|
| 3926 |
:help '[ |
| 3927 |
:help '] |
| 3928 |
:help `a |
| 3929 |
:help v |
| 3930 |
|
| 3931 |
|
| 3932 |
============================================================================= |
| 3933 |
|
| 3934 |
SECTION 16 - COMMAND-LINE MODE |
| 3935 |
|
| 3936 |
|
| 3937 |
16.1. How do I use the name of the current file in the command mode or an |
| 3938 |
ex command line? |
| 3939 |
|
| 3940 |
In the command line, the '%' character represents the name of the current |
| 3941 |
file. In some commands, you have to use expand("%") to get the filename: |
| 3942 |
|
| 3943 |
:!perl % |
| 3944 |
|
| 3945 |
For more information, read |
| 3946 |
|
| 3947 |
:help :_% |
| 3948 |
:help cmdline-special |
| 3949 |
:help expand() |
| 3950 |
|
| 3951 |
|
| 3952 |
16.2. How do I edit the text in the Vim command-line effectively? |
| 3953 |
|
| 3954 |
You can use the command-line window for editing Vim command-line text. To |
| 3955 |
open the Vim command-line window use the "q:" command in normal mode. In |
| 3956 |
command-line mode, use the CTRL-F key. In this window, the command line |
| 3957 |
history will be displayed. You can use normal Vim keys/commands to edit any |
| 3958 |
previous/new command line. To execute a command line, press the |
| 3959 |
enter/return key. |
| 3960 |
|
| 3961 |
In a similar vain, the search history can be edited with "q/" and "q?" |
| 3962 |
commands. |
| 3963 |
|
| 3964 |
For more information, read |
| 3965 |
|
| 3966 |
:help cmdline-window |
| 3967 |
|
| 3968 |
|
| 3969 |
16.3. How do I switch from Vi mode to Ex mode? |
| 3970 |
|
| 3971 |
You can use the Q command to switch from Vi mode to Ex mode. To switch from |
| 3972 |
Ex mode back to the Vi mode, use the :vi command. |
| 3973 |
|
| 3974 |
For more information, read |
| 3975 |
|
| 3976 |
:help Q |
| 3977 |
:help gQ |
| 3978 |
:help Ex-mode |
| 3979 |
:help :vi |
| 3980 |
|
| 3981 |
|
| 3982 |
16.4. How do I copy the output from an ex-command into a buffer? |
| 3983 |
|
| 3984 |
To copy the output from an ex-command into a buffer, you have to first get |
| 3985 |
the command output into a register. You can use the ":redir" command to get |
| 3986 |
the output into a register. For example, |
| 3987 |
|
| 3988 |
:redir @a |
| 3989 |
:g/HelloWord/p |
| 3990 |
:redir END |
| 3991 |
|
| 3992 |
Now the register 'a' will contain the output from the ex command |
| 3993 |
"g/HelloWord/p". Now you can paste the contents of the register 'a' into a |
| 3994 |
buffer. You can also send or append the output of an ex-command into a file |
| 3995 |
using the 'redir' command. |
| 3996 |
|
| 3997 |
You can prefix the ":global" command with ":silent", to avoid having the |
| 3998 |
lines printed to the screen. |
| 3999 |
|
| 4000 |
To redirect the output from an ex-command to a file, you can use the |
| 4001 |
following set of commands: |
| 4002 |
|
| 4003 |
:redir > myfile |
| 4004 |
:g/HelloWord/p |
| 4005 |
:redir END |
| 4006 |
|
| 4007 |
For more information, read |
| 4008 |
|
| 4009 |
:help :redir |
| 4010 |
:help :silent |
| 4011 |
|
| 4012 |
|
| 4013 |
16.5. When I press the tab key to complete the name of a file in the |
| 4014 |
command mode, if there are more than one matching file names, then |
| 4015 |
Vim completes the first matching file name and displays a list of all |
| 4016 |
matching filenames. How do I configure Vim to only display the list |
| 4017 |
of all the matching filenames and not complete the first one? |
| 4018 |
|
| 4019 |
You can modify the 'wildmode' option to configure the way Vim completes |
| 4020 |
filenames in the command mode. In this case, you can set the 'wildmode' |
| 4021 |
option to 'list': |
| 4022 |
|
| 4023 |
:set wildmode=list |
| 4024 |
|
| 4025 |
For more information, read |
| 4026 |
|
| 4027 |
:help 'wildmode' |
| 4028 |
|
| 4029 |
|
| 4030 |
16.6. How do I copy text from a buffer to the command line and from the |
| 4031 |
command line to a buffer? |
| 4032 |
|
| 4033 |
To copy text from a buffer to the command line, after yanking the text from |
| 4034 |
the buffer, use Ctrl-R 0 in the command line to paste the text. You can |
| 4035 |
also yank the text to a specific register and use CTRL-R <register> to |
| 4036 |
paste the text to the command line. You can use CTRL-R CTRL-W to paste the |
| 4037 |
word under the cursor in the command line. |
| 4038 |
|
| 4039 |
To copy text from the command line into a buffer, you can paste the |
| 4040 |
contents of the : register using the ":p command. The most recently |
| 4041 |
executed command line is stored in the : register. |
| 4042 |
|
| 4043 |
Another approach for copying and pasting text to and from the command line |
| 4044 |
is to open the command line window using q: from normal mode or CTRL-F from |
| 4045 |
the command-line mode. In the command line window you can use all the Vim |
| 4046 |
commands to edit the command line. |
| 4047 |
|
| 4048 |
For more information, read |
| 4049 |
|
| 4050 |
:help c_CTRL-R |
| 4051 |
:help quote_: |
| 4052 |
:help cmdline-window |
| 4053 |
|
| 4054 |
|
| 4055 |
16.7. How do I put a command onto the command history without executing it? |
| 4056 |
|
| 4057 |
To put a command onto the command history without executing it, press the |
| 4058 |
<Esc> key to cancel the command. |
| 4059 |
|
| 4060 |
For more information, read |
| 4061 |
|
| 4062 |
:help c_<Esc> |
| 4063 |
|
| 4064 |
|
| 4065 |
16.8. How do I increase the height of the command-line? |
| 4066 |
|
| 4067 |
You can increase the height of the command-line by changing the 'cmdheight' |
| 4068 |
option: |
| 4069 |
|
| 4070 |
:set cmdheight=2 |
| 4071 |
|
| 4072 |
For more information, read |
| 4073 |
|
| 4074 |
:help 'cmdheight' |
| 4075 |
:help hit-enter |
| 4076 |
:help 05.7 |
| 4077 |
|
| 4078 |
|
| 4079 |
============================================================================= |
| 4080 |
|
| 4081 |
SECTION 17 - VIMINFO |
| 4082 |
|
| 4083 |
|
| 4084 |
17.1. When I invoke Vim, I get error messages about illegal characters in |
| 4085 |
the viminfo file. What should I do to get rid of these messages? |
| 4086 |
|
| 4087 |
You can remove the $HOME/.viminfo or the $HOME/_viminfo file to get rid of |
| 4088 |
these error messages. |
| 4089 |
|
| 4090 |
For more information, read |
| 4091 |
|
| 4092 |
:help viminfo-errors |
| 4093 |
:help viminfo-file-name |
| 4094 |
:help viminfo |
| 4095 |
:help 21.3 |
| 4096 |
|
| 4097 |
|
| 4098 |
17.2. How do I disable the viminfo feature? |
| 4099 |
|
| 4100 |
By default, the viminfo feature is disabled. If the viminfo feature is |
| 4101 |
enabled by a system-wide vimrc file, then you can disable the viminfo |
| 4102 |
feature by setting the 'viminfo' option to an empty string in your local |
| 4103 |
.vimrc file: |
| 4104 |
|
| 4105 |
:set viminfo="" |
| 4106 |
|
| 4107 |
For more information, read |
| 4108 |
|
| 4109 |
:help 'viminfo' |
| 4110 |
|
| 4111 |
|
| 4112 |
17.3. How do I save and use Vim marks across Vim sessions? |
| 4113 |
|
| 4114 |
You can save and restore Vim marks across Vim sessions using the viminfo |
| 4115 |
file. To use the viminfo file, make sure the 'viminfo' option is not empty. |
| 4116 |
To save and restore Vim marks, the 'viminfo' option should not contain the |
| 4117 |
'f' flag or should have a value greater than zero for the 'f' option. |
| 4118 |
|
| 4119 |
For more information, read |
| 4120 |
|
| 4121 |
:help 21.3 |
| 4122 |
:help viminfo |
| 4123 |
:help 'viminfo' |
| 4124 |
:help :wviminfo |
| 4125 |
:help :rviminfo |
| 4126 |
|
| 4127 |
|
| 4128 |
============================================================================= |
| 4129 |
|
| 4130 |
SECTION 18 - REMOTE EDITING |
| 4131 |
|
| 4132 |
|
| 4133 |
18.1. How do I open a file with existing instance of gvim? What happened to |
| 4134 |
the Vim 5.x OpenWithVim.exe and SendToVim.exe files? |
| 4135 |
|
| 4136 |
Starting with Vim6, the OLE version of OpenWithVim.exe and SendToVim.exe |
| 4137 |
Vim utilities are replaced by the new client-server feature. To open the |
| 4138 |
file j.txt with an existing instance of Gvim (MyVim), use: |
| 4139 |
|
| 4140 |
$ gvim --servername MyVim --remote-silent j.txt |
| 4141 |
|
| 4142 |
To list the server names of all the currently running Vim instances, use |
| 4143 |
|
| 4144 |
$ vim --serverlist |
| 4145 |
|
| 4146 |
To get more information about client-server feature, read |
| 4147 |
|
| 4148 |
:help client-server |
| 4149 |
|
| 4150 |
|
| 4151 |
18.2. How do I send a command to a Vim server to write all buffers to disk? |
| 4152 |
|
| 4153 |
You can use the Vim remote server functionality to do this: |
| 4154 |
|
| 4155 |
$ gvim --servername myVIM --remote-send "<C-\><C-N>:wall<CR>" |
| 4156 |
|
| 4157 |
For more information, read |
| 4158 |
|
| 4159 |
:help client-server |
| 4160 |
:help CTRL-\_CTRL-N |
| 4161 |
:help :wall |
| 4162 |
|
| 4163 |
|
| 4164 |
18.3. Where can I get the documentation about the Vim remote server |
| 4165 |
functionality? |
| 4166 |
|
| 4167 |
You can get more information about the Vim remote server functionality by |
| 4168 |
reading |
| 4169 |
|
| 4170 |
:help client-server |
| 4171 |
|
| 4172 |
|
| 4173 |
============================================================================= |
| 4174 |
|
| 4175 |
SECTION 19 - OPTIONS |
| 4176 |
|
| 4177 |
|
| 4178 |
19.1. How do I configure Vim in a simple way? |
| 4179 |
|
| 4180 |
You can use the ":options" command to open the Vim option window: |
| 4181 |
|
| 4182 |
:options |
| 4183 |
|
| 4184 |
This window can be used for viewing and setting all the options. |
| 4185 |
|
| 4186 |
For more information, read |
| 4187 |
|
| 4188 |
:help :options |
| 4189 |
|
| 4190 |
|
| 4191 |
19.2. How do I toggle the value of an option? |
| 4192 |
|
| 4193 |
You can prefix the option with "inv" to toggle the value of the option: |
| 4194 |
|
| 4195 |
:set invignorecase |
| 4196 |
:set invhlsearch |
| 4197 |
|
| 4198 |
You can also suffix the option with "!" to toggle the value: |
| 4199 |
|
| 4200 |
:set ignorecase! |
| 4201 |
:set hlsearch! |
| 4202 |
|
| 4203 |
For more information, read |
| 4204 |
|
| 4205 |
:help set-option |
| 4206 |
|
| 4207 |
|
| 4208 |
19.3. How do I set an option that affects only the current buffer/window? |
| 4209 |
|
| 4210 |
Some of the Vim options can have a local or global value. A local value |
| 4211 |
applies only to a specific buffer or window. A global value applies to all |
| 4212 |
the buffers or windows. |
| 4213 |
|
| 4214 |
When a Vim option is modified using the ":set" command, both the global and |
| 4215 |
local values for the option are changed. You can use the ":setlocal" |
| 4216 |
command to modify only the local value for the option and the ":setglobal" |
| 4217 |
command to modify only the global value. |
| 4218 |
|
| 4219 |
You can use the ":setlocal" command to set an option that will affect only |
| 4220 |
the current file/buffer: |
| 4221 |
|
| 4222 |
:setlocal textwidth=70 |
| 4223 |
|
| 4224 |
Note that not all options can have a local value. You can use ":setlocal" |
| 4225 |
command to set an option locally to a buffer/window only if the option is |
| 4226 |
allowed to have a local value. |
| 4227 |
|
| 4228 |
You can also use the following command to set a option locally: |
| 4229 |
|
| 4230 |
:let &l:{option-name} = <value> |
| 4231 |
|
| 4232 |
For more information, read |
| 4233 |
|
| 4234 |
:help :setlocal |
| 4235 |
:help local-options |
| 4236 |
|
| 4237 |
|
| 4238 |
19.4. How do I use space characters for a Vim option value? |
| 4239 |
|
| 4240 |
To use space characters in a Vim option value, you have to escape the space |
| 4241 |
character. For example: |
| 4242 |
|
| 4243 |
:set tags=tags\ /usr/tags |
| 4244 |
|
| 4245 |
For more information, read |
| 4246 |
|
| 4247 |
:help option-backslash |
| 4248 |
|
| 4249 |
|
| 4250 |
19.5. Can I add (embed) Vim option settings to the contents of a file? |
| 4251 |
|
| 4252 |
You can use modelines to add Vim option settings to the contents of a file. |
| 4253 |
For example, in a C file, you can add the following line to the top or the |
| 4254 |
bottom of the file: |
| 4255 |
|
| 4256 |
/* vim:sw=4: */ |
| 4257 |
|
| 4258 |
This will set the 'shiftwidth' option to 4, when editing that C file. |
| 4259 |
For this to work, the 'modeline' option should be set. By default, the |
| 4260 |
'modeline' option is not set, for security reasons, in Debian. The |
| 4261 |
'modelines' settings specifies the number of lines that will be checked for |
| 4262 |
the Vim set commands. |
| 4263 |
|
| 4264 |
For more information, read |
| 4265 |
|
| 4266 |
:help 21.6 |
| 4267 |
:help modeline |
| 4268 |
:help auto-setting |
| 4269 |
:help 'modeline' |
| 4270 |
:help 'modelines' |
| 4271 |
|
| 4272 |
|
| 4273 |
19.6. How do I display the line numbers of all the lines in a file? |
| 4274 |
|
| 4275 |
You can set the 'number' option to display the line numbers for all the |
| 4276 |
lines. |
| 4277 |
|
| 4278 |
:set number |
| 4279 |
|
| 4280 |
For more information, read |
| 4281 |
|
| 4282 |
:help 'number' |
| 4283 |
|
| 4284 |
|
| 4285 |
19.7. How do I change the width of the line numbers displayed using the |
| 4286 |
"number" option? |
| 4287 |
|
| 4288 |
The width used for displaying the line numbers for the 'number' option is |
| 4289 |
hard-coded in Vim. It is not possible to change this width by setting some |
| 4290 |
option. |
| 4291 |
|
| 4292 |
The request and the patch to add an option to change the number of columns |
| 4293 |
used for the 'number' option is in the Vim todo list: |
| 4294 |
|
| 4295 |
"Add an option to set the width of the 'number' column. Eight |
| 4296 |
positions is often more than needed. Or adjust the width to the length |
| 4297 |
of the file? |
| 4298 |
Add patch that adds 'numberlen' option. (James Harvey) |
| 4299 |
Other patch with min and max from Emmanuel Renieris (2002 Jul 24) |
| 4300 |
Other patch without an option by Gilles Roy (2002 Jul 25)" |
| 4301 |
|
| 4302 |
|
| 4303 |
19.8. How do I display (view) all the invisible characters like space, tabs |
| 4304 |
and newlines in a file? |
| 4305 |
|
| 4306 |
You can set the 'list' option to see all the invisible characters in your |
| 4307 |
file. |
| 4308 |
|
| 4309 |
:set list |
| 4310 |
|
| 4311 |
With this option set, you can view space characters, tabs, newlines, |
| 4312 |
trailing space characters and wrapped lines. |
| 4313 |
|
| 4314 |
To not display the invisible characters (which is the default), you have to |
| 4315 |
reset the 'list' option: |
| 4316 |
|
| 4317 |
:set nolist |
| 4318 |
(or) |
| 4319 |
:set list! |
| 4320 |
|
| 4321 |
The ":set list!" command will toggle the current setting of the boolean |
| 4322 |
'list' option. |
| 4323 |
|
| 4324 |
You can modify the 'listchars' option to configure how and which invisible |
| 4325 |
characters are displayed. For example, with the following command all the |
| 4326 |
trailing space characters will be displayed with a '.' character. |
| 4327 |
|
| 4328 |
:set listchars=trail:. |
| 4329 |
|
| 4330 |
For more information, read |
| 4331 |
|
| 4332 |
:help 'listchars' |
| 4333 |
:help 'list' |
| 4334 |
|
| 4335 |
|
| 4336 |
19.9. How do I configure Vim to always display the current line and column |
| 4337 |
number? |
| 4338 |
|
| 4339 |
You can set the 'ruler' option to display current column and line number in |
| 4340 |
the status line: |
| 4341 |
|
| 4342 |
:set ruler |
| 4343 |
|
| 4344 |
For more information, read |
| 4345 |
|
| 4346 |
:help 'ruler' |
| 4347 |
|
| 4348 |
|
| 4349 |
19.10. How do I display the current Vim mode? |
| 4350 |
|
| 4351 |
You can set the 'showmode' option to display the current Vim mode. In |
| 4352 |
Insert, Replace and Visual modes, Vim will display the current mode on the |
| 4353 |
last line. |
| 4354 |
|
| 4355 |
:set showmode |
| 4356 |
|
| 4357 |
For more information, read |
| 4358 |
|
| 4359 |
:help 'showmode' |
| 4360 |
|
| 4361 |
|
| 4362 |
19.11. How do I configure Vim to show pending/partial commands on the |
| 4363 |
status line? |
| 4364 |
|
| 4365 |
You can set the 'showcmd' option to display pending/partial commands in the |
| 4366 |
status line: |
| 4367 |
|
| 4368 |
:set showcmd |
| 4369 |
|
| 4370 |
For more information, read |
| 4371 |
|
| 4372 |
:help 'showcmd' |
| 4373 |
|
| 4374 |
|
| 4375 |
19.12. How do I configure the Vim status line to display different |
| 4376 |
settings/values? |
| 4377 |
|
| 4378 |
You can set the 'statusline' option to display different values/settings in |
| 4379 |
the Vim status line. |
| 4380 |
|
| 4381 |
For more information, read |
| 4382 |
|
| 4383 |
:help 'statusline' |
| 4384 |
:help 'laststatus' |
| 4385 |
:help 'rulerformat' |
| 4386 |
:help 'ruler' |
| 4387 |
|
| 4388 |
|
| 4389 |
19.13. How do I configure Vim to display status line always? |
| 4390 |
|
| 4391 |
You can set the 'laststatus' option to 2 to display the status line always. |
| 4392 |
|
| 4393 |
:set laststatus=2 |
| 4394 |
|
| 4395 |
For more information, read |
| 4396 |
|
| 4397 |
:help 'laststatus' |
| 4398 |
|
| 4399 |
|
| 4400 |
19.14. How do I make a Vim setting persistent across different Vim |
| 4401 |
invocations/instances/sessions? |
| 4402 |
|
| 4403 |
To make a Vim option setting persistent across different Vim instances, add |
| 4404 |
your setting to the .vimrc or .gvimrc file. You can also use the ":mkvimrc" |
| 4405 |
command to generate a vimrc file for the current settings. |
| 4406 |
|
| 4407 |
For more information, read |
| 4408 |
|
| 4409 |
:help save-settings |
| 4410 |
:help vimrc |
| 4411 |
:help gvimrc |
| 4412 |
:help vimrc-intro |
| 4413 |
:help :mkvimrc |
| 4414 |
:help initialization |
| 4415 |
|
| 4416 |
|
| 4417 |
19.15. Why do I hear a beep (why does my window flash) about 1 second after |
| 4418 |
I hit the Escape key? |
| 4419 |
|
| 4420 |
This is normal behavior. If your window flashes, then you've got the visual |
| 4421 |
bell on. Otherwise, you should hear a beep. |
| 4422 |
|
| 4423 |
Vim needs a timeout to tell the difference between a simple escape and, |
| 4424 |
say, a cursor key sequence. When you press a key in normal mode (and even |
| 4425 |
in insert mode) and that key is the beginning of a mapping, Vim waits a |
| 4426 |
certain amount of time to see if the rest of the mapping sequence follows. |
| 4427 |
If the mapping sequence is completed before a given timeout period, the |
| 4428 |
mapping for that sequence of keys is applied. If you interrupt the mapping, |
| 4429 |
the normal actions associated with the keys are executed. |
| 4430 |
|
| 4431 |
For example, if you have a mapping defined as ":imap vvv Vim is great!!" |
| 4432 |
and you type "vvv" quickly, the "Vim is great!!" will be inserted into your |
| 4433 |
text. But if you type "vv v" then that is what will put into your text. |
| 4434 |
This is also true if you type "vvv" too slowly where "too slowly" is longer |
| 4435 |
than the value for the timeout option. Setting the timeout option to a |
| 4436 |
larger value can help alleviate problems that appear when using function |
| 4437 |
keys over a slow line. |
| 4438 |
|
| 4439 |
For more information, read |
| 4440 |
|
| 4441 |
:help ttimeout |
| 4442 |
|
| 4443 |
|
| 4444 |
19.16. How do I make the 'c' and 's' commands display a '$' instead of |
| 4445 |
deleting the characters I'm changing? |
| 4446 |
|
| 4447 |
To make the 'c' and 's' commands display a '$' instead of deleting the |
| 4448 |
characters, add the $ flag to the 'cpoptions' option: |
| 4449 |
|
| 4450 |
:set cpoptions+=$ |
| 4451 |
|
| 4452 |
For more information, read |
| 4453 |
|
| 4454 |
:help 'cpoptions' |
| 4455 |
|
| 4456 |
|
| 4457 |
19.17. How do I remove more than one flag using a single ":set" command |
| 4458 |
from a Vim option? |
| 4459 |
|
| 4460 |
You can remove more than one flag from a Vim option using a single ":set" |
| 4461 |
command, by specifying the flags in exactly the same order as they appear |
| 4462 |
in the option. For example, if you use the following command to remove the |
| 4463 |
't' and 'n' flags from the 'formatoptions' option: |
| 4464 |
|
| 4465 |
:set formatoptions-=tn |
| 4466 |
|
| 4467 |
The 't' and 'n' flags will be removed from the 'formatoptions' option, only |
| 4468 |
if the 'formatoptions' option contains these flags in this order: 'tn'. |
| 4469 |
Otherwise, it will not remove the flags. To avoid this problem, you can |
| 4470 |
remove the flags one by one: |
| 4471 |
|
| 4472 |
:set formatoptions-=t formatoptions-=n |
| 4473 |
|
| 4474 |
For more information, read |
| 4475 |
|
| 4476 |
:help :set-= |
| 4477 |
|
| 4478 |
|
| 4479 |
============================================================================= |
| 4480 |
|
| 4481 |
SECTION 20 - MAPPING KEYS |
| 4482 |
|
| 4483 |
|
| 4484 |
20.1. How do I know what a key is mapped to? |
| 4485 |
|
| 4486 |
To see what a key is mapped to, use the following commands: |
| 4487 |
|
| 4488 |
:map <key> |
| 4489 |
:map! <key> |
| 4490 |
|
| 4491 |
You can also check the mappings in a particular mode using one of the |
| 4492 |
":cmap", ":nmap", ":vmap", ":imap", ":omap", etc commands. |
| 4493 |
|
| 4494 |
For more information, read |
| 4495 |
|
| 4496 |
:help map-listing |
| 4497 |
:help map-overview |
| 4498 |
|
| 4499 |
|
| 4500 |
20.2. How do list all the user-defined key mappings? |
| 4501 |
|
| 4502 |
You can list all the user-defined key mappings using: |
| 4503 |
|
| 4504 |
:map |
| 4505 |
|
| 4506 |
For more information, read |
| 4507 |
|
| 4508 |
:help map-listing |
| 4509 |
|
| 4510 |
|
| 4511 |
20.3. How do I unmap a previously mapped key? |
| 4512 |
|
| 4513 |
You can unmap a previously mapped key using the ":unmap" command: |
| 4514 |
|
| 4515 |
:unmap <key> |
| 4516 |
:unmap! <key> |
| 4517 |
|
| 4518 |
For mode specific mappings, you can use one of the |
| 4519 |
":nunmap/:vunmap/:ounmap/:iunmap/:lunmap/:cunmap" commands. |
| 4520 |
|
| 4521 |
The following command will fail to unmap a buffer-local mapped key: |
| 4522 |
|
| 4523 |
:unmap <key> |
| 4524 |
|
| 4525 |
To unmap a buffer-local mapped key, you have to use the <buffer> keyword in |
| 4526 |
the unmap command: |
| 4527 |
|
| 4528 |
:unmap <buffer> <key> |
| 4529 |
:unmap! <buffer> <key> |
| 4530 |
|
| 4531 |
For more information, read |
| 4532 |
|
| 4533 |
:help :unmap |
| 4534 |
:help map-modes |
| 4535 |
:help map-local |
| 4536 |
:help 'mapleader' |
| 4537 |
|
| 4538 |
|
| 4539 |
20.4. I am not able to create a mapping for the <xxx> key. What is wrong? |
| 4540 |
|
| 4541 |
First make sure that the key is passed to Vim. In insert mode, press CTRL-V |
| 4542 |
followed by the desired key. You should see the keycode corresponding to |
| 4543 |
the key . If you do see the keycode, then you can create a mapping for the |
| 4544 |
key using the following command: |
| 4545 |
|
| 4546 |
:map <C-V><xxx> <your_command_to_be_mapped> |
| 4547 |
|
| 4548 |
For more information, read |
| 4549 |
|
| 4550 |
:help map-keys-fails |
| 4551 |
:help :map-special-keys |
| 4552 |
:help key-codes |
| 4553 |
|
| 4554 |
|
| 4555 |
20.5. How do I map the numeric keypad keys? |
| 4556 |
|
| 4557 |
First make sure that the numeric keypad keys are passed to Vim. Next, you |
| 4558 |
can use the following command to map the numeric keypad keys: |
| 4559 |
|
| 4560 |
:map <kSomething> <your_command> |
| 4561 |
|
| 4562 |
where, <kSomething> can be kHome, kEnd, kPageUp, kPageDown, kPlus, kMinus, |
| 4563 |
kDivide, kMultiply, kEnter, etc. |
| 4564 |
|
| 4565 |
For more information, read |
| 4566 |
|
| 4567 |
:help key-codes |
| 4568 |
:help terminal-options |
| 4569 |
|
| 4570 |
|
| 4571 |
20.6. How do I create a mapping that works only in visual mode? |
| 4572 |
|
| 4573 |
You can create mappings that work only in specific mode (normal, command, |
| 4574 |
insert, visual, etc). To create a mapping that works only in the visual |
| 4575 |
mode, use the ":vmap" command: |
| 4576 |
|
| 4577 |
:vmap <F3> <your mapping here> |
| 4578 |
|
| 4579 |
For more information, read |
| 4580 |
|
| 4581 |
:help :vmap |
| 4582 |
:help map-modes |
| 4583 |
:help 40.1 |
| 4584 |
|
| 4585 |
|
| 4586 |
20.7. In a Vim script, how do I know which keys to use for my mappings, so |
| 4587 |
that the mapped key will not collide with an already used key? |
| 4588 |
|
| 4589 |
Vim uses most of the keys in the keyboard. You can use the <leader> prefix |
| 4590 |
in maps to define keys which will not overlap with Vim keys. For example: |
| 4591 |
|
| 4592 |
:map <leader>S <C-W>s |
| 4593 |
:map <leader>j <C-W>j |
| 4594 |
:map <leader>k <C-W>k |
| 4595 |
|
| 4596 |
where by default <leader> gets substituted with a backslash (\), so the |
| 4597 |
user would enter |
| 4598 |
|
| 4599 |
\s |
| 4600 |
\j |
| 4601 |
\k |
| 4602 |
|
| 4603 |
to invoke the above map commands. The user can change the mapleader |
| 4604 |
variable to be whatever they wanted: |
| 4605 |
|
| 4606 |
:let mapleader = "," |
| 4607 |
|
| 4608 |
When writing a plugin or other script, more often than not, it is advisable |
| 4609 |
to use :noremap instead of :map to avoid side effects from user defined |
| 4610 |
mappings. |
| 4611 |
|
| 4612 |
For more information, read |
| 4613 |
|
| 4614 |
:help <Leader> |
| 4615 |
:help <LocalLeader> |
| 4616 |
:help write-plugin |
| 4617 |
|
| 4618 |
|
| 4619 |
20.8. How do I map the escape key? |
| 4620 |
|
| 4621 |
You can map the Escape key to some other key using the ":map" command. For |
| 4622 |
example, the following command maps the escape key to CTRL-O. |
| 4623 |
|
| 4624 |
:map <C-O> <Esc> |
| 4625 |
|
| 4626 |
|
| 4627 |
20.9. How do I map a key to perform nothing? |
| 4628 |
|
| 4629 |
You can map a key to <Nop> to perform nothing when the key is pressed. For |
| 4630 |
example, with the following mappings, the <F7> key will do nothing when |
| 4631 |
pressed. |
| 4632 |
|
| 4633 |
:map <F7> <Nop> |
| 4634 |
:map! <F7> <Nop> |
| 4635 |
|
| 4636 |
For more information, read |
| 4637 |
|
| 4638 |
:help <Nop> |
| 4639 |
:help :map |
| 4640 |
:help :map! |
| 4641 |
:help map-modes |
| 4642 |
|
| 4643 |
|
| 4644 |
20.10. I want to use the Tab key to indent a block of text and Shift-Tab |
| 4645 |
key to unindent a block of text. How do I map the keys to do this? |
| 4646 |
This behavior is similar to textpad, visual studio, etc. |
| 4647 |
|
| 4648 |
Use the following mapping: |
| 4649 |
|
| 4650 |
:inoremap <S-Tab> <C-O><LT><LT> |
| 4651 |
:nnoremap <Tab> >> |
| 4652 |
:nnoremap <S-Tab> <LT><LT> |
| 4653 |
:vnoremap <Tab> > |
| 4654 |
:vnoremap <S-Tab> <LT> |
| 4655 |
|
| 4656 |
Note that, the <S-Tab> mapping will work only if Vim receives the correct |
| 4657 |
key sequence. This is mostly the case with GUI Vim. |
| 4658 |
|
| 4659 |
For more information, read |
| 4660 |
|
| 4661 |
:help :inoremap |
| 4662 |
:help :nnoremap |
| 4663 |
:help :vnoremap |
| 4664 |
:help <S-Tab> |
| 4665 |
:help i_CTRL-O |
| 4666 |
:help >> |
| 4667 |
:help << |
| 4668 |
:help <LT> |
| 4669 |
|
| 4670 |
|
| 4671 |
20.11. In my mappings the special characters like <CR> are not recognized. |
| 4672 |
How can I configure Vim to recognize special characters? |
| 4673 |
|
| 4674 |
Check the value of the 'cpoptions' option: |
| 4675 |
|
| 4676 |
:set cpoptions? |
| 4677 |
|
| 4678 |
If this option contains the '<' flag, then special characters will not be |
| 4679 |
recognized in mappings. Remove the '<' flag from 'cpoptions' option: |
| 4680 |
|
| 4681 |
:set cpo-=< |
| 4682 |
|
| 4683 |
Also, check the value of the 'compatible' option: |
| 4684 |
|
| 4685 |
:se compatible? |
| 4686 |
|
| 4687 |
The 'compatible' option must be reset: |
| 4688 |
|
| 4689 |
:se nocompatible |
| 4690 |
|
| 4691 |
For more information, read |
| 4692 |
|
| 4693 |
:help 'cpoptions' |
| 4694 |
:help 'compatible' |
| 4695 |
|
| 4696 |
|
| 4697 |
20.12. How do I use the '|' to separate multiple commands in a map? |
| 4698 |
|
| 4699 |
You can escape the '|' character using backslash (\) to use '|' in a map. |
| 4700 |
|
| 4701 |
:map _l :!ls \| more<CR> |
| 4702 |
|
| 4703 |
You can also try the following command: |
| 4704 |
|
| 4705 |
:map _l :!ls <bar> more<CR> |
| 4706 |
|
| 4707 |
There are also other ways to do this. |
| 4708 |
|
| 4709 |
For more information, read |
| 4710 |
|
| 4711 |
:help map_bar |
| 4712 |
|
| 4713 |
|
| 4714 |
20.13. If I have a mapping/abbreviation whose ending is the beginning of |
| 4715 |
another mapping/abbreviation, how do I keep the first from expanding |
| 4716 |
into the second one? |
| 4717 |
|
| 4718 |
Instead of using the ":map lhs rhs" command, use the ":noremap lhs rhs" |
| 4719 |
command. For abbreviations, use "noreabbrev lhs rhs". The "nore" prefix |
| 4720 |
prevents the mapping or abbreviation from being expanded again. |
| 4721 |
|
| 4722 |
For more information, read |
| 4723 |
|
| 4724 |
:help :noremap |
| 4725 |
:help :noreabbrev |
| 4726 |
|
| 4727 |
|
| 4728 |
20.14. Why does it take a second or more for Vim to process a key, |
| 4729 |
sometimes when I press a key? |
| 4730 |
|
| 4731 |
Make sure you have not defined a mapping for this key using the following |
| 4732 |
command: |
| 4733 |
|
| 4734 |
:map <key> |
| 4735 |
|
| 4736 |
If a mapping is defined for this key and the mapped key contains more than |
| 4737 |
one character, then Vim will wait for the next character to be pressed to |
| 4738 |
determine whether it is the mapped key or not. For example, if you have |
| 4739 |
mapped "ab", then if you press "a", Vim will wait for the next key to be |
| 4740 |
pressed. If the next key is "b", Vim will execute the mapped sequence. |
| 4741 |
Otherwise, Vim will proceed with the normal processing of "a" followed by |
| 4742 |
the next key. If the 'timeout' option is set (which is the default), then |
| 4743 |
Vim will timeout after waiting for the period specified with the |
| 4744 |
'timeoutlen' option (default is 1 second). |
| 4745 |
|
| 4746 |
For more information, read |
| 4747 |
|
| 4748 |
:help map-typing |
| 4749 |
:help 'timeoutlen' |
| 4750 |
:help 'ttimeoutlen' |
| 4751 |
:help 'timeout' |
| 4752 |
:help 'ttimeout' |
| 4753 |
:help vt100-cursor-keys |
| 4754 |
:help slow-fast-terminal |
| 4755 |
|
| 4756 |
|
| 4757 |
20.15. How do I map a key to run an external command using a visually |
| 4758 |
selected text? |
| 4759 |
|
| 4760 |
You can the ":vmap" command to map a key in the visual mode. In the mapped |
| 4761 |
command sequence, you have to first yank the text. The yanked text is |
| 4762 |
available in the '"' register. Now, you can use the contents of this |
| 4763 |
register to run the external command. For example, to run the external |
| 4764 |
command "perldoc" on a visually selected text, you can use the following |
| 4765 |
mapping: |
| 4766 |
|
| 4767 |
:vmap <F7> y:!exec "!perldoc '" . @" . "'"<CR> |
| 4768 |
|
| 4769 |
If you want the mapping to work in the visual mode, but not with the |
| 4770 |
highlighted text, you can use the following command: |
| 4771 |
|
| 4772 |
:vmap <F7> :<C-U>!perldoc <cword><CR> |
| 4773 |
|
| 4774 |
The above mapping will use the word under the cursor instead of the |
| 4775 |
highlighted text. Note the use of the <C-U> before invoking the "perldoc" |
| 4776 |
external command. The <C-U> is used to erase the range of text selected in |
| 4777 |
the visual mode and displayed on the command line. If the visual range is |
| 4778 |
not removed using <C-U>, then the output from the external command will |
| 4779 |
replace the visually selected text. |
| 4780 |
|
| 4781 |
For more information, read |
| 4782 |
|
| 4783 |
:help :vmap |
| 4784 |
:help quote_quote |
| 4785 |
:help let-register |
| 4786 |
:help c_CTRL-U |
| 4787 |
:help :!cmd |
| 4788 |
|
| 4789 |
|
| 4790 |
20.16. How do I map the Ctrl-I key while still retaining the functionality |
| 4791 |
of the <Tab> key? |
| 4792 |
|
| 4793 |
The Ctrl-I key and the <Tab> key produce the same keycode, so Vim cannot |
| 4794 |
distinguish between the Ctrl-I and the <Tab> key. When you map the Ctrl-I |
| 4795 |
key, the <Tab> key is also mapped (and vice versa). The same restriction |
| 4796 |
applies for the Ctrl-[ key and the <Esc> key. |
| 4797 |
|
| 4798 |
For more information, read |
| 4799 |
|
| 4800 |
:help keycodes |
| 4801 |
|
| 4802 |
|
| 4803 |
============================================================================= |
| 4804 |
|
| 4805 |
SECTION 21 - ABBREVIATIONS |
| 4806 |
|
| 4807 |
|
| 4808 |
21.1. How do I auto correct misspelled words? |
| 4809 |
|
| 4810 |
You can auto correct misspelled words using abbreviations. For example, the |
| 4811 |
following abbreviation can be used to correct "teh" with "the": |
| 4812 |
|
| 4813 |
:abbreviate teh the |
| 4814 |
|
| 4815 |
Vim supports abbreviations in insert mode, replace mode and command-line |
| 4816 |
mode. |
| 4817 |
|
| 4818 |
For more information, read |
| 4819 |
|
| 4820 |
:help 24.7 |
| 4821 |
:help abbreviations |
| 4822 |
:help Q_ab |
| 4823 |
|
| 4824 |
|
| 4825 |
21.2. How do I create multi-line abbreviations? |
| 4826 |
|
| 4827 |
You can create multi-line abbreviations by embedding the "<CR>" |
| 4828 |
key code in the text: |
| 4829 |
|
| 4830 |
iabbrev #c --------------<CR>-- Date:<CR>--<CR>--------- |
| 4831 |
|
| 4832 |
With the above abbreviation, when you type #c, it will be expanded to |
| 4833 |
the following text: |
| 4834 |
|
| 4835 |
-------------- |
| 4836 |
-- Date: |
| 4837 |
-- |
| 4838 |
--------- |
| 4839 |
|
| 4840 |
For more information, read |
| 4841 |
|
| 4842 |
:help abbreviations |
| 4843 |
|
| 4844 |
|
| 4845 |
21.3. When my abbreviations are expanded, an additional space character is |
| 4846 |
added at the end of the expanded text. How do I avoid this character? |
| 4847 |
|
| 4848 |
To avoid an additional space character at the end of the expanded text, you |
| 4849 |
can expand the abbreviation by pressing the CTRL-] key. The abbreviation |
| 4850 |
will be expanded without adding a space character at the end. |
| 4851 |
|
| 4852 |
Another alternative is to use the following function and command: |
| 4853 |
|
| 4854 |
function! Eatchar(pat) |
| 4855 |
let c = nr2char(getchar()) |
| 4856 |
return (c =~ a:pat) ? '' : c |
| 4857 |
endfunction |
| 4858 |
command! -nargs=+ Iabbr execute "iabbr" <q-args> . "<C-R>=Eatchar('\\s')<CR>" |
| 4859 |
|
| 4860 |
Now, define your abbreviations using the new "Iabbr" command instead of the |
| 4861 |
builtin "iabbrev" command. With this command, after expanding the |
| 4862 |
abbreviated text, the next typed space character will be discarded. |
| 4863 |
|
| 4864 |
For more information, read |
| 4865 |
|
| 4866 |
:help abbreviations |
| 4867 |
|
| 4868 |
|
| 4869 |
21.4. How do I insert the current date/time stamp into the file? |
| 4870 |
|
| 4871 |
You can use the strftime() function to insert the current data/time stamp |
| 4872 |
in a file. For example, you can use the following abbreviation: |
| 4873 |
|
| 4874 |
iabbrev dts <C-R>=strftime("%y/%m/%d %H:%M")<CR> |
| 4875 |
|
| 4876 |
With this abbreviation, when you type dts in insert mode, it will be |
| 4877 |
expanded to the date/time stamp. |
| 4878 |
|
| 4879 |
Some other forms of the above abbreviation are listed below: |
| 4880 |
|
| 4881 |
iabbrev mdyl <C-R>=strftime("%a %d %b %Y")<CR> |
| 4882 |
iabbrev mdys <C-R>=strftime("%y%m%d")<CR> |
| 4883 |
iabbrev mdyc <C-R>=strftime("%c")<CR> |
| 4884 |
iabbrev hml <C-R>=strftime("%d/%m/%y %H:%M:%S")<CR> |
| 4885 |
iabbrev hms <C-R>=strftime("%H:%M:%S")<CR> |
| 4886 |
|
| 4887 |
For more information, read |
| 4888 |
|
| 4889 |
:help strftime() |
| 4890 |
:help i_CTRL-R |
| 4891 |
|
| 4892 |
|
| 4893 |
21.5. How do I prevent an abbreviation from expanding in insert mode? |
| 4894 |
|
| 4895 |
You can prevent an abbreviation from expanding in insert mode by typing |
| 4896 |
CTRL-V before the character after the abrreviated word. |
| 4897 |
|
| 4898 |
For more information, read |
| 4899 |
|
| 4900 |
:help abbreviations |
| 4901 |
|
| 4902 |
|
| 4903 |
============================================================================= |
| 4904 |
|
| 4905 |
SECTION 22 - RECORD AND PLAYBACK |
| 4906 |
|
| 4907 |
|
| 4908 |
22.1. How do I repeat an editing operation (insertion, deletion, paste, |
| 4909 |
etc)? |
| 4910 |
|
| 4911 |
You can repeat the last editing operation using the '.' command. This will |
| 4912 |
repeat the last simple change like a insert, delete, change, paste, etc. |
| 4913 |
|
| 4914 |
For more information, read |
| 4915 |
|
| 4916 |
:help 04.3 |
| 4917 |
:help single-repeat |
| 4918 |
:help Q_re |
| 4919 |
|
| 4920 |
|
| 4921 |
22.2. How I record and repeat a set of key sequences? |
| 4922 |
|
| 4923 |
You can use the 'q' command in normal mode to record a set of key sequences |
| 4924 |
and store it in a register. For example, in the normal mode you can press q |
| 4925 |
followed by a register name {0-9a-bA-Z"} to start the recording. To |
| 4926 |
end/stop the recording press q again. You can playback/repeat the recorded |
| 4927 |
key sequences by pressing @ followed by the register name. e.g. @a. |
| 4928 |
|
| 4929 |
Another approach is to start Vim with the "-w" command-line argument. |
| 4930 |
|
| 4931 |
$ vim -w <file_name> |
| 4932 |
|
| 4933 |
Vim will record all the characters typed in the session in the |
| 4934 |
specified file "file_name". You can use the recorded file with the "-s" |
| 4935 |
command line argument to play it back: |
| 4936 |
|
| 4937 |
$ vim -s <file_name> |
| 4938 |
|
| 4939 |
For more information, read |
| 4940 |
|
| 4941 |
:help 10.1 |
| 4942 |
:help recording |
| 4943 |
:help -w |
| 4944 |
:help -s |
| 4945 |
|
| 4946 |
|
| 4947 |
22.3. How do I edit/modify a recorded set of key sequences? |
| 4948 |
|
| 4949 |
The recorded key sequences are stored in a register. You can paste the |
| 4950 |
contents of the register into a Vim buffer, edit the pasted text and again |
| 4951 |
yank the text into the register. You can also use the ":let" command to |
| 4952 |
modify the register. For example: |
| 4953 |
|
| 4954 |
:let @a = "iHello World\<Esc>" |
| 4955 |
|
| 4956 |
For more information, read |
| 4957 |
|
| 4958 |
:help recording |
| 4959 |
:help 10.1 |
| 4960 |
:help let-register |
| 4961 |
:help <> |
| 4962 |
:help 'cpoptions' |
| 4963 |
|
| 4964 |
|
| 4965 |
22.4. How do I write recorded key sequences to a file? |
| 4966 |
|
| 4967 |
The recorded key sequences are stored in a register. You can paste the |
| 4968 |
contents of the register into a Vim buffer. Now you can save the buffer |
| 4969 |
into a file. You can also modify the pasted text and again yank into the |
| 4970 |
register to modify the recorded key sequence. For example, if you record a |
| 4971 |
set of key sequences using qa ..... q. The recorded key sequences are |
| 4972 |
stored in the register 'a'. You can paste the contents of register 'a' |
| 4973 |
using "ap. |
| 4974 |
|
| 4975 |
For more information, read |
| 4976 |
|
| 4977 |
:help recording |
| 4978 |
:help 10.1 |
| 4979 |
|
| 4980 |
|
| 4981 |
22.5. I am using register 0 to record my key sequences (i.e. q0 .... q). |
| 4982 |
In the recorded key sequences, I am yanking some text. After the |
| 4983 |
first replay of the recorded key sequence, I am no longer able to |
| 4984 |
play it back. |
| 4985 |
|
| 4986 |
Register 0 contains the text from the last yank operation. In your recorded |
| 4987 |
key sequence, when the yank is performed, register 0 is overwritten with |
| 4988 |
the yanked text. So your recording stored in register 0 is lost. You have |
| 4989 |
to use some other register. |
| 4990 |
|
| 4991 |
For more information, read |
| 4992 |
|
| 4993 |
:help registers |
| 4994 |
|
| 4995 |
|
| 4996 |
============================================================================= |
| 4997 |
|
| 4998 |
SECTION 23 - AUTOCOMMANDS |
| 4999 |
|
| 5000 |
|
| 5001 |
23.1. How do I execute a command when I try to modify a read-only file? |
| 5002 |
|
| 5003 |
You can use the FileChangedRO autocommand event to execute a command when a |
| 5004 |
read-only file modified. For example, you can use this event to checkout a |
| 5005 |
read-only file: |
| 5006 |
|
| 5007 |
:autocmd FileChangedRO * call MyCheckoutFunction() |
| 5008 |
|
| 5009 |
For more information, read |
| 5010 |
|
| 5011 |
:help FileChangedRO |
| 5012 |
|
| 5013 |
|
| 5014 |
23.2. How do I execute a command every time when entering a buffer? |
| 5015 |
|
| 5016 |
You can use the BufEnter autocommand event to execute a command every time |
| 5017 |
when entering a buffer. For example: |
| 5018 |
|
| 5019 |
:autocmd BufEnter *.c set formatoptions=croqt |
| 5020 |
|
| 5021 |
For more information, read |
| 5022 |
|
| 5023 |
:help BufEnter |
| 5024 |
|
| 5025 |
|
| 5026 |
23.3. How do I execute a command every time when entering a window? |
| 5027 |
|
| 5028 |
You can use the WinEnter autocommand event to execute a command every time |
| 5029 |
when entering a window. For example: |
| 5030 |
|
| 5031 |
:autocmd WinEnter *.c call MyFunction() |
| 5032 |
|
| 5033 |
For more information, read |
| 5034 |
|
| 5035 |
:help WinEnter |
| 5036 |
|
| 5037 |
|
| 5038 |
23.4. From an autocmd, how can I determine the name of the file or the |
| 5039 |
buffer number for which the autocommand is executed? |
| 5040 |
|
| 5041 |
You can use the special words <afile> or <abuf> in an autocmd to get the |
| 5042 |
name of the file or the buffer number for which the autocommand is |
| 5043 |
executed. |
| 5044 |
|
| 5045 |
For more information, read |
| 5046 |
|
| 5047 |
:help :<afile> |
| 5048 |
:help :<abuf> |
| 5049 |
:help :<amatch> |
| 5050 |
|
| 5051 |
|
| 5052 |
23.5. How do I automatically save all the changed buffers whenever Vim |
| 5053 |
loses focus? |
| 5054 |
|
| 5055 |
You can define an autocommand for the FocusLost event which will save all |
| 5056 |
the modified buffers whenever Vim loses focus: |
| 5057 |
|
| 5058 |
:autocmd FocusLost * wall |
| 5059 |
|
| 5060 |
For more information, read |
| 5061 |
|
| 5062 |
:help FocusLost |
| 5063 |
:help :wall |
| 5064 |
|
| 5065 |
|
| 5066 |
23.6. How do I execute/run a function when Vim exits to do some cleanup? |
| 5067 |
|
| 5068 |
You can use VimLeave autocmd event to execute a function just before Vim |
| 5069 |
exists. For example, |
| 5070 |
|
| 5071 |
:autocmd VimLeave * call MyCleanupFunction() |
| 5072 |
|
| 5073 |
For more information, read |
| 5074 |
|
| 5075 |
:help VimLeave |
| 5076 |
|
| 5077 |
|
| 5078 |
============================================================================= |
| 5079 |
|
| 5080 |
SECTION 24 - SYNTAX HIGHLIGHT |
| 5081 |
|
| 5082 |
|
| 5083 |
24.1. How do I turn off/on syntax highlighting? |
| 5084 |
|
| 5085 |
By default, the Vim syntax highlighting is turned off. To enable the syntax |
| 5086 |
highlighting, you can use one of the following commands: |
| 5087 |
|
| 5088 |
:syntax enable |
| 5089 |
|
| 5090 |
or |
| 5091 |
|
| 5092 |
:syntax on |
| 5093 |
|
| 5094 |
To disable the syntax highlighting, you can use the following command: |
| 5095 |
|
| 5096 |
:syntax off |
| 5097 |
|
| 5098 |
For more information, read |
| 5099 |
|
| 5100 |
:help 06.1 |
| 5101 |
:help 06.4 |
| 5102 |
:help :syntax-enable |
| 5103 |
:help :syntax-on |
| 5104 |
:help :syn-clear |
| 5105 |
|
| 5106 |
|
| 5107 |
24.2. How do I change the background and foreground colors used by Vim? |
| 5108 |
|
| 5109 |
Vim uses the "Normal" highlight group for the background and foreground |
| 5110 |
colors. To change the foreground/background colors, you have to modify the |
| 5111 |
"Normal" highlight group. For example, to set the background color to blue |
| 5112 |
and foreground color to white, you can use |
| 5113 |
|
| 5114 |
:highlight Normal ctermbg=blue ctermfg=white guibg=blue guifg=white |
| 5115 |
|
| 5116 |
If you are using the Motif or the Athena version of the GUI Vim, then you |
| 5117 |
can modify the foreground and background resource names in the .Xdefaults |
| 5118 |
files to change the colors: |
| 5119 |
|
| 5120 |
Vim.foreground: Black |
| 5121 |
Vim.backround: Wheat |
| 5122 |
|
| 5123 |
You can also use the "-foreground" and "-background" command-line arguments |
| 5124 |
to specify the foreground and background colors. These arguments are |
| 5125 |
supported only in the Motif or Athena versions: |
| 5126 |
|
| 5127 |
$ gvim -foreground Black -background Wheat |
| 5128 |
|
| 5129 |
For more information, read |
| 5130 |
|
| 5131 |
:help :highlight |
| 5132 |
:help .Xdefaults |
| 5133 |
:help -gui |
| 5134 |
|
| 5135 |
|
| 5136 |
24.3. How do I change the highlight colors to suit a dark/light background? |
| 5137 |
|
| 5138 |
You can set the 'background' option to either 'dark' or 'light' to change |
| 5139 |
the highlight colors to suit a dark/light background: |
| 5140 |
|
| 5141 |
:set background=dark |
| 5142 |
|
| 5143 |
For more information, read |
| 5144 |
|
| 5145 |
:help 'background' |
| 5146 |
:help 6.2 |
| 5147 |
|
| 5148 |
|
| 5149 |
24.4. How do I change the color of the line numbers displayed when the |
| 5150 |
":set number" command is used? |
| 5151 |
|
| 5152 |
The line numbers displayed use the LineNr highlighting group. To display |
| 5153 |
the current colors used, use |
| 5154 |
|
| 5155 |
:hi LineNr |
| 5156 |
|
| 5157 |
To change the color modify the LineNr highlight group. For example: |
| 5158 |
|
| 5159 |
:hi linenr guifg=red guibg=black |
| 5160 |
|
| 5161 |
This will give red numbers on a black background in GVIM. |
| 5162 |
|
| 5163 |
For more information, read |
| 5164 |
|
| 5165 |
:help :highlight |
| 5166 |
|
| 5167 |
|
| 5168 |
24.5. How do I change the background color used for a Visually selected |
| 5169 |
block? |
| 5170 |
|
| 5171 |
You can modify the 'Visual' highlight group to change the color used for a |
| 5172 |
visually selected block: |
| 5173 |
|
| 5174 |
:highlight Visual guibg=red |
| 5175 |
|
| 5176 |
For more information, read |
| 5177 |
|
| 5178 |
:help :highlight |
| 5179 |
:help hl-Visual |
| 5180 |
|
| 5181 |
|
| 5182 |
24.6. How do I highlight the special characters (tabs, trailing spaces, end |
| 5183 |
of line, etc) displayed by the 'list' option? |
| 5184 |
|
| 5185 |
You can modify the "NonText" and "SpecialKey" highlight groups to highlight |
| 5186 |
the special characters displayed by the 'list' option: |
| 5187 |
|
| 5188 |
:highlight NonText guibg=red |
| 5189 |
:highlight SpecialKey guibg=green |
| 5190 |
|
| 5191 |
The "NonText" highlighting group is used for "eol", "extends" and |
| 5192 |
"precedes" settings in the "listchars" option. The "SpecialKey" |
| 5193 |
highlighting group is used for the "tab" and "trail" settings. |
| 5194 |
|
| 5195 |
For more information, read |
| 5196 |
|
| 5197 |
:help 'listchars' |
| 5198 |
:help hl-NonText |
| 5199 |
:help hl-SpecialKey |
| 5200 |
|
| 5201 |
|
| 5202 |
24.7. How do I specify a colorscheme in my .vimrc/.gvimrc file, so that Vim |
| 5203 |
uses the specified colorscheme everytime? |
| 5204 |
|
| 5205 |
You can specify the color scheme using the ":colorscheme" command in your |
| 5206 |
.vimrc or .gvimrc file: |
| 5207 |
|
| 5208 |
colorschme evening |
| 5209 |
|
| 5210 |
For more information, read |
| 5211 |
|
| 5212 |
:help :colorscheme |
| 5213 |
|
| 5214 |
|
| 5215 |
24.8. Vim syntax highlighting is broken. When I am editing a file, some |
| 5216 |
parts of the file is not syntax highlighted or syntax highlighted |
| 5217 |
incorrectly. |
| 5218 |
|
| 5219 |
Vim doesn't read the whole file to parse the text for syntax highlighting. |
| 5220 |
It starts parsing wherever you are viewing the file. That saves a lot of |
| 5221 |
time, but sometimes the colors are wrong. A simple fix is refreshing the |
| 5222 |
screen using the CTRL-L key. Or scroll back a bit and then forward again. |
| 5223 |
You can also use the command: |
| 5224 |
|
| 5225 |
:syntax sync fromstart |
| 5226 |
|
| 5227 |
Note that this might considerably slow down the screen refreshing. |
| 5228 |
|
| 5229 |
For more information, read |
| 5230 |
|
| 5231 |
:help :syn-sync |
| 5232 |
:help :syn-sync-first |
| 5233 |
|
| 5234 |
|
| 5235 |
24.9. Is there a built-in function to syntax-highlight the corresponding |
| 5236 |
matching bracket? |
| 5237 |
|
| 5238 |
No. Vim doesn't support syntax-highlighting matching brackets. You can try |
| 5239 |
using the plugin developed by Charles Campbell: |
| 5240 |
|
| 5241 |
http://vim.sourceforge.net/tips/tip.php?tip_id=177 |
| 5242 |
|
| 5243 |
You can jump to a matching bracket using the '%' key. You can set the |
| 5244 |
'showmatch' option to temporarily jump to a matching bracket when in insert |
| 5245 |
mode. |
| 5246 |
|
| 5247 |
For more information, read |
| 5248 |
|
| 5249 |
:help % |
| 5250 |
:help 'showmatch' |
| 5251 |
:help 'matchtime' |
| 5252 |
:help 'matchpairs' |
| 5253 |
|
| 5254 |
|
| 5255 |
24.10. How do I turn off the C comment syntax highlighting? |
| 5256 |
|
| 5257 |
You can use the following command to turn off C comment syntax |
| 5258 |
highlighting: |
| 5259 |
|
| 5260 |
:highlight clear comment |
| 5261 |
|
| 5262 |
For more information, read |
| 5263 |
|
| 5264 |
:help c-syntax |
| 5265 |
|
| 5266 |
|
| 5267 |
24.11. How do I add my own syntax extensions to the standard syntax files |
| 5268 |
supplied with Vim? |
| 5269 |
|
| 5270 |
You should not modify the syntax files supplied with Vim to add your |
| 5271 |
extensions. When you install the next version of Vim, you will lose your |
| 5272 |
changes. Instead you should create a file under the ~/.vim/after/syntax |
| 5273 |
directory with the same name as the original syntax file and add your |
| 5274 |
additions to this file. |
| 5275 |
|
| 5276 |
For more information, read |
| 5277 |
|
| 5278 |
:help mysyntaxfile-add |
| 5279 |
:help 'runtimepath' |
| 5280 |
|
| 5281 |
|
| 5282 |
24.12. How do I replace a standard syntax file that comes with the Vim |
| 5283 |
distribution with my own syntax file? |
| 5284 |
|
| 5285 |
You can replace a standary syntax file that comes with the Vim distribution |
| 5286 |
by creating a file with the same name as the original syntax file and |
| 5287 |
placing it in the vim runtime syntax (~/.vim/syntax) directory. For |
| 5288 |
example, to replace the c.vim syntax file in a Unix system, place the new |
| 5289 |
c.vim in the ~/.vim/syntax directory. In a MS-Windows system, place the new |
| 5290 |
syntax file in the $HOME/vimfiles/syntax or $VIM/vimfiles/syntax directory. |
| 5291 |
|
| 5292 |
For more information, read |
| 5293 |
|
| 5294 |
:help mysyntaxfile-replace |
| 5295 |
:help 44.11 |
| 5296 |
:help mysyntaxfile |
| 5297 |
|
| 5298 |
|
| 5299 |
24.13. How do I highlight all the characters after a particular column? |
| 5300 |
|
| 5301 |
You can use the ":match" command to highlight all the characters after a |
| 5302 |
particular column: |
| 5303 |
|
| 5304 |
:match Todo '\%>75v.\+' |
| 5305 |
|
| 5306 |
This will highlight all the characters after the 75th column. |
| 5307 |
|
| 5308 |
For more information, read |
| 5309 |
|
| 5310 |
:help :match |
| 5311 |
:help /\%v |
| 5312 |
:help /\+ |
| 5313 |
:help /. |
| 5314 |
|
| 5315 |
|
| 5316 |
24.14. How do I convert a source file (.c, .h, etc) with the Vim syntax |
| 5317 |
highlighting into a HTML file? |
| 5318 |
|
| 5319 |
You can use the 2html.vim script to convert a source file into a HTML file |
| 5320 |
with the Vim syntax highlighting. Use the following command: |
| 5321 |
|
| 5322 |
:runtime! syntax/2html.vim |
| 5323 |
|
| 5324 |
For more information, read |
| 5325 |
|
| 5326 |
:help convert-to-HTML |
| 5327 |
|
| 5328 |
|
| 5329 |
24.15. How do I list the definition of all the current highlight groups? |
| 5330 |
|
| 5331 |
You can list the definition of all the current highlight groups using the |
| 5332 |
":highlight" (without any arguments) ex command. |
| 5333 |
|
| 5334 |
For more information, read |
| 5335 |
|
| 5336 |
:help :highlight |
| 5337 |
|
| 5338 |
|
| 5339 |
============================================================================= |
| 5340 |
|
| 5341 |
SECTION 25 - VIM SCRIPT WRITING |
| 5342 |
|
| 5343 |
|
| 5344 |
25.1. How do I list the names of all the scripts sourced by Vim? |
| 5345 |
|
| 5346 |
You can use the ":scriptnames" command to list the names of all the scripts |
| 5347 |
sourced by Vim: |
| 5348 |
|
| 5349 |
:scriptnames |
| 5350 |
|
| 5351 |
For more information, read |
| 5352 |
|
| 5353 |
:help :scriptnames |
| 5354 |
|
| 5355 |
|
| 5356 |
25.2. How do I debug Vim scripts? |
| 5357 |
|
| 5358 |
Vim has built-in support for a primitive debugger to debug Vim plugins and |
| 5359 |
scripts. Using this debugger you can set breakpoints and step through the |
| 5360 |
plugin functions. |
| 5361 |
|
| 5362 |
For more information, read |
| 5363 |
|
| 5364 |
:help debug-scripts |
| 5365 |
:help -D |
| 5366 |
|
| 5367 |
|
| 5368 |
25.3. How do I locate the script/plugin which sets a Vim option? |
| 5369 |
|
| 5370 |
You can use the ":verbose" command to locate the plugin/script which last |
| 5371 |
modified a Vim option. For example: |
| 5372 |
|
| 5373 |
:verbose set textwidth? |
| 5374 |
|
| 5375 |
For more information, read |
| 5376 |
|
| 5377 |
:help :set-verbose |
| 5378 |
:help :verbose |
| 5379 |
|
| 5380 |
|
| 5381 |
25.4. I am getting some error/informational messages from Vim (possibly |
| 5382 |
when running a script), the messages are cleared immediately. How do |
| 5383 |
I display the messages again? |
| 5384 |
|
| 5385 |
You can use the ":messages" command to display the previous messages. |
| 5386 |
|
| 5387 |
:messages |
| 5388 |
|
| 5389 |
For more information, read |
| 5390 |
|
| 5391 |
:help :messages |
| 5392 |
:help :echoerr |
| 5393 |
:help :echomsg |
| 5394 |
:help message-history |
| 5395 |
|
| 5396 |
|
| 5397 |
25.5. How do I save and restore a plugin specific information across Vim |
| 5398 |
invocations? |
| 5399 |
|
| 5400 |
Vim will save and restore global variables that start with an uppercase |
| 5401 |
letter and don't contain a lower case letter. For this to work, the |
| 5402 |
'viminfo' option must contain the '!' flag. Vim will store the variables in |
| 5403 |
the viminfo file. |
| 5404 |
|
| 5405 |
For more information, read |
| 5406 |
|
| 5407 |
:help 'viminfo' |
| 5408 |
:help viminfo-file |
| 5409 |
:help variables |
| 5410 |
|
| 5411 |
|
| 5412 |
25.6. How do I start insert mode from a Vim function? |
| 5413 |
|
| 5414 |
You can use the ":startinsert" command to start the insert mode from inside |
| 5415 |
a Vim function. |
| 5416 |
|
| 5417 |
For more information, read |
| 5418 |
|
| 5419 |
:help :startinsert |
| 5420 |
|
| 5421 |
|
| 5422 |
25.7. How do I change the cursor position from within a Vim function? |
| 5423 |
|
| 5424 |
You can use the cursor() function to position the cursor. |
| 5425 |
|
| 5426 |
call cursor(lnum, col) |
| 5427 |
|
| 5428 |
You can also use the following command to change the cursor position: |
| 5429 |
|
| 5430 |
exe "normal! " . lnum . "G" . col . "|" |
| 5431 |
|
| 5432 |
For more information, read |
| 5433 |
|
| 5434 |
:help cursor() |
| 5435 |
:help bar |
| 5436 |
|
| 5437 |
|
| 5438 |
25.8. How do I check the value of an environment variable in the .vimrc |
| 5439 |
file? |
| 5440 |
|
| 5441 |
You can use prefix the environment variable name with the '$' character to |
| 5442 |
use it from a Vim script/function. You can refer to the value of an |
| 5443 |
environment variable using the $env_var syntax: |
| 5444 |
|
| 5445 |
if $EDITOR == 'vi' |
| 5446 |
endif |
| 5447 |
|
| 5448 |
For more information, read |
| 5449 |
|
| 5450 |
:help expr-env |
| 5451 |
|
| 5452 |
|
| 5453 |
25.9. How do I check whether an environment variable is set or not from a |
| 5454 |
Vim function? |
| 5455 |
|
| 5456 |
You can use the exists() function to check for the existence of a |
| 5457 |
environment variable. |
| 5458 |
|
| 5459 |
if exists("$MY_ENV_VAR") |
| 5460 |
endif |
| 5461 |
|
| 5462 |
For more information, read |
| 5463 |
|
| 5464 |
:help exists() |
| 5465 |
:help expr-env |
| 5466 |
|
| 5467 |
|
| 5468 |
25.10. How do I call/use the Vim built-in functions? |
| 5469 |
|
| 5470 |
You can use the ":call" command to invoke a Vim built-in function: |
| 5471 |
|
| 5472 |
:call cursor(10,20) |
| 5473 |
|
| 5474 |
You can use the ":echo" command to echo the value returned by a function: |
| 5475 |
|
| 5476 |
:echo char2nr('a') |
| 5477 |
|
| 5478 |
You can use the ":let" command to assign the value returned by a function |
| 5479 |
to a variable: |
| 5480 |
|
| 5481 |
:let a = getline('.') |
| 5482 |
|
| 5483 |
To store the return value from a function into a Vim register, you can use |
| 5484 |
the following command: |
| 5485 |
|
| 5486 |
:let @a = system('ls') |
| 5487 |
|
| 5488 |
The above command will store the return value from the 'ls' command into |
| 5489 |
the register 'a'. |
| 5490 |
|
| 5491 |
For more information, read |
| 5492 |
|
| 5493 |
:help :call |
| 5494 |
:help :echo |
| 5495 |
:help :let |
| 5496 |
:help :let-register |
| 5497 |
:help user-functions |
| 5498 |
:help usr_41.txt |
| 5499 |
|
| 5500 |
|
| 5501 |
25.11. I am using some normal mode commands in my Vim script. How do I |
| 5502 |
avoid using the user-defined mappings for these normal mode commands |
| 5503 |
and use the standard Vim functionality for these normal mode |
| 5504 |
commands? |
| 5505 |
|
| 5506 |
You can use the "normal!" command in your script to invoke a normal-mode |
| 5507 |
command. This will use the standard functionality of the normal mode |
| 5508 |
command and will not use the user-defined mapping. |
| 5509 |
|
| 5510 |
For more information, read |
| 5511 |
|
| 5512 |
:help :normal |
| 5513 |
|
| 5514 |
|
| 5515 |
25.12. How do I get the current visually selected text into a Vim variable |
| 5516 |
or register? |
| 5517 |
|
| 5518 |
You can get the current visually selected text into a Vim variable by |
| 5519 |
yanking the text into Vim register and then assigning the contents of the |
| 5520 |
register into the variable: |
| 5521 |
|
| 5522 |
:normal! gvy |
| 5523 |
:let myvar = @" |
| 5524 |
|
| 5525 |
The above command copies the visually selected text into the variable |
| 5526 |
"myvar". |
| 5527 |
|
| 5528 |
You can also use the command: |
| 5529 |
|
| 5530 |
:normal! gv"*y |
| 5531 |
|
| 5532 |
In the above command, gv reselects the last visually selected text and the |
| 5533 |
rest of the command copies the selected text into the * (clipboard) |
| 5534 |
register. Alternatively, you can set the 'a' flag in the 'guioptions' |
| 5535 |
option to automatically copy a visually selected text into the * register. |
| 5536 |
To do this as part of a visual map, you can use a command similar to the |
| 5537 |
one shown below: |
| 5538 |
|
| 5539 |
:vmap <F3> "*y:call ... |
| 5540 |
|
| 5541 |
For more information, read |
| 5542 |
|
| 5543 |
:help gv |
| 5544 |
:help :normal |
| 5545 |
:help let-@ |
| 5546 |
:help quotestar |
| 5547 |
:help clipboard |
| 5548 |
:help registers |
| 5549 |
|
| 5550 |
|
| 5551 |
25.13. I have some text in a Vim variable 'myvar'. I would like to use this |
| 5552 |
variable in a ":s" substitute command to replace a text 'mytext'. |
| 5553 |
How do I do this? |
| 5554 |
|
| 5555 |
You can use the 'execute' command to evaluate the variable: |
| 5556 |
|
| 5557 |
:execute '%s/mytext/' . myvar . '/' |
| 5558 |
|
| 5559 |
For more information, read |
| 5560 |
|
| 5561 |
:help :execute |
| 5562 |
|
| 5563 |
You can also use "\=" in the substitute command to evaluate the variable: |
| 5564 |
|
| 5565 |
:%s/mytext/\=myvar/ |
| 5566 |
|
| 5567 |
For more information, read |
| 5568 |
|
| 5569 |
:help sub-replace-special |
| 5570 |
|
| 5571 |
|
| 5572 |
25.14. A Vim variable (bno) contains a buffer number. How do I use this |
| 5573 |
variable to open the corresponding buffer? |
| 5574 |
|
| 5575 |
The :buffer command will not accept a variable name. It accepts only a |
| 5576 |
buffer number or buffer name. You have to use the ":execute" command to |
| 5577 |
evaluate the variable into the corresponding value. For example: |
| 5578 |
|
| 5579 |
:execute "buffer " . bno |
| 5580 |
|
| 5581 |
For more information, read |
| 5582 |
|
| 5583 |
:help :execute |
| 5584 |
|
| 5585 |
|
| 5586 |
25.15. How do I store the value of a Vim option into a Vim variable? |
| 5587 |
|
| 5588 |
You can prefix the option name with the '&' character and assign the option |
| 5589 |
value to a Vim variable using the "let" command. For example, to store the |
| 5590 |
value of the 'textwidth' option into the Vim variable "old_tw", you can use |
| 5591 |
the following command: |
| 5592 |
|
| 5593 |
:let old_tw = &tw |
| 5594 |
|
| 5595 |
To do the opposite, to set the 'textwidth' option with the value stored in |
| 5596 |
the 'old_tw' variable, you can use the following command: |
| 5597 |
|
| 5598 |
:let &tw = old_tw |
| 5599 |
|
| 5600 |
For more information, read |
| 5601 |
|
| 5602 |
:help expr-option |
| 5603 |
:help let-option |
| 5604 |
|
| 5605 |
|
| 5606 |
25.16. I have copied and inserted some text into a buffer from a Vim |
| 5607 |
function. How do I indent the inserted text from the Vim function? |
| 5608 |
|
| 5609 |
You can use the following command to format the just inserted text: |
| 5610 |
|
| 5611 |
:normal '[='] |
| 5612 |
|
| 5613 |
For more information, read |
| 5614 |
|
| 5615 |
:help '[ |
| 5616 |
:help '] |
| 5617 |
:help = |
| 5618 |
:help :normal |
| 5619 |
|
| 5620 |
|
| 5621 |
25.17. How do I get the character under the cursor from a Vim script? |
| 5622 |
|
| 5623 |
You can use the getline() function and use string index [] to get the |
| 5624 |
character: |
| 5625 |
|
| 5626 |
:echo getline(".")[col(".") - 1] |
| 5627 |
|
| 5628 |
In the above command, getline(".") returns the text in the current line. |
| 5629 |
The indexing of the string starts at zero, and you can get a single |
| 5630 |
character in a string by its index with the "string[index]" notation. The |
| 5631 |
col(".") returns the column of the cursor position; the adjustment is to |
| 5632 |
get the right character of the string. |
| 5633 |
|
| 5634 |
Alternatively, you can use the following sequence of commands to get the |
| 5635 |
character under the cursor: |
| 5636 |
|
| 5637 |
normal! vy |
| 5638 |
let ch=@" |
| 5639 |
|
| 5640 |
Note that the above commands will change the '< and '> marks. |
| 5641 |
|
| 5642 |
For more information, read |
| 5643 |
|
| 5644 |
:help getline() |
| 5645 |
:help col() |
| 5646 |
:help expr-[] |
| 5647 |
|
| 5648 |
|
| 5649 |
25.18. How do I get the name of the current file without the extension? |
| 5650 |
|
| 5651 |
You can get the name of the current file without the extension using: |
| 5652 |
|
| 5653 |
:echo expand("%:r") |
| 5654 |
|
| 5655 |
With some commands, you can use the file name modifiers directly: |
| 5656 |
|
| 5657 |
:cd %:p:h |
| 5658 |
:!gcc -o %:r.o % |
| 5659 |
|
| 5660 |
For more information, read |
| 5661 |
|
| 5662 |
:help filename-modifiers |
| 5663 |
:help expand() |
| 5664 |
:help cmdline-special |
| 5665 |
:help fnamemodify() |
| 5666 |
|
| 5667 |
|
| 5668 |
25.19. How do I get the basename of the current file? |
| 5669 |
|
| 5670 |
You can use the :t filename modifier to get the basename of the current |
| 5671 |
file: |
| 5672 |
|
| 5673 |
:echo expand("%:t") |
| 5674 |
|
| 5675 |
For more information, read |
| 5676 |
|
| 5677 |
:help filename-modifiers |
| 5678 |
|
| 5679 |
|
| 5680 |
25.20. How do I get the output from a Vim function into the current buffer? |
| 5681 |
|
| 5682 |
You can insert the return value from a function using the following command |
| 5683 |
in insert mode: |
| 5684 |
|
| 5685 |
<C-R>=MyFunc() |
| 5686 |
|
| 5687 |
Note that this will only insert the return value of the function. |
| 5688 |
|
| 5689 |
For more information, read |
| 5690 |
|
| 5691 |
:help i_CTRL-R |
| 5692 |
:help i_CTRL-R_CTRL-R |
| 5693 |
:help i_CTRL-R_CTRL-O |
| 5694 |
:help expression |
| 5695 |
|
| 5696 |
|
| 5697 |
25.21. How do I call external programs from a Vim function? |
| 5698 |
|
| 5699 |
There are several ways to call external programs from a Vim function. You |
| 5700 |
can use the builtin system() function to invoke external programs and get |
| 5701 |
the result: |
| 5702 |
|
| 5703 |
:let output = system("ls") |
| 5704 |
|
| 5705 |
You can also use "!" ex-command to run an external command. |
| 5706 |
|
| 5707 |
For more information, read |
| 5708 |
|
| 5709 |
:help system() |
| 5710 |
:help :! |
| 5711 |
:help 10.9 |
| 5712 |
|
| 5713 |
|
| 5714 |
25.22. How do I get the return status of a program executed using the ":!" |
| 5715 |
command? |
| 5716 |
|
| 5717 |
You can use the predefined Vim v:shell_error variable to get the return |
| 5718 |
status of the last run shell command. |
| 5719 |
|
| 5720 |
For more information, read |
| 5721 |
|
| 5722 |
:help v:shell_error |
| 5723 |
|
| 5724 |
|
| 5725 |
25.23. How do I determine whether the current buffer is modified or not? |
| 5726 |
|
| 5727 |
You can check the value of the 'modified' option to determine whether the |
| 5728 |
current buffer is modified: |
| 5729 |
|
| 5730 |
:set modified? |
| 5731 |
|
| 5732 |
From a Vim script, you can check the value of the 'modified' option: |
| 5733 |
|
| 5734 |
if &modified |
| 5735 |
echo "File is modified" |
| 5736 |
endif |
| 5737 |
|
| 5738 |
For more information, read |
| 5739 |
|
| 5740 |
:help 'modified' |
| 5741 |
|
| 5742 |
|
| 5743 |
25.24. I would like to use the carriage return character in a normal |
| 5744 |
command from a Vim script. How do I specify the carriage return |
| 5745 |
character? |
| 5746 |
|
| 5747 |
You can use the ":execute" command to specify the special (control) |
| 5748 |
character in a normal mode command: |
| 5749 |
|
| 5750 |
:execute "normal \<CR>" |
| 5751 |
:execute "normal ixxx\<Esc>" |
| 5752 |
|
| 5753 |
For more information, read |
| 5754 |
|
| 5755 |
:help :execute |
| 5756 |
:help expr-quote |
| 5757 |
|
| 5758 |
|
| 5759 |
25.25. How do I split long lines in a Vim script? |
| 5760 |
|
| 5761 |
You can split long lines in a Vim script by inserting the backslash |
| 5762 |
character ("\") at the start of the next line. For example, |
| 5763 |
|
| 5764 |
For more information, read |
| 5765 |
|
| 5766 |
:help line-continuation |
| 5767 |
|
| 5768 |
|
| 5769 |
25.26. When I try to "execute" my function using the "execute 'echo |
| 5770 |
Myfunc()'" command, the cursor is moved to the top of the current |
| 5771 |
buffer. Why? |
| 5772 |
|
| 5773 |
The ":execute" command runs the normal mode command specified by the |
| 5774 |
argument. In the case of the following command: |
| 5775 |
|
| 5776 |
:execute "echo Myfunc()" |
| 5777 |
|
| 5778 |
The call to "echo Myfunc()" will return 0. The ":execute" command will run |
| 5779 |
the normal mode command "0", which moves the cursor to the top of the file. |
| 5780 |
To call a Vim function, you should use the ":call" command instead of the |
| 5781 |
":execute" command: |
| 5782 |
|
| 5783 |
:call Myfunc() |
| 5784 |
|
| 5785 |
For more information, read |
| 5786 |
|
| 5787 |
:help :call |
| 5788 |
:help :execute |
| 5789 |
:help :echo |
| 5790 |
:help user-functions |
| 5791 |
:help 41.5 |
| 5792 |
:help 41.6 |
| 5793 |
|
| 5794 |
|
| 5795 |
25.27. How do I source/execute the contents of a register? |
| 5796 |
|
| 5797 |
If you have yanked a set of Vim commands into a Vim register (for example |
| 5798 |
register 'a'), then you can source the contents of the register using one |
| 5799 |
of the following commands: |
| 5800 |
|
| 5801 |
:@a |
| 5802 |
or |
| 5803 |
:exe @a |
| 5804 |
|
| 5805 |
For more information, read |
| 5806 |
|
| 5807 |
:help :@ |
| 5808 |
|
| 5809 |
|
| 5810 |
25.28. After calling a Vim function or a mapping, when I press the 'u' |
| 5811 |
key to undo the last change, Vim undoes all the changes made by |
| 5812 |
the mapping/function. Why? |
| 5813 |
|
| 5814 |
When you call a function or a mapping, all the operations performed by the |
| 5815 |
function/mapping are treated as one single operation. When you undo the |
| 5816 |
last operation by pressing 'u', all the changes made by the |
| 5817 |
function/mapping are reversed. |
| 5818 |
|
| 5819 |
For more information, read |
| 5820 |
|
| 5821 |
:help undo-redo |
| 5822 |
:help map-undo |
| 5823 |
|
| 5824 |
|
| 5825 |
25.29. How can I call a function defined with s: (script local function) |
| 5826 |
from another script/plugin? |
| 5827 |
|
| 5828 |
The s: prefix for a Vim function name is used to create a script local |
| 5829 |
function. A script local function can be called only from within that |
| 5830 |
script and cannot be called from other scripts. To define a function in a |
| 5831 |
script/plugin, so that it can be called from other plugins/scripts, define |
| 5832 |
the function without the s: prefix. |
| 5833 |
|
| 5834 |
For more information, read |
| 5835 |
|
| 5836 |
:help script-variable |
| 5837 |
:help script-local |
| 5838 |
:help :scriptnames |
| 5839 |
|
| 5840 |
|
| 5841 |
25.30. Is it possible to un-source a sourced script? In otherwords, reverse |
| 5842 |
all the commands executed by sourcing a script. |
| 5843 |
|
| 5844 |
No. It is not possible to reverse or undo all the commands executed by |
| 5845 |
sourcing a script. |
| 5846 |
|
| 5847 |
For more information, read |
| 5848 |
|
| 5849 |
:help :source |
| 5850 |
|
| 5851 |
|
| 5852 |
============================================================================= |
| 5853 |
|
| 5854 |
SECTION 26 - PLUGINS |
| 5855 |
|
| 5856 |
|
| 5857 |
26.1. How do I set different options for different types of files? |
| 5858 |
|
| 5859 |
You can create filetype plugins to set different options for different |
| 5860 |
types of files. You should first enable filetype plugins using the command: |
| 5861 |
|
| 5862 |
:filetype plugin on |
| 5863 |
|
| 5864 |
A filetype plugin is a vim script that is loaded whenever Vim opens or |
| 5865 |
creates a file of that type. For example, to ensure that the 'textwidth' |
| 5866 |
option is set to 80 when editing a C program (filetype 'c'), create one of |
| 5867 |
the following files: |
| 5868 |
|
| 5869 |
~/.vim/ftplugin/c.vim (Unix) |
| 5870 |
%HOME%\vimfiles\ftplugin\c.vim (Windows) |
| 5871 |
|
| 5872 |
with the following text in it: |
| 5873 |
|
| 5874 |
setlocal textwidth=80 |
| 5875 |
|
| 5876 |
You can also use autocommands to set specific options when editing specific |
| 5877 |
type of files. For example, to set the 'textwidth' option to 75 for only |
| 5878 |
*.txt files, you can use the following autocmd: |
| 5879 |
|
| 5880 |
autocmd BufRead *.txt setlocal textwidth=80 |
| 5881 |
|
| 5882 |
For more information, read |
| 5883 |
|
| 5884 |
:help filetype-plugin |
| 5885 |
:help add-filetype-plugin |
| 5886 |
:help autocmd |
| 5887 |
:help 40.3 |
| 5888 |
|
| 5889 |
|
| 5890 |
26.2. I have downloaded a Vim plugin or a syntax file or a indent file, or |
| 5891 |
a color scheme or a filetype plugin from the web. Where should I copy |
| 5892 |
these files so that Vim will find them? |
| 5893 |
|
| 5894 |
You can place the Vim runtime files (plugins, syntax files, indent files, |
| 5895 |
color schemes, filetype plugins, etc) under one of the directories |
| 5896 |
specified in the 'runtimepath' option. To determine the current value of |
| 5897 |
the 'runtimepath' option, use the following command: |
| 5898 |
|
| 5899 |
:set runtimepath |
| 5900 |
|
| 5901 |
For Unix systems, this is usally the "$HOME/.vim" directory. For MS-Windows |
| 5902 |
systems, this is usually the $VIM\vimfiles or $HOME\vimfiles directory. |
| 5903 |
Depending on the type of the runtime file, you have to place it under a |
| 5904 |
specific directory under the above runtime directory. The names of the |
| 5905 |
directories are listed below: |
| 5906 |
|
| 5907 |
colors/ - color scheme files |
| 5908 |
compiler/ - compiler files |
| 5909 |
doc/ - documentation |
| 5910 |
ftplugin/ - filetype plugins |
| 5911 |
indent/ - indent scripts |
| 5912 |
keymap/ - key mapping files |
| 5913 |
lang/ - menu translations |
| 5914 |
plugin/ - plugin scripts |
| 5915 |
syntax/ - syntax files |
| 5916 |
tutor/ - files for vimtutor |
| 5917 |
|
| 5918 |
For more information, read |
| 5919 |
|
| 5920 |
:help your-runtime-dir |
| 5921 |
:help 'runtimepath' |
| 5922 |
:help :runtime |
| 5923 |
|
| 5924 |
|
| 5925 |
26.3. How do I extend an existing filetype plugin? |
| 5926 |
|
| 5927 |
You can extend an existing filetype plugin by creating a file under either |
| 5928 |
the $VIMRTUNTIME/after/ftplugin or the $VIMRTUNTIME/ftplugin directory. The |
| 5929 |
name of the file should be the same as the name of the existing filetype |
| 5930 |
plugin file. You can place your additions to the new file. |
| 5931 |
|
| 5932 |
If you placed the file in the after/ftplugin runtime directory, then Vim |
| 5933 |
will first source the existing filetype plugin file and then will source |
| 5934 |
the new file. If you placed the file in the $VIMRTUNTIME/ftplugin runtime |
| 5935 |
directory, then Vim will first source the new file and then will source the |
| 5936 |
existing filetype plugin file. |
| 5937 |
|
| 5938 |
For more information, read |
| 5939 |
|
| 5940 |
:help ftplugin-overrule |
| 5941 |
:help filetype-plugin |
| 5942 |
:help add-filetype-plugin |
| 5943 |
:help 'runtimepath' |
| 5944 |
|
| 5945 |
|
| 5946 |
26.4. How do I turn off loading the Vim plugins? |
| 5947 |
|
| 5948 |
You can reset the 'loadplugins' option to turn off loading the plugins: |
| 5949 |
|
| 5950 |
:set noloadplugins |
| 5951 |
|
| 5952 |
You can also specify the "--noplugin" command line argument to stop loading |
| 5953 |
the plugins: |
| 5954 |
|
| 5955 |
$ vim --noplugin |
| 5956 |
|
| 5957 |
For more information, read |
| 5958 |
|
| 5959 |
:help 'loadplugins' |
| 5960 |
:help --noplugin |
| 5961 |
:help load-plugins |
| 5962 |
|
| 5963 |
|
| 5964 |
26.5. How do I turn on/off loading the filetype plugins? |
| 5965 |
|
| 5966 |
By default, Vim will not load the filetype plugins. You can configure Vim |
| 5967 |
to load filetype plugins using the command: |
| 5968 |
|
| 5969 |
filetype plugin on |
| 5970 |
|
| 5971 |
You can turn off loading the filetype plugins using: |
| 5972 |
|
| 5973 |
filetype plugin off |
| 5974 |
|
| 5975 |
For more information, read |
| 5976 |
|
| 5977 |
:help filetype-plugin-on |
| 5978 |
:help filetype-plugin-off |
| 5979 |
:help :filetype |
| 5980 |
|
| 5981 |
|
| 5982 |
26.6. How do I override settings made in a file type plugin in the global |
| 5983 |
ftplugin directory for all the file types? |
| 5984 |
|
| 5985 |
You can use an autocommand triggered on the FileType event: |
| 5986 |
|
| 5987 |
au Filetype * set formatoptions=xyz |
| 5988 |
|
| 5989 |
This should at least be after "filetype on" in your vimrc. Best is to put |
| 5990 |
it in your "myfiletypefile" file, so that it's always last. |
| 5991 |
|
| 5992 |
If you want to override a setting for a particular filetype, then create a |
| 5993 |
file with the same name as the original filetype plugin in the |
| 5994 |
~/.vim/after/ftplugin directory For example, to override a setting in the |
| 5995 |
c.vim filetype plugin, create a c.vim file in the ~/.vim/after/ftplugin |
| 5996 |
directory and add your preferences in this file. |
| 5997 |
|
| 5998 |
For more information, read |
| 5999 |
|
| 6000 |
:help ftplugin-overrule |
| 6001 |
:help ftplugins |
| 6002 |
:help myfiletypefile |
| 6003 |
|
| 6004 |
|
| 6005 |
26.7. How do I disable the Vim directory browser plugin? |
| 6006 |
|
| 6007 |
To disable the directory browsing Vim plugin, add the following line to |
| 6008 |
your .vimrc file: |
| 6009 |
|
| 6010 |
let loaded_explorer = 1 |
| 6011 |
|
| 6012 |
For more information, read |
| 6013 |
|
| 6014 |
:help file-explorer |
| 6015 |
|
| 6016 |
|
| 6017 |
26.8. How do I set the filetype option for files with names matching a |
| 6018 |
particular pattern or depending on the file extension? |
| 6019 |
|
| 6020 |
You can set the 'filetype' option for files with names matching a |
| 6021 |
particular pattern using an autocmd. For example, to set the 'filetype' |
| 6022 |
option to 'c' for all files with extension '.x', you can use the following |
| 6023 |
autocmd: |
| 6024 |
|
| 6025 |
autocmd! BufRead,BufNewFile *.x setfiletype c |
| 6026 |
|
| 6027 |
A better alternative to the above approach is to create a filetype.vim file |
| 6028 |
in the ~/.vim directory (or in one of the directories specified in the |
| 6029 |
'runtimepath' option) and add the following lines: |
| 6030 |
|
| 6031 |
" my filetype file |
| 6032 |
if exists("did_load_filetypes") |
| 6033 |
finish |
| 6034 |
endif |
| 6035 |
augroup filetypedetect |
| 6036 |
au! BufRead,BufNewFile *.x setfiletype c |
| 6037 |
augroup END |
| 6038 |
|
| 6039 |
For more information, read |
| 6040 |
|
| 6041 |
:help new-filetype |
| 6042 |
:help 43.2 |
| 6043 |
:help :setfiletype |
| 6044 |
|
| 6045 |
|
| 6046 |
============================================================================= |
| 6047 |
|
| 6048 |
SECTION 27 - EDITING PROGRAM FILES |
| 6049 |
|
| 6050 |
|
| 6051 |
27.1. How do I enable automatic indentation for C/C++ files? |
| 6052 |
|
| 6053 |
You can enable file-type based indentation using: |
| 6054 |
|
| 6055 |
:filetype indent on |
| 6056 |
|
| 6057 |
If you want to only enable automatic C indentation, then use: |
| 6058 |
|
| 6059 |
:set cindent |
| 6060 |
|
| 6061 |
For more information, read |
| 6062 |
|
| 6063 |
:help 'cindent' |
| 6064 |
:help C-indenting |
| 6065 |
:help filetype |
| 6066 |
|
| 6067 |
|
| 6068 |
27.2. How do I configure the indentation used for C/C++ files? |
| 6069 |
|
| 6070 |
You can configure the Vim C indentation by modifying the value of the |
| 6071 |
'cinoptions', 'cinkeys' and 'cinwords' options. |
| 6072 |
|
| 6073 |
For more information, read |
| 6074 |
|
| 6075 |
:help 'cindent' |
| 6076 |
:help 'cinoptions' |
| 6077 |
:help 'cinkeys' |
| 6078 |
:help 'cinwords' |
| 6079 |
:help C-indenting |
| 6080 |
:help cinoptions-values |
| 6081 |
:help 'smartindent' |
| 6082 |
|
| 6083 |
|
| 6084 |
27.3. How do I turn off the automatic indentation feature? |
| 6085 |
|
| 6086 |
By default, the automatic indentation is not turned on. You must have |
| 6087 |
configured Vim to do automatic indentation in either .vimrc or .gvimrc |
| 6088 |
files. You can disable automatic indentation using either, |
| 6089 |
|
| 6090 |
:filetype indent off |
| 6091 |
|
| 6092 |
or |
| 6093 |
|
| 6094 |
:set nocindent |
| 6095 |
|
| 6096 |
Also, check the setting for the following options: |
| 6097 |
|
| 6098 |
:set autoindent? |
| 6099 |
:set smartindent? |
| 6100 |
:set indentexpr? |
| 6101 |
|
| 6102 |
For more information, read |
| 6103 |
|
| 6104 |
:help 'cindent' |
| 6105 |
:help filetype-indent-off |
| 6106 |
:help 'autoindent' |
| 6107 |
:help 'smartindent' |
| 6108 |
:help 'indentexpr' |
| 6109 |
|
| 6110 |
|
| 6111 |
27.4. How do I change the number of space characters used for the automatic |
| 6112 |
indentation? |
| 6113 |
|
| 6114 |
You can modify the 'shiftwidth' option to change the number of space |
| 6115 |
characters used for the automatic indentation: |
| 6116 |
|
| 6117 |
:set shiftwidth=4 |
| 6118 |
|
| 6119 |
For more information, read |
| 6120 |
|
| 6121 |
:help 'shiftwidth' |
| 6122 |
|
| 6123 |
|
| 6124 |
27.5. I am editing a C program using Vim. How do I display the definition |
| 6125 |
of a macro or a variable? |
| 6126 |
|
| 6127 |
You can use the [d command to display the definition of a macro and the [i |
| 6128 |
command to display the definition of a variable. |
| 6129 |
|
| 6130 |
For more information, read |
| 6131 |
|
| 6132 |
:help [d |
| 6133 |
:help [i |
| 6134 |
:help include-search |
| 6135 |
:help 29.4 |
| 6136 |
:help 29.5 |
| 6137 |
|
| 6138 |
|
| 6139 |
27.6. I am editing a C program using Vim. How do I jump to the beginning or |
| 6140 |
end of a code block from within the block? |
| 6141 |
|
| 6142 |
You can use '[{' command to jump to the beginning of the code block and ']} |
| 6143 |
to jump to the end of the code block from inside the block. |
| 6144 |
|
| 6145 |
For more information, read |
| 6146 |
|
| 6147 |
:help [{ |
| 6148 |
:help ]} |
| 6149 |
:help various-motions |
| 6150 |
|
| 6151 |
|
| 6152 |
27.7. Is there a way to turn off the "//" comment auto-insertion behavior |
| 6153 |
for C++ files? If I'm sitting on a line beginning with "//", then I |
| 6154 |
open a new line above or below it, Vim automatically inserts new "//" |
| 6155 |
chars. |
| 6156 |
|
| 6157 |
You can modify the value of the 'comments' option to stop Vim from |
| 6158 |
inserting the C++ comment character ("//") automatically. For example: |
| 6159 |
|
| 6160 |
:set comments=sr:/*,mb:*,el:*/ |
| 6161 |
|
| 6162 |
For more information, read |
| 6163 |
|
| 6164 |
:help 'comments' |
| 6165 |
:help format-comments |
| 6166 |
|
| 6167 |
|
| 6168 |
27.8. How do I add the comment character '#' to a set of lines at the |
| 6169 |
beginning of each line? |
| 6170 |
|
| 6171 |
First, select the first character in all the lines using visual block mode |
| 6172 |
(CTRL-V). Press 'I' to start inserting characters at the beginning of the |
| 6173 |
line. Enter the comment character and then stop the insert mode by pressing |
| 6174 |
<Esc>. Vim will automatically insert the entered characters at the |
| 6175 |
beginning of all the selected lines. |
| 6176 |
|
| 6177 |
For more information, read |
| 6178 |
|
| 6179 |
:help visual-block |
| 6180 |
:help blockwise-operators |
| 6181 |
:help v_b_I |
| 6182 |
|
| 6183 |
|
| 6184 |
27.9. How do I edit a header file with the same name as the corresponding C |
| 6185 |
source file? |
| 6186 |
|
| 6187 |
You can use the following command to edit a header file with the same name |
| 6188 |
as the corresponding C source file: |
| 6189 |
|
| 6190 |
:e %:t:r.h |
| 6191 |
|
| 6192 |
You can use the following command to edit the file in a new split window: |
| 6193 |
|
| 6194 |
:sp %:t:r.h |
| 6195 |
|
| 6196 |
In the above commands, the percent sign expands to the name of the current |
| 6197 |
file. The ":t" modifier extracts the tail (last component) of the |
| 6198 |
filename. The ":r" modifier extracts the root of the filename. The .h is |
| 6199 |
appended to the resulting name to get the header filename. |
| 6200 |
|
| 6201 |
Another approach is to use the following command: |
| 6202 |
|
| 6203 |
:sfind %:t:r.h |
| 6204 |
|
| 6205 |
This command will search for the header file in the directories specified |
| 6206 |
in the 'path' option. |
| 6207 |
|
| 6208 |
For more information, read |
| 6209 |
|
| 6210 |
:help cmdline-special |
| 6211 |
:help filename-modifiers |
| 6212 |
:help :sfind |
| 6213 |
:help 'path' |
| 6214 |
|
| 6215 |
|
| 6216 |
27.10. How do I automatically insert comment leaders while typing comments? |
| 6217 |
|
| 6218 |
To automatically insert comment leaders while typing comments, add the 'r' |
| 6219 |
and 'o' flags to the 'formatoptions' option. |
| 6220 |
|
| 6221 |
:set formatoptions+=ro |
| 6222 |
|
| 6223 |
You may also want to add the 'c' flag to auto-wrap comments using the |
| 6224 |
'textwidth' option setting and the 'q' flag to format comments with the |
| 6225 |
"gq" command: |
| 6226 |
|
| 6227 |
:set formatoptions=croq |
| 6228 |
|
| 6229 |
For more information, read |
| 6230 |
|
| 6231 |
:help 30.6 |
| 6232 |
:help format-comments |
| 6233 |
:help 'comments' |
| 6234 |
:help fo-table |
| 6235 |
|
| 6236 |
|
| 6237 |
============================================================================= |
| 6238 |
|
| 6239 |
SECTION 28 - QUICKFIX |
| 6240 |
|
| 6241 |
|
| 6242 |
28.1. How do I build programs from Vim? |
| 6243 |
|
| 6244 |
You can use the ":make" command to build programs from Vim. The ":make" |
| 6245 |
command runs the program specified by the 'makeprg' option. |
| 6246 |
|
| 6247 |
For more information, read |
| 6248 |
|
| 6249 |
:help 30.1 |
| 6250 |
:help make_makeprg |
| 6251 |
:help 'makeprg' |
| 6252 |
:help 'makeef' |
| 6253 |
:help :make |
| 6254 |
:help quickfix |
| 6255 |
|
| 6256 |
|
| 6257 |
28.2. When I run the make command in Vim I get the errors listed as the |
| 6258 |
compiler compiles the program. When it finishes this list disappears |
| 6259 |
and I have to use the :clist command to see the error message again. |
| 6260 |
Is there any other way to see these error messages? |
| 6261 |
|
| 6262 |
You can use the ":copen" or ":cwindow" command to open the quickfix window |
| 6263 |
that contains the compiler output. You can select different error lines |
| 6264 |
from this window and jump to the corresponding line in the source code. |
| 6265 |
|
| 6266 |
For more information, read |
| 6267 |
|
| 6268 |
:help :copen |
| 6269 |
:help :cwindow |
| 6270 |
:help quickfix |
| 6271 |
|
| 6272 |
|
| 6273 |
============================================================================= |
| 6274 |
|
| 6275 |
SECTION 29 - FOLDING |
| 6276 |
|
| 6277 |
|
| 6278 |
29.1. How do I extend the Vim folding support? |
| 6279 |
|
| 6280 |
You can use the 'foldexpr' option to fold using an user specified function. |
| 6281 |
For example, to fold subroutines of the following form into a single line: |
| 6282 |
|
| 6283 |
sub foo { |
| 6284 |
my $barf; |
| 6285 |
$barf = 3; |
| 6286 |
return $barf; |
| 6287 |
} |
| 6288 |
|
| 6289 |
You can use the following commands: |
| 6290 |
|
| 6291 |
set foldmethod=expr |
| 6292 |
set foldexpr=MyFoldExpr(v:lnum) |
| 6293 |
fun! MyFoldExpr(line) |
| 6294 |
let str = getline(a:line) |
| 6295 |
if str =~ '^sub\>' |
| 6296 |
return '1' |
| 6297 |
elseif str =~ '^}' |
| 6298 |
return '<1' |
| 6299 |
else |
| 6300 |
return foldlevel(a:line - 1) |
| 6301 |
endif |
| 6302 |
endfun |
| 6303 |
|
| 6304 |
For more information, read |
| 6305 |
|
| 6306 |
:help 'foldexpr' |
| 6307 |
:help fold-expr |
| 6308 |
|
| 6309 |
|
| 6310 |
29.2. When I enable folding by setting the 'foldmethod' option, all the |
| 6311 |
folds are closed. How do I prevent this? |
| 6312 |
|
| 6313 |
You can set the 'foldlevelstart' option to a particular value to close only |
| 6314 |
folds above the specified value. |
| 6315 |
|
| 6316 |
:set foldlevelstart=99 |
| 6317 |
|
| 6318 |
For more information, read |
| 6319 |
|
| 6320 |
:help 'foldlevelstart' |
| 6321 |
:help 'foldlevel' |
| 6322 |
:help fold-foldlevel |
| 6323 |
|
| 6324 |
|
| 6325 |
29.3. How do I control how many folds will be opened when I start editing a |
| 6326 |
file? |
| 6327 |
|
| 6328 |
You can modify the 'foldlevelstart' option to control the number of folds |
| 6329 |
that will be opened when you start editing a file. To start editing with |
| 6330 |
all the folds closed: |
| 6331 |
|
| 6332 |
:set foldlevelstart=0 |
| 6333 |
|
| 6334 |
To start editing with all the folds opened, you can use |
| 6335 |
|
| 6336 |
:set foldlevelstart=999 |
| 6337 |
|
| 6338 |
For more information, read |
| 6339 |
|
| 6340 |
:help 'foldlevelstart' |
| 6341 |
|
| 6342 |
|
| 6343 |
29.4. How do I open and close folds using the mouse? |
| 6344 |
|
| 6345 |
You can click on the + and - characters displayed at the leftmost column to |
| 6346 |
open and close fold. For this to work, you have to set the 'foldcolumn' |
| 6347 |
to a value greater than zero: |
| 6348 |
|
| 6349 |
:set foldcolumn=2 |
| 6350 |
|
| 6351 |
For more information, read |
| 6352 |
|
| 6353 |
:help 'foldcolumn' |
| 6354 |
|
| 6355 |
|
| 6356 |
29.5. How do I change the text displayed for a closed fold? |
| 6357 |
|
| 6358 |
You can use the 'foldtext' option to change the text displayed for a closed |
| 6359 |
fold. |
| 6360 |
|
| 6361 |
For more information, read |
| 6362 |
|
| 6363 |
:help 'foldtext' |
| 6364 |
:help fold-foldtext |
| 6365 |
:help 'fillchars' |
| 6366 |
|
| 6367 |
|
| 6368 |
29.6. How do I store and restore manually created folds across different |
| 6369 |
Vim invocations? |
| 6370 |
|
| 6371 |
You can use the ":mkview" command to store manually created folds. Later, |
| 6372 |
you can use the ":loadview" command to restore the folds. For this to work, |
| 6373 |
the 'viewoptions' must contain "folds". |
| 6374 |
|
| 6375 |
For more information, read |
| 6376 |
|
| 6377 |
:help 28.4 |
| 6378 |
:help :mkview |
| 6379 |
:help :loadview |
| 6380 |
:help 'viewoptions' |
| 6381 |
:help 'viewdir' |
| 6382 |
:help :mksession |
| 6383 |
:help 'sessionoptions' |
| 6384 |
|
| 6385 |
|
| 6386 |
============================================================================= |
| 6387 |
|
| 6388 |
SECTION 30 - VIM WITH EXTERNAL APPLICATIONS |
| 6389 |
|
| 6390 |
|
| 6391 |
30.1. Can I run a shell inside a Vim window? |
| 6392 |
|
| 6393 |
Currently Vim doesn't have support for running shell and other external |
| 6394 |
commands inside a Vim window. |
| 6395 |
|
| 6396 |
For more information, read |
| 6397 |
|
| 6398 |
:help shell-window |
| 6399 |
|
| 6400 |
Alternatively, you can try using the Unix "screen" utility or the 'splitvt' |
| 6401 |
program. |
| 6402 |
|
| 6403 |
You can also use the vimsh plugin by Brian Sturk to run a shell in a Vim |
| 6404 |
window. To use this you need to have Vim built with python support. For |
| 6405 |
more information visit the following URL: |
| 6406 |
|
| 6407 |
http://vim.sourceforge.net/scripts/script.php?script_id=165 |
| 6408 |
|
| 6409 |
|
| 6410 |
30.2. How do I pass the word under the cursor to an external command? |
| 6411 |
|
| 6412 |
You can use the special keyword <cword> to pass the word under the cursor |
| 6413 |
to an external command. For example: |
| 6414 |
|
| 6415 |
:!dict <cword> |
| 6416 |
|
| 6417 |
For more information, read |
| 6418 |
|
| 6419 |
:help <cword> |
| 6420 |
|
| 6421 |
|
| 6422 |
30.3. How do I get the output of a shell command into a Vim buffer? |
| 6423 |
|
| 6424 |
You can use the ":r !" command to get the output of a shell command into a |
| 6425 |
Vim buffer. For example, to insert the output of the "ls" shell command, |
| 6426 |
you can use the following command: |
| 6427 |
|
| 6428 |
:r !ls |
| 6429 |
|
| 6430 |
To insert the output of the shell command above the first line use the |
| 6431 |
following command: |
| 6432 |
|
| 6433 |
:0r !ls |
| 6434 |
|
| 6435 |
For more information, read |
| 6436 |
|
| 6437 |
:help :r! |
| 6438 |
|
| 6439 |
|
| 6440 |
30.4. How do I pipe the contents of the current buffer to an external |
| 6441 |
command and replace the contents of the buffer with the output from |
| 6442 |
the command? |
| 6443 |
|
| 6444 |
You can use the :! command to pipe the contents of the current buffer to a |
| 6445 |
external command and replace the contents of the buffer with the output |
| 6446 |
from the command. For example, to sort the contents of the current buffer, |
| 6447 |
using the Unix sort command, you can use the following command: |
| 6448 |
|
| 6449 |
:%!sort |
| 6450 |
|
| 6451 |
To sort only lines 10-20, you can use the following command |
| 6452 |
|
| 6453 |
:10,20!sort |
| 6454 |
|
| 6455 |
Also, if you want to pipe a buffer to an external command but not put the |
| 6456 |
results back in the buffer, you can use |
| 6457 |
|
| 6458 |
:w !sort |
| 6459 |
|
| 6460 |
The above command will pipe the entire buffer to the sort command. Note |
| 6461 |
that the space between the 'w' and the '!' is critical. To pipe only a |
| 6462 |
range of lines, you can use |
| 6463 |
|
| 6464 |
:10,20w !sort |
| 6465 |
|
| 6466 |
The above command will pipe the lines 10-20 to the sort command. |
| 6467 |
|
| 6468 |
For more information, read |
| 6469 |
|
| 6470 |
:help :range! |
| 6471 |
:help 10.9 |
| 6472 |
:help :w_c |
| 6473 |
|
| 6474 |
|
| 6475 |
30.5. How do I sort a section of my file? |
| 6476 |
|
| 6477 |
You can pipe a section of the file to the Unix "sort" utility to sort the |
| 6478 |
file. For example: |
| 6479 |
|
| 6480 |
:5,100!sort |
| 6481 |
|
| 6482 |
You can also use a visual block, and use the "!sort" command on the |
| 6483 |
selected block. |
| 6484 |
|
| 6485 |
To sort using visual blocks (sort based on a column or sort just the column |
| 6486 |
itself), read the following tip from the Vim online web page: |
| 6487 |
|
| 6488 |
http://vim.sourceforge.net/tips/tip.php?tip_id=588 |
| 6489 |
|
| 6490 |
|
| 6491 |
30.6. Is there a step-by-step guide for using Vim with slrn? |
| 6492 |
|
| 6493 |
Visit the following link to get information about using Vim with Slrn: |
| 6494 |
|
| 6495 |
http://thingy.apana.org.au/~fun/slrn/ |
| 6496 |
|
| 6497 |
|
| 6498 |
30.7. How do I use Vim as a pager? |
| 6499 |
|
| 6500 |
You can use Vim as a pager using the $VIMRUNTIME/macros/less.sh shell |
| 6501 |
script, supplied as part of the standard Vim distribution. This shell |
| 6502 |
script uses the $VIMRUNTIME/macros/less.vim Vim script to provide less like |
| 6503 |
key bindings. |
| 6504 |
|
| 6505 |
For more information, read |
| 6506 |
|
| 6507 |
:help less |
| 6508 |
|
| 6509 |
|
| 6510 |
30.8. How do I view Unix man pages from inside Vim? |
| 6511 |
|
| 6512 |
You can view Unix man pages, inside Vim, using the man.vim plugin supplied |
| 6513 |
as part of the standard Vim distribution. To use this plugin, add the |
| 6514 |
following line to your startup vimrc file: |
| 6515 |
|
| 6516 |
runtime ftplugin/man.vim |
| 6517 |
|
| 6518 |
You can also press the K key to run the program specified by the |
| 6519 |
'keywordprg' option with the keyword under the cursor. By default, |
| 6520 |
'keywordprg' is set to run man on the keyword under the cursor. |
| 6521 |
|
| 6522 |
For more information, read |
| 6523 |
|
| 6524 |
:help man-plugin |
| 6525 |
:help K |
| 6526 |
:help 'keywordprg' |
| 6527 |
|
| 6528 |
|
| 6529 |
30.9. How do I change the diff command used by the Vim diff support? |
| 6530 |
|
| 6531 |
By default, the Vim diff support uses the 'diff' command. You can change |
| 6532 |
this by changing the 'diffexpr' option. |
| 6533 |
|
| 6534 |
For more information, read |
| 6535 |
|
| 6536 |
:help diff-diffexpr |
| 6537 |
:help 'diffexpr' |
| 6538 |
|
| 6539 |
|
| 6540 |
30.10. How do I use the Vim diff mode without folding? |
| 6541 |
|
| 6542 |
You can use the following command-line to start Vim with two filenames |
| 6543 |
and use the diff mode without folding: |
| 6544 |
|
| 6545 |
$ vim -o file1 file2 "+windo set diff scrollbind scrollopt+=hor nowrap" |
| 6546 |
|
| 6547 |
If you like vertically split windows, then replace "-o" with "-O". |
| 6548 |
|
| 6549 |
For more information, read |
| 6550 |
|
| 6551 |
:help vimdiff |
| 6552 |
|
| 6553 |
|
| 6554 |
============================================================================= |
| 6555 |
|
| 6556 |
SECTION 31 - GUI VIM |
| 6557 |
|
| 6558 |
|
| 6559 |
31.1. How do I create buffer specific menus? |
| 6560 |
|
| 6561 |
Adding support for buffer specific menus is in the Vim TODO list. In the |
| 6562 |
mean time, you can try Michael Geddes's plugin, buffermenu.vim: |
| 6563 |
|
| 6564 |
http://vim.sourceforge.net/scripts/script.php?script_id=246 |
| 6565 |
|
| 6566 |
|
| 6567 |
31.2. How do I change the font used by GUI Vim? |
| 6568 |
|
| 6569 |
You can change the 'guifont' option to change the font used by GUI Vim. To |
| 6570 |
display the current value of this option, you can use |
| 6571 |
|
| 6572 |
:set guifont? |
| 6573 |
|
| 6574 |
You can add the displayed font name to the .vimrc file to use the font |
| 6575 |
across Vim sessions. For example, add the following line to the .vimrc file |
| 6576 |
to use Andale Mono font. |
| 6577 |
|
| 6578 |
set guifont=Andale_Mono:h10:cANSI |
| 6579 |
|
| 6580 |
For Win32, GTK and Photon version of Vim, you can use the following command |
| 6581 |
to bringup a dialog which will help you in changing the guifont: |
| 6582 |
|
| 6583 |
:set guifont=* |
| 6584 |
|
| 6585 |
You can also use the -font Vim command line option to specify the font used |
| 6586 |
for normal text. |
| 6587 |
|
| 6588 |
For more information, read |
| 6589 |
|
| 6590 |
:help 'guifont' |
| 6591 |
:help 'guifontset' |
| 6592 |
:help 'guifontwide' |
| 6593 |
:help font-sizes |
| 6594 |
:help -font |
| 6595 |
:help -boldfont |
| 6596 |
:help -italicfont |
| 6597 |
:help -menufont |
| 6598 |
:help -menufontset |
| 6599 |
|
| 6600 |
|
| 6601 |
31.3. When starting GUI Vim, how do I specify the location of the GVIM |
| 6602 |
window? |
| 6603 |
|
| 6604 |
You can use the "-geometry" command line argument to specify the location |
| 6605 |
of the GUI Vim window. For example: |
| 6606 |
|
| 6607 |
$ gvim -geometry 80x25+100+300 |
| 6608 |
|
| 6609 |
For more information, read |
| 6610 |
|
| 6611 |
:help 31.4 |
| 6612 |
:help -geom |
| 6613 |
|
| 6614 |
|
| 6615 |
31.4. How do I add a horizontal scrollbar in GVim? |
| 6616 |
|
| 6617 |
You can enable the horizontal scrollbar by modifying the 'guioptions' |
| 6618 |
option: |
| 6619 |
|
| 6620 |
:set guioptions+=b |
| 6621 |
|
| 6622 |
For more information, read |
| 6623 |
|
| 6624 |
:help 'guioptions' |
| 6625 |
:help gui-horiz-scroll |
| 6626 |
|
| 6627 |
|
| 6628 |
31.5. How do I make the scrollbar appear in the left side by default? |
| 6629 |
|
| 6630 |
You can add the 'l' flag to the 'guioptions' option to make the scrollbar |
| 6631 |
appear in the left side. |
| 6632 |
|
| 6633 |
:set guioptions+=l |
| 6634 |
:set guioptions-=r |
| 6635 |
|
| 6636 |
For more information, read |
| 6637 |
|
| 6638 |
:help 'guioptions' |
| 6639 |
:help gui-scrollbars |
| 6640 |
|
| 6641 |
|
| 6642 |
31.6. How do I remove the Vim menubar? |
| 6643 |
|
| 6644 |
You can remove the Vim menubar by removing the 'm' flag from the |
| 6645 |
'guioptions' option: |
| 6646 |
|
| 6647 |
:set guioptions-=m |
| 6648 |
|
| 6649 |
For more information, read |
| 6650 |
|
| 6651 |
:help 'guioptions' |
| 6652 |
|
| 6653 |
|
| 6654 |
31.7. I am using GUI Vim. When I press the ALT key and a letter, the menu |
| 6655 |
starting with that letter is selected. I don't want this behavior as |
| 6656 |
I want to map the ALT-<key> combination. How do I do this? |
| 6657 |
|
| 6658 |
You can use the 'winaltkeys' option to disable the use of the ALT key to |
| 6659 |
select a menu item: |
| 6660 |
|
| 6661 |
:set winaltkeys=no |
| 6662 |
|
| 6663 |
For more information, read |
| 6664 |
|
| 6665 |
:help 'winaltkeys' |
| 6666 |
:help :simalt |
| 6667 |
|
| 6668 |
|
| 6669 |
31.8. Is it possible to scroll the text by dragging the scrollbar so that |
| 6670 |
the cursor stays in the original location? |
| 6671 |
|
| 6672 |
The way Vim is designed, the cursor position has to be in a visible spot in |
| 6673 |
normal, visual, select and insert mode. This cannot be changed without |
| 6674 |
modifying Vim. When the scrollbar is used, the cursor will be moved so that |
| 6675 |
it is always visible. Another approach to solving this problem is to use |
| 6676 |
the Vim marks. You can mark the current cursor position using ma. Then |
| 6677 |
scroll to a different part of the text and jump back to the old position |
| 6678 |
using `a. You can also try the following suggestion from the Vim Online |
| 6679 |
website: |
| 6680 |
|
| 6681 |
http://www.vim.org/tip_view.php?tip_id=320 |
| 6682 |
|
| 6683 |
For more information, read |
| 6684 |
|
| 6685 |
:help mark-motions |
| 6686 |
|
| 6687 |
|
| 6688 |
31.9. How do I get gvim to start browsing files in a particular directory |
| 6689 |
when using the ":browse" command? |
| 6690 |
|
| 6691 |
You can set the 'browsedir' option to the default directory to use for the |
| 6692 |
":browse" command. |
| 6693 |
|
| 6694 |
:set browsedir='<your_dir>' |
| 6695 |
|
| 6696 |
For more information, read |
| 6697 |
|
| 6698 |
:help 'browsedir' |
| 6699 |
|
| 6700 |
|
| 6701 |
31.10. For some questions, like when a file is changed outside of Vim, Vim |
| 6702 |
displays a GUI dialog box. How do I replace this GUI dialog box with |
| 6703 |
a console dialog box? |
| 6704 |
|
| 6705 |
You can set the 'c' flag in the 'guioptions' option to configure Vim to use |
| 6706 |
console dialogs instead of GUI dialogs: |
| 6707 |
|
| 6708 |
:set guioptions+=c |
| 6709 |
|
| 6710 |
For more information, read |
| 6711 |
|
| 6712 |
:help 'guioptions' |
| 6713 |
|
| 6714 |
|
| 6715 |
31.11. I am trying to use GUI Vim as the editor for my xxx application. |
| 6716 |
When the xxx application launches GUI Vim to edit a file, the |
| 6717 |
control immediately returns to the xxx application. How do I start |
| 6718 |
GUI Vim, so that the control returns to the xxx application only |
| 6719 |
after I quit Vim? |
| 6720 |
|
| 6721 |
You have to start GUI Vim with the '-f' (foreground) command line option: |
| 6722 |
|
| 6723 |
$ gvim -f |
| 6724 |
|
| 6725 |
By default, GUI Vim will disconnect from the program that started Vim. With |
| 6726 |
the '-f' option, GUI Vim will not disconnect from the program that started |
| 6727 |
it. |
| 6728 |
|
| 6729 |
For more information, read |
| 6730 |
|
| 6731 |
:help gui-fork |
| 6732 |
:help -f |
| 6733 |
|
| 6734 |
|
| 6735 |
31.12. Why does the "Select Font" dialog doesn't show all the fonts |
| 6736 |
installed in my system? |
| 6737 |
|
| 6738 |
Vim supports only fixed width (mono-spaced) fonts. Proportional fonts are |
| 6739 |
not supported. In the "Select Font" dialog, only fixed width fonts will be |
| 6740 |
displayed. |
| 6741 |
|
| 6742 |
For more information, read |
| 6743 |
|
| 6744 |
:help font-sizes |
| 6745 |
:help 'guifont' |
| 6746 |
|
| 6747 |
|
| 6748 |
31.13. How do I use the mouse in Vim command-line mode? |
| 6749 |
|
| 6750 |
You can set the 'c' flag in the 'mouse' option to use mouse in the Vim |
| 6751 |
command-line mode: |
| 6752 |
|
| 6753 |
:set mouse+=c |
| 6754 |
|
| 6755 |
For more information, read |
| 6756 |
|
| 6757 |
:help mouse-using |
| 6758 |
:help gui-mouse |
| 6759 |
:help 09.2 |
| 6760 |
|
| 6761 |
|
| 6762 |
31.14. When I use the middle mouse button to scroll text, it pastes the |
| 6763 |
last copied text. How do I disable this behavior? |
| 6764 |
|
| 6765 |
You can map the middle mouse button to <Nop> to disable the middle mouse |
| 6766 |
button: |
| 6767 |
|
| 6768 |
:map <MiddleMouse> <Nop> |
| 6769 |
:map! <MiddleMouse> <Nop> |
| 6770 |
|
| 6771 |
For more information, read |
| 6772 |
|
| 6773 |
:help gui-mouse-mapping |
| 6774 |
:help <Nop> |
| 6775 |
|
| 6776 |
|
| 6777 |
31.15. How do I change the location and size of a GUI Vim window? |
| 6778 |
|
| 6779 |
You can use the "winpos" command to change the Vim window position. To |
| 6780 |
change the size of the window, you can modify the "lines" and "columns" |
| 6781 |
options. |
| 6782 |
|
| 6783 |
For example, the following commands will position the GUI Vim window at the |
| 6784 |
X,Y co-ordinates 50,50 and set the number of lines to 50 and the number of |
| 6785 |
columsn to 80. |
| 6786 |
|
| 6787 |
:winpos 50 50 |
| 6788 |
:set lines=50 |
| 6789 |
:set columns=80 |
| 6790 |
|
| 6791 |
The arguments to the 'winpos' command specify the pixel co-ordinates of the |
| 6792 |
Vim window. The 'lines' and 'columns' options specify the number of lines |
| 6793 |
and characters to use for the height and the width of the window |
| 6794 |
respectively. |
| 6795 |
|
| 6796 |
For more information, read |
| 6797 |
|
| 6798 |
:help 31.4 |
| 6799 |
:help :winpos |
| 6800 |
:help 'lines' |
| 6801 |
:help 'columns' |
| 6802 |
:help GUIEnter |
| 6803 |
|
| 6804 |
|
| 6805 |
============================================================================= |
| 6806 |
|
| 6807 |
SECTION 32 - VIM ON UNIX |
| 6808 |
|
| 6809 |
|
| 6810 |
32.1. I am running Vim in a xterm. When I press the CTRL-S key, Vim |
| 6811 |
freezes. What should I do now? |
| 6812 |
|
| 6813 |
Many terminal emulators and real terminal drivers use the CTRL-S key to |
| 6814 |
stop the data from arriving so that you can stop a fast scrolling display |
| 6815 |
to look at it (also allowed older terminals to slow down the computer so |
| 6816 |
that it did not get buffer overflows). You can start the output again by |
| 6817 |
pressing the CTRL-Q key. |
| 6818 |
|
| 6819 |
When you press the CTRL-S key, the terminal driver will stop sending the |
| 6820 |
output data. As a result of this, it will look like Vim is hung. If you |
| 6821 |
press the CTRL-Q key, then everything will be back to normal. |
| 6822 |
|
| 6823 |
You can turn off the terminal driver flow control using the 'stty' command: |
| 6824 |
|
| 6825 |
$ stty -ixon -ixoff |
| 6826 |
|
| 6827 |
or, you can change the keys used for the terminal flow control, using the |
| 6828 |
following commands: |
| 6829 |
|
| 6830 |
$ stty stop <char> |
| 6831 |
$ stty start <char> |
| 6832 |
|
| 6833 |
|
| 6834 |
32.2. I am seeing weird screen update problems in Vim. What can I do to |
| 6835 |
solve this screen/display update problems? |
| 6836 |
|
| 6837 |
You have to use a proper terminal emulator like xterm with correct TERM |
| 6838 |
settings (TERM=xterm) and a correct terminfo/termcap file. |
| 6839 |
For more information, read |
| 6840 |
|
| 6841 |
:help 'term' |
| 6842 |
|
| 6843 |
|
| 6844 |
32.3. I am using the terminal/console version of Vim. In insertmode, When I |
| 6845 |
press the backspace key, the character before the cursor is not |
| 6846 |
erased. How do I configure Vim to do this? |
| 6847 |
|
| 6848 |
You have to make sure that Vim gets the correct keycode for the backpspace |
| 6849 |
key. You can try using the command: |
| 6850 |
|
| 6851 |
:fixdel |
| 6852 |
|
| 6853 |
Make sure the TERM environment variable is set to the correct terminal |
| 6854 |
name. You can try using the 'stty' command: |
| 6855 |
|
| 6856 |
$ stty erase ^H |
| 6857 |
|
| 6858 |
where, you have to enter the ^H character by pressing the CTRL-V key and |
| 6859 |
then the CTRL-H key. |
| 6860 |
|
| 6861 |
For more information, read |
| 6862 |
|
| 6863 |
:help :fixdel |
| 6864 |
:help Linux-backspace |
| 6865 |
:help NetBSD-backspace |
| 6866 |
|
| 6867 |
|
| 6868 |
32.4. I am using Vim in a xterm. When I quit Vim, the screen contents are |
| 6869 |
restored back to the original contents. How do I disable this? |
| 6870 |
|
| 6871 |
The xterm has a capability called "alternate screen". If this capability |
| 6872 |
is present, vim switches to that alternate screen upon startup and back on |
| 6873 |
exit, thus restoring the original screen contents. To disable this |
| 6874 |
feature, add the following line to your .vimrc file: |
| 6875 |
|
| 6876 |
:set t_ti= t_te= |
| 6877 |
|
| 6878 |
For more information, read |
| 6879 |
|
| 6880 |
:help restorescreen |
| 6881 |
:help xterm-screens |
| 6882 |
|
| 6883 |
|
| 6884 |
32.5. When I start Vim, it takes quite a few seconds to start. How do I |
| 6885 |
minimize the startup time? |
| 6886 |
|
| 6887 |
This may be related to Vim opening the X display for setting the xterm |
| 6888 |
title and using the X clipboard. Make sure the DISPLAY variable is set to |
| 6889 |
point to the correct host. Try using the command line: |
| 6890 |
|
| 6891 |
$ vim -X |
| 6892 |
|
| 6893 |
This will prevent Vim from opening the X display. With this command-line |
| 6894 |
option, the X clipboard cannot be used and also Vim will not be able to |
| 6895 |
change the xterm title. |
| 6896 |
|
| 6897 |
You can also set the 'clipboard' option to |
| 6898 |
|
| 6899 |
:set clipboard=exclude:.* |
| 6900 |
|
| 6901 |
This has the same effect as using the -X command-line argument. |
| 6902 |
|
| 6903 |
For more information, read |
| 6904 |
|
| 6905 |
:help -X |
| 6906 |
:help 'clipboard' |
| 6907 |
|
| 6908 |
|
| 6909 |
32.6. How can I make the cursor in gvim in unix stop blinking? |
| 6910 |
|
| 6911 |
You can modify the 'guicursor' option, to stop the cursor from blinking. |
| 6912 |
For example: |
| 6913 |
|
| 6914 |
:set guicursor=a:blinkon0 |
| 6915 |
|
| 6916 |
For more information, read |
| 6917 |
|
| 6918 |
:help 'guicursor' |
| 6919 |
|
| 6920 |
|
| 6921 |
32.7. How do I change the menu font on GTK Vim? |
| 6922 |
|
| 6923 |
You can modify the ~/.gtkrc file to change the menu font on GTK Vim. For |
| 6924 |
example: |
| 6925 |
|
| 6926 |
style "default" |
| 6927 |
{ font ="smooth09" } |
| 6928 |
class "*" style "default" |
| 6929 |
|
| 6930 |
The last line changes the font of all widgets. |
| 6931 |
|
| 6932 |
For more information, read |
| 6933 |
|
| 6934 |
:help gui-gtk |
| 6935 |
|
| 6936 |
|
| 6937 |
32.8. How do I prevent <Ctrl-Z> from suspending Vim? |
| 6938 |
|
| 6939 |
You can map <Ctrl-Z> to prevent the suspending. Here are some suggestions: |
| 6940 |
|
| 6941 |
- Make <Ctrl-Z> do nothing: |
| 6942 |
|
| 6943 |
:map <C-Z> <Nop> |
| 6944 |
|
| 6945 |
- Make <Ctrl-Z> start a shell: |
| 6946 |
|
| 6947 |
:map <C-Z> :shell<CR> |
| 6948 |
|
| 6949 |
- Make <Ctrl-Z> give an error message: |
| 6950 |
|
| 6951 |
:map <C-Z> :"suspending disabled<CR> |
| 6952 |
|
| 6953 |
For the last example, the double quote is necessary in order to keep the |
| 6954 |
message on the status line. |
| 6955 |
|
| 6956 |
|
| 6957 |
32.9. When I kill the xterm running Vim, the Vim process continues to run |
| 6958 |
and takes up a lot of CPU (99%) time. Why is this happening? |
| 6959 |
|
| 6960 |
When Vim is built with support for Python interface, you will have this |
| 6961 |
problem. This is a known problem with the python thread library and Vim. To |
| 6962 |
solve this problem, use a Vim binary built without the Python interface. |
| 6963 |
|
| 6964 |
For more information, read |
| 6965 |
|
| 6966 |
:help +python |
| 6967 |
:help python |
| 6968 |
|
| 6969 |
|
| 6970 |
32.10. How do I get the Vim syntax highlighting to work in a Unix terminal? |
| 6971 |
|
| 6972 |
The easiest and simplest way to get Vim syntax highlighting is to use the |
| 6973 |
GUI version of Vim (GVIM). To get syntax highlighting to work in the |
| 6974 |
console/terminal version of Vim, you have to run a terminal emulator (like |
| 6975 |
Xfree86 xterm or rxvt or dtterm) that supports color. Note that if a |
| 6976 |
terminal emulator supports changing the background and foreground colors, |
| 6977 |
that does not mean that it also supports ANSI escape sequences for changing |
| 6978 |
the color. You can download the latest version of Xfree86 xterm from |
| 6979 |
http://dickey.his.com/xterm/xterm.html. You can download the latest version |
| 6980 |
of rxvt from http://www.rxvt.org. You have to install the terminfo/termcap |
| 6981 |
file that supports colors for the terminal emulator. Also, set the TERM |
| 6982 |
environment variable to the correct name of the term that supports colors. |
| 6983 |
|
| 6984 |
You can use the colortest.vim script supplied with the Vim runtime |
| 6985 |
package to test the color setup. To use this script, follow these steps: |
| 6986 |
|
| 6987 |
:e $VIMRUNTIME/syntax/colortest.vim |
| 6988 |
:source % |
| 6989 |
|
| 6990 |
For more information, read |
| 6991 |
|
| 6992 |
:help 06.2 |
| 6993 |
:help terminal-colors |
| 6994 |
:help termcap-colors |
| 6995 |
:help startup-terminal |
| 6996 |
:help xterm-color |
| 6997 |
:help colortest.vim |
| 6998 |
|
| 6999 |
|
| 7000 |
============================================================================= |
| 7001 |
|
| 7002 |
SECTION 33 - VIM ON MS-WINDOWS |
| 7003 |
|
| 7004 |
|
| 7005 |
33.1. In MS-Windows, CTRL-V doesn't start the blockwise visual mode. What |
| 7006 |
happened? |
| 7007 |
|
| 7008 |
The mswin.vim script provides key mappings and options to make Vim behave |
| 7009 |
like a MS-Windows application. One of the keys mapped is CTRL-V which is |
| 7010 |
used for pasting text in MS-Windows applications. This will disable the use |
| 7011 |
of CTRL-V to start the blockwise visual mode. The mswin.vim script maps |
| 7012 |
CTRL-Q for staring the blockwise visual mode. So you can use CTRL-Q instead |
| 7013 |
of CTRL-V. |
| 7014 |
|
| 7015 |
For more information, read |
| 7016 |
|
| 7017 |
:help CTRL-V |
| 7018 |
:help CTRl-V-alternative |
| 7019 |
:help CTRL-Q |
| 7020 |
:help 10.5 |
| 7021 |
|
| 7022 |
|
| 7023 |
33.2. When I press the CTRL-Y key, it acts like the CTRL-R key. How do I |
| 7024 |
configure Vim to treat CTRL-Y as CTRL-Y? |
| 7025 |
|
| 7026 |
The mapping of the CTRL-Y key to the CTRL-R key is done by the mswin.vim |
| 7027 |
script. The mswin.vim script maps CTRL-Y to make Vim behave like a standard |
| 7028 |
MS-Windows application. This is explained in ":help CTRL-Y". You can either |
| 7029 |
comment out the line in mswin.vim that maps the CTRL-Y key or you can |
| 7030 |
remove the line in your .vimrc file that sources the mswin.vim script. |
| 7031 |
|
| 7032 |
|
| 7033 |
33.3. How do I start GUI Vim in a maximized window always? |
| 7034 |
|
| 7035 |
You can use the "simalt" command to maximize the Vim window. You can use |
| 7036 |
the GUIEnter autocmd to maximize the Vim window on startup: |
| 7037 |
|
| 7038 |
autocmd GUIEnter * simalt ~x |
| 7039 |
|
| 7040 |
For more information, read |
| 7041 |
|
| 7042 |
:help :simalt |
| 7043 |
:help GUIEnter |
| 7044 |
:help gui-win32-maximized |
| 7045 |
|
| 7046 |
|
| 7047 |
33.4. After doing some editing operations, Vim freezes. The cursor becomes |
| 7048 |
an empty rectangle. I am not able enter any characters. What is |
| 7049 |
happening? |
| 7050 |
|
| 7051 |
Most probably, you used the mouse wheel to scroll the text in Vim. There is |
| 7052 |
a known problem in using intellimouse mouse wheel with Vim. To avoid this |
| 7053 |
problem, disable Universal scrolling support for Vim. |
| 7054 |
|
| 7055 |
For more information, read |
| 7056 |
|
| 7057 |
:help intellimouse-wheel-problems |
| 7058 |
|
| 7059 |
|
| 7060 |
33.5. I am using Windows XP, the display speed of maximized GVim is very |
| 7061 |
slow. What can I do to speed the display updates? |
| 7062 |
|
| 7063 |
This may be due to the fact that you have enabled 'Smooth edges of screen |
| 7064 |
fonts' in the display properties. Try turning off font smoothing or try |
| 7065 |
changing the smoothing method to "Standard". |
| 7066 |
|
| 7067 |
|
| 7068 |
33.6. What are the recommended settings for using Vim with cygwin? |
| 7069 |
|
| 7070 |
You may want to set the following shell related Vim settings: |
| 7071 |
|
| 7072 |
:set shellcmdflag=-c |
| 7073 |
:set shellquote= |
| 7074 |
:set shellslash " Use the forward slash for expansion. |
| 7075 |
:set shellxquote=\" |
| 7076 |
:set shell=d:\cygwin\bin\bash.exe " Use the bash shell |
| 7077 |
:set shellpipe=2>&1| tee |
| 7078 |
:set shellredir=>%s 2>&1 |
| 7079 |
|
| 7080 |
|
| 7081 |
33.7. I am trying to use GNU diff with Vim diff mode. When I run the diff |
| 7082 |
from command line, it works. When I try to use the diff with Vim it |
| 7083 |
doesn't work. What should I do now? |
| 7084 |
|
| 7085 |
There is a problem with using GNU diff with Vim. You can try using the |
| 7086 |
GNU diff.exe built by Ron Aaron from the following link: |
| 7087 |
|
| 7088 |
http://www.mossbayeng.com/~ron/vim/builds.html |
| 7089 |
|
| 7090 |
|
| 7091 |
33.8. Is it possible to use Vim as an external editor for MS-Windows |
| 7092 |
Outlook email client? |
| 7093 |
|
| 7094 |
You can use the "cubiclevim" COM Add-In to use Vim as an external editor |
| 7095 |
for MS-Windows Outlook email client. Visit the following URL for more |
| 7096 |
information: |
| 7097 |
|
| 7098 |
http://sourceforge.net/projects/cubiclevim |
| 7099 |
|
| 7100 |
Note that currently this works only with MS-Office 2000 and XP. |
| 7101 |
|
| 7102 |
|
| 7103 |
33.9. I am using Vim to edit HTML files. How do I start internet explorer |
| 7104 |
with the current file to preview the HTML file? |
| 7105 |
|
| 7106 |
You can use the following command: |
| 7107 |
|
| 7108 |
:!start c:\progra~1\intern~1\iexplore.exe file://%:p<CR> |
| 7109 |
|
| 7110 |
|
| 7111 |
33.10. I would like to use Vim with Microsoft Visual Studio. How do I do |
| 7112 |
this? |
| 7113 |
|
| 7114 |
You have to download and use the OLE version of Vim (for example: |
| 7115 |
gvim61ole.zip). This file also contains instructions on how to use Vim with |
| 7116 |
Visual Studio. |
| 7117 |
|
| 7118 |
For more information, read |
| 7119 |
|
| 7120 |
:help MSVisualStudio |
| 7121 |
|
| 7122 |
|
| 7123 |
33.11. Where do I place the _vimrc and _gvimrc files? |
| 7124 |
|
| 7125 |
You can place the _vimrc and _gvimrc files under the directory pointed to |
| 7126 |
by the VIM environment variable. If you are sharing this system with other |
| 7127 |
users, then you can place the files in a directory and set the HOME |
| 7128 |
environment variable to this directory. |
| 7129 |
|
| 7130 |
For more information, read |
| 7131 |
|
| 7132 |
:help $HOME-use |
| 7133 |
:help _vimrc |
| 7134 |
|
| 7135 |
|
| 7136 |
33.12. Everytime I save a file, Vim warns about the file being changed |
| 7137 |
outside of Vim. Why? |
| 7138 |
|
| 7139 |
If you get the following warning message, everytime you save a file: |
| 7140 |
|
| 7141 |
WARNING: The file has been changed since reading it!!! |
| 7142 |
Do you really want to write to it (y/n)? |
| 7143 |
|
| 7144 |
then this problem could be related to a bug in MS-Windows on the day |
| 7145 |
daylight saving time starts. Vim remembers the timestamp of the file after |
| 7146 |
it was written. Just before the next write the timestamp is obtained again |
| 7147 |
to check if the file was changed outside of Vim. This works correctly, |
| 7148 |
except on the day daylight saving time starts. |
| 7149 |
|
| 7150 |
This problem will go away the next day after the day the daylight saving |
| 7151 |
time starts. |
| 7152 |
|
| 7153 |
For more information, read |
| 7154 |
|
| 7155 |
:help W11 |
| 7156 |
|
| 7157 |
|
| 7158 |
============================================================================= |
| 7159 |
|
| 7160 |
SECTION 34 - PRINTING |
| 7161 |
|
| 7162 |
|
| 7163 |
34.1. How do I print a file along with line numbers for all the lines? |
| 7164 |
|
| 7165 |
You can set the 'printoptions' option and use the ":hardcopy" command to |
| 7166 |
print your file: |
| 7167 |
|
| 7168 |
:set printoptions=number:y |
| 7169 |
:hardcopy |
| 7170 |
|
| 7171 |
For more information, read |
| 7172 |
|
| 7173 |
:help 'printoptions' |
| 7174 |
:help :hardcopy |
| 7175 |
|
| 7176 |
|
| 7177 |
34.2. How do I print a file with the Vim syntax highlighting colors? |
| 7178 |
|
| 7179 |
You can use the ":hardcopy" command to print a file with the Vim syntax |
| 7180 |
highlighting colors. You can also convert your file to a HTML file using |
| 7181 |
the 2html.vim script and print the HTML file. |
| 7182 |
|
| 7183 |
For more information, read |
| 7184 |
|
| 7185 |
:help syntax-printing |
| 7186 |
:help 2html.vim |
| 7187 |
:help :hardcopy |
| 7188 |
:help printing |
| 7189 |
|
| 7190 |
|
| 7191 |
============================================================================= |
| 7192 |
|
| 7193 |
SECTION 35 - BUILDING VIM FROM SOURCE |
| 7194 |
|
| 7195 |
|
| 7196 |
35.1. How do I build Vim from the sources on a Unix system? |
| 7197 |
|
| 7198 |
For a Unix system, follow these steps to build Vim from the sources: |
| 7199 |
|
| 7200 |
- Download the source and run-time files archive (vim-##.tar.bz2) from the |
| 7201 |
ftp://ftp.vim.org/pub/vim/unix directory. |
| 7202 |
- Extract the archive using the bzip2 and tar utilities using the command: |
| 7203 |
|
| 7204 |
$ bunzip2 -c <filename> | tar -xf - |
| 7205 |
|
| 7206 |
- Run the 'make' command to configure and build Vim with the default |
| 7207 |
configuration. |
| 7208 |
- Run 'make install' command to install Vim in the default directory. |
| 7209 |
|
| 7210 |
To enable/disable various Vim features, before running the 'make' command |
| 7211 |
you can run the 'configure' command with different flags to include/exclude |
| 7212 |
the various Vim features. To list all the available options for the |
| 7213 |
'configure' command, use: |
| 7214 |
|
| 7215 |
$ configure -help |
| 7216 |
|
| 7217 |
For more information, read |
| 7218 |
|
| 7219 |
:help install |
| 7220 |
|
| 7221 |
|
| 7222 |
35.2. How do I install Vim in my home directory or a directory other |
| 7223 |
than the default installation directory in Unix? |
| 7224 |
|
| 7225 |
To install Vim in a directory other than the default installation |
| 7226 |
directory, you have to specify the directory using the --prefix option |
| 7227 |
while running the configure script. |
| 7228 |
|
| 7229 |
$ ./configure --prefix=/users/xyz |
| 7230 |
|
| 7231 |
You can enable/disable various Vim feature by supplying different arguments |
| 7232 |
to the configure script. For more information about all these options, run |
| 7233 |
|
| 7234 |
$ ./configure --help |
| 7235 |
|
| 7236 |
For more information, read |
| 7237 |
|
| 7238 |
:help install-home |
| 7239 |
:help install |
| 7240 |
|
| 7241 |
|
| 7242 |
35.3. How do I build Vim from the sources on a MS-Windows system? |
| 7243 |
|
| 7244 |
For a MS-Windows system, Vim can be built using either the Visual C++ |
| 7245 |
compiler or the Borland C++ compiler or the Ming GCC compiler or the cygwin |
| 7246 |
gcc compiler. Follow these steps to build Vim from the sources for |
| 7247 |
MS-Windows: |
| 7248 |
|
| 7249 |
- Download the source (vim##src.zip), runtime (vim##rt.zip) and the extra |
| 7250 |
(vim-##-extra.tar.gz) archives from the ftp://ftp.vim.org/pub/vim/pc |
| 7251 |
directory. |
| 7252 |
- Extract the archives into a directory (for example, c:\vimsrc) |
| 7253 |
- Depending on the installed compiler, you can use the corresponding |
| 7254 |
makefile to build the Vim sources. For Visual C++ use the Make_mvc.mak |
| 7255 |
makefile, for borland C++ use the Make_bc5.mak makefile, for ming GCC use |
| 7256 |
the Make_ming.mak makefile, for cygwin gcc use the Make_cyg.mak makefile. |
| 7257 |
|
| 7258 |
Depending on whether you want to build the GUI version of Vim or the |
| 7259 |
console version of Vim, you have to pass different arguments to the |
| 7260 |
makefiles. After successfully building the sources, you can copy the |
| 7261 |
vim.exe or gvim.exe file to the desired directory along with the files from |
| 7262 |
the runtime archive. |
| 7263 |
|
| 7264 |
You can visit the following site for extensive information about building |
| 7265 |
Vim on a MS-Windows system using the various compilers: |
| 7266 |
|
| 7267 |
http://mywebpage.netscape.com/SharpPeople/vim/howto/index.html |
| 7268 |
|
| 7269 |
For more information, read |
| 7270 |
|
| 7271 |
:help install |
| 7272 |
|
| 7273 |
|
| 7274 |
35.4. The Vim help, syntax, indent files are missing from my Vim |
| 7275 |
installation. How do I install these files? |
| 7276 |
|
| 7277 |
The Vim help, syntax, indent and other runtime files are part of the Vim |
| 7278 |
runtime package. You need to download and install the Vim runtime package. |
| 7279 |
For example, for MS-Windows, the name of the Vim 6.1 runtime package is |
| 7280 |
vim61rt.zip. |
| 7281 |
|
| 7282 |
For more information, read |
| 7283 |
|
| 7284 |
:help install |
| 7285 |
|
| 7286 |
|
| 7287 |
35.5. I have built Vim from the source and installed the Vim package using |
| 7288 |
"make install". Do I need to keep the Vim source directory? |
| 7289 |
|
| 7290 |
No. Once you have built and installed Vim in some directory other than the |
| 7291 |
original source directory (for example, /usr/bin or /usr/local/bin), then |
| 7292 |
you can remove the source directory. |
| 7293 |
|
| 7294 |
|
| 7295 |
35.6. How do I determine the Vim features which are enabled at compile |
| 7296 |
time? |
| 7297 |
|
| 7298 |
You can use the ":version" command to determine the Vim features that are |
| 7299 |
enabled at compile time. The features that are enabled will be prefixed |
| 7300 |
with a "+". The features that are not enabled will be prefixed with a "-". |
| 7301 |
|
| 7302 |
If you want to test for a feature in a script, you can use the has() |
| 7303 |
function: |
| 7304 |
|
| 7305 |
if has("menu") |
| 7306 |
" Set up some menus |
| 7307 |
endif |
| 7308 |
|
| 7309 |
For more information, read |
| 7310 |
|
| 7311 |
:help :version |
| 7312 |
:help +feature-list |
| 7313 |
:help has() |
| 7314 |
|
| 7315 |
|
| 7316 |
35.7. Can I build Vim without the GUI support? |
| 7317 |
|
| 7318 |
Yes. You can build Vim by optionally enabling/disabling many of the |
| 7319 |
features including GUI. |
| 7320 |
|
| 7321 |
For more information, read |
| 7322 |
|
| 7323 |
:help install |
| 7324 |
|
| 7325 |
|
| 7326 |
35.8. When building Vim on a Unix system, I am getting "undefined reference |
| 7327 |
to term_set_winsize' error. How do I resolve this error? |
| 7328 |
|
| 7329 |
You will get this error when the build process is not able to locate the |
| 7330 |
termlib, termcap or ncurses library. You have to install the ncurses-dev |
| 7331 |
package to resolve this error. |
| 7332 |
|
| 7333 |
|
| 7334 |
35.9. Vim configure keeps complaining about the lack of gtk-config while |
| 7335 |
trying to use GTK 2.03. This is correct, since in GTK 2 they moved to |
| 7336 |
using the generic pkg-config. I can get pkg-config to list the |
| 7337 |
various includes and libs for gtk, but for some reason the configure |
| 7338 |
script still isn't picking this up. |
| 7339 |
|
| 7340 |
Use the following shell script named gtk-config: |
| 7341 |
|
| 7342 |
#!/bin/sh |
| 7343 |
pkg-config gtk+-2.0 $1 $2 |
| 7344 |
|
| 7345 |
|
| 7346 |
============================================================================= |
| 7347 |
|
| 7348 |
SECTION 36 - VARIOUS |
| 7349 |
|
| 7350 |
|
| 7351 |
36.1. How do I edit binary files with Vim? |
| 7352 |
|
| 7353 |
You can set the following options to edit binary files in Vim: |
| 7354 |
|
| 7355 |
:set binary |
| 7356 |
:set display=uhex |
| 7357 |
|
| 7358 |
You can also use the "-b" command-line option to edit a binary file: |
| 7359 |
|
| 7360 |
$ vim -b <binary_file_name> |
| 7361 |
|
| 7362 |
You can also use the xxd utility (part of the Vim distribution) to edit |
| 7363 |
binary files. |
| 7364 |
|
| 7365 |
For more information, read |
| 7366 |
|
| 7367 |
:help 23.4 |
| 7368 |
:help edit-binary |
| 7369 |
:help hex-editing |
| 7370 |
:help -b |
| 7371 |
:help 'binary' |
| 7372 |
:help 'endofline' |
| 7373 |
:help 'display' |
| 7374 |
|
| 7375 |
|
| 7376 |
36.2. How do I disable the visual error flash and the error beep? |
| 7377 |
|
| 7378 |
You can disable both the visual error flash and the error beep using the |
| 7379 |
following command: |
| 7380 |
|
| 7381 |
:set visualbell t_vb= |
| 7382 |
|
| 7383 |
For more information, read |
| 7384 |
|
| 7385 |
:help 'visualbell' |
| 7386 |
:help 'errorbells' |
| 7387 |
:help t_vb |
| 7388 |
|
| 7389 |
|
| 7390 |
36.3. How do I display the ascii value of a character displayed in a |
| 7391 |
buffer? |
| 7392 |
|
| 7393 |
You can use the 'ga' command to display the ascii value of a displayed |
| 7394 |
character. |
| 7395 |
|
| 7396 |
For more information, read |
| 7397 |
|
| 7398 |
:help ga |
| 7399 |
:help :ascii |
| 7400 |
|
| 7401 |
|
| 7402 |
36.4. Can I use zero as a count for a Vim command? |
| 7403 |
|
| 7404 |
You cannot use zero as a count for a Vim command, as "0" is a command on |
| 7405 |
its own, moving to the first column of the line. |
| 7406 |
|
| 7407 |
For more information, read |
| 7408 |
|
| 7409 |
:help 0 |
| 7410 |
:help count |
| 7411 |
|
| 7412 |
|
| 7413 |
36.5. How do I disable the Vim welcome screen? |
| 7414 |
|
| 7415 |
You can disable the Vim welcome screen, by adding the 'I' flag to the |
| 7416 |
'shortmess' option: |
| 7417 |
|
| 7418 |
:set shortmess+=I |
| 7419 |
|
| 7420 |
For more information, read |
| 7421 |
|
| 7422 |
:help :intro |
| 7423 |
:help 'shortmess' |
| 7424 |
|
| 7425 |
|
| 7426 |
36.6. How do I avoid the "hit enter to continue" prompt? |
| 7427 |
|
| 7428 |
Vim will prompt you with the "hit enter to continue" prompt, if there are |
| 7429 |
some messages on the screen for you to read and the screen is about to be |
| 7430 |
redrawn. You can add the 'T' flag to the 'shortmess' option to truncate |
| 7431 |
all messages. This will help in avoiding the hit-enter prompt: |
| 7432 |
|
| 7433 |
:set shortmess+=T |
| 7434 |
|
| 7435 |
You can also increase the command height by setting the 'cmdheight' option: |
| 7436 |
|
| 7437 |
:set cmdheight=2 |
| 7438 |
|
| 7439 |
For more information, read |
| 7440 |
|
| 7441 |
:help hit-enter |
| 7442 |
:help avoid-hit-enter |
| 7443 |
:help 'shortmess' |
| 7444 |
:help 'cmdheight' |
| 7445 |
|
| 7446 |
|
| 7447 |
36.7. How do I invoke Vim from command line to run a group of commands on a |
| 7448 |
group of files? |
| 7449 |
|
| 7450 |
There are several ways to invoke Vim from command line to run a group of |
| 7451 |
commands on a group of files. You can use a set of "-c" command line |
| 7452 |
options to specify a group of commands: |
| 7453 |
|
| 7454 |
$ vim -c "<ex_command_1>" -c "<ex_command_2>" *.txt |
| 7455 |
|
| 7456 |
Each of the ex-command specified with the "-c" command line option is |
| 7457 |
executed one by one sequentially. You can also use a single "-c" command |
| 7458 |
line option and the "|" character to separate the ex commands: |
| 7459 |
|
| 7460 |
$ vim -c "<ex_command_1> | <ex_command_2>" *.txt |
| 7461 |
|
| 7462 |
In the above command, if an ex command fails, then all the remaining ex |
| 7463 |
commands will not be executed. |
| 7464 |
|
| 7465 |
For example, to replace "ABC" with "DEF" in a file from the command-line, |
| 7466 |
you can use the following command: |
| 7467 |
|
| 7468 |
$ vim -c "%s/ABC/DEF/ge | update" myfile.txt |
| 7469 |
|
| 7470 |
To replace "ABC" with "DEF" in multiple files from the command-line, |
| 7471 |
you can use the following command: |
| 7472 |
|
| 7473 |
$ vim -c "argdo %s/ABC/DEF/ge | update" *.txt |
| 7474 |
|
| 7475 |
You can store the group of commands into a file and use the "-s" command |
| 7476 |
line option to run the commands on a set of files. For example, if the |
| 7477 |
group of commands are stored in the file mycmds.txt, then you can use the |
| 7478 |
following command: |
| 7479 |
|
| 7480 |
$ vim -s mycmds.txt *.pl |
| 7481 |
|
| 7482 |
For more information, read |
| 7483 |
|
| 7484 |
:help -c |
| 7485 |
:help -s |
| 7486 |
|
| 7487 |
|
| 7488 |
36.8. How do I use a normal mode command from insert mode without leaving |
| 7489 |
the insert mode? |
| 7490 |
|
| 7491 |
You can use a normal command from insert mode, without leaving the insert |
| 7492 |
mode, by first pressing the CTRL-O key and then follow that with a single |
| 7493 |
normal mode command. |
| 7494 |
|
| 7495 |
To execute more than one normal mode command, press the CTRL-L key, |
| 7496 |
followed by any number of normal mode commands and then press <Esc> to get |
| 7497 |
back to the insert mode. |
| 7498 |
|
| 7499 |
For more information, read |
| 7500 |
|
| 7501 |
:help i_CTRL-O |
| 7502 |
:help i_CTRL-L |
| 7503 |
|
| 7504 |
|
| 7505 |
36.9. How do I start Vim in insert mode? |
| 7506 |
|
| 7507 |
You can start Vim in insert mode using the ":startinsert" ex command. |
| 7508 |
|
| 7509 |
$ vim +startinsert myfile.txt |
| 7510 |
|
| 7511 |
The above command will open the file "myfile.txt" and start insert mode |
| 7512 |
with the cursor in front of the first character on the first line. To open |
| 7513 |
the file and start appending after the last character on the last line, |
| 7514 |
you can use the following command: |
| 7515 |
|
| 7516 |
$ vim + +startinsert! myfile.txt |
| 7517 |
|
| 7518 |
For more information, read |
| 7519 |
|
| 7520 |
:help :startinsert |
| 7521 |
|
| 7522 |
|
| 7523 |
============================================================================= |
| 7524 |
|
| 7525 |
SECTION 37 - UNICODE |
| 7526 |
Author: Tony Mechelynck <antoine.mechelynck AT belgacom.net> |
| 7527 |
|
| 7528 |
|
| 7529 |
37.1. Is it possible to create Unicode files using Vim? |
| 7530 |
|
| 7531 |
Yes. It may be more or less complicated depending on the keyboard and fonts |
| 7532 |
available to you, but it is always possible to encode any possible Unicode |
| 7533 |
codepoint (and some illegal ones) into a file. To create a Unicode file |
| 7534 |
using Vim, you should have compiled Vim with the "+multi_byte" compile-time |
| 7535 |
option. You can get more information about Unicode from the following |
| 7536 |
sites: |
| 7537 |
|
| 7538 |
http://www.unicode.org |
| 7539 |
http://www.cl.cam.ac.uk/~mgk25/unicode.html |
| 7540 |
|
| 7541 |
For more information, read |
| 7542 |
|
| 7543 |
:help multibyte |
| 7544 |
:help usr_45.txt |
| 7545 |
|
| 7546 |
|
| 7547 |
37.2. Which Vim settings are particularly important for editing Unicode |
| 7548 |
files? |
| 7549 |
|
| 7550 |
The most important are the various "encoding" options, i.e., 'encoding', |
| 7551 |
'fileencoding', 'fileencodings' and 'termencoding'. The boolean option |
| 7552 |
'bomb' is also significant. |
| 7553 |
|
| 7554 |
For more information, read |
| 7555 |
|
| 7556 |
:help 'encoding' |
| 7557 |
:help 'fileencoding' |
| 7558 |
:help 'fileencodings' |
| 7559 |
:help 'termencoding' |
| 7560 |
:help 'bomb' |
| 7561 |
|
| 7562 |
|
| 7563 |
37.3. What is the 'encoding' option? |
| 7564 |
|
| 7565 |
Basically, the 'encoding' option defines how Vim will represent your data |
| 7566 |
internally. However, all Unicode encodings are represented internally as |
| 7567 |
utf-8 and converted (if necessary) when reading and writing. |
| 7568 |
|
| 7569 |
For more information, read |
| 7570 |
|
| 7571 |
:help 'encoding' |
| 7572 |
|
| 7573 |
|
| 7574 |
37.4. How does Vim name the various Unicode encodings? |
| 7575 |
|
| 7576 |
Utf-8 is called utf-8 or utf8; utf-16 is called ucs-2 or ucs2; utf-32 is |
| 7577 |
called ucs-4 or ucs4. Also, you may specify endianness (except for utf-8 |
| 7578 |
which does not vary for endianness) by appending le for little-endian or be |
| 7579 |
for big-endian. If you create a file with an encoding of ucs-2 or ucs-4 |
| 7580 |
without specifying endianness, Vim will use what is typical of your |
| 7581 |
machine. |
| 7582 |
|
| 7583 |
For more information, read |
| 7584 |
|
| 7585 |
:help encoding-names |
| 7586 |
:help encoding-values |
| 7587 |
:help encoding-table |
| 7588 |
|
| 7589 |
|
| 7590 |
37.5. How does Vim specify the presence or absence of a byte-order mark? |
| 7591 |
|
| 7592 |
When reading a file, if the 'fileencodings' option includes "ucs-bom", Vim |
| 7593 |
will check for a byte-order mark. When writing a file, if the 'bomb' option |
| 7594 |
is set, Vim will write a byte-order mark on files whose encoding warrants |
| 7595 |
it. |
| 7596 |
|
| 7597 |
For more information, read |
| 7598 |
|
| 7599 |
:help 'fileencodings' |
| 7600 |
:help 'bomb' |
| 7601 |
|
| 7602 |
|
| 7603 |
37.6. What is the 'fileencoding' option? |
| 7604 |
|
| 7605 |
The 'fileencoding' option defines the particular encoding which Vim will |
| 7606 |
use to write a file. If empty, then the value of the 'encoding' option is |
| 7607 |
the default. |
| 7608 |
|
| 7609 |
For more information, read |
| 7610 |
|
| 7611 |
:help 'fileencoding' |
| 7612 |
|
| 7613 |
|
| 7614 |
37.7. What is the 'fileencodings' option? |
| 7615 |
|
| 7616 |
The 'fileencodings' option defines the heuristics used by Vim when opening |
| 7617 |
an existing file. It is a comma separated list of encodings. A special |
| 7618 |
name, "ucs-bom" is used to indicate that Vim should check for the presence |
| 7619 |
of a byte-order mark; however, it will not be recognised if it comes after |
| 7620 |
"utf-8". Normally, "ucs-bom" (if present) should be first in the list. |
| 7621 |
|
| 7622 |
When Vim opens a file, it checks it against the encodings listed in |
| 7623 |
'fileencodings'. The first one that matches is used. If there is no match, |
| 7624 |
then Vim sets 'fileencoding' to the null string, i.e., the value of |
| 7625 |
'encoding' will be used. |
| 7626 |
|
| 7627 |
For more information, read |
| 7628 |
|
| 7629 |
:help 'fileencodings' |
| 7630 |
:help 'encoding' |
| 7631 |
|
| 7632 |
|
| 7633 |
37.8. What is the 'termencoding' option? |
| 7634 |
|
| 7635 |
The 'termencoding' option defines how your keyboard encodes the data you |
| 7636 |
type. If empty, Vim assumes that it has the same value as 'encoding'. |
| 7637 |
Usually it should be set to something that matches your locale. |
| 7638 |
|
| 7639 |
For more information, read |
| 7640 |
|
| 7641 |
:help 'termencoding' |
| 7642 |
:help locale |
| 7643 |
|
| 7644 |
|
| 7645 |
37.9. What is the 'bomb' option? |
| 7646 |
|
| 7647 |
When reading a file with "ucs-bom" present in the 'fileencodings' option, |
| 7648 |
Vim will set the 'bomb' option on or off depending on the presence or |
| 7649 |
absence of a byte-order mark at the start of the file. When writing, Vim |
| 7650 |
will write a byte-order mark if the 'bomb' option is set. You may set or |
| 7651 |
unset it manually do make Vim write, or not write, the b.o.m. |
| 7652 |
|
| 7653 |
For more information, read |
| 7654 |
|
| 7655 |
:help 'bomb' |
| 7656 |
|
| 7657 |
|
| 7658 |
37.10. Where can I find an example of a typical use of all these options? |
| 7659 |
|
| 7660 |
There is a "tip", with explains them in different words with an example, at |
| 7661 |
http://vim.sourceforge.net/tip_view.php?tip_id=246 . |
| 7662 |
|
| 7663 |
|
| 7664 |
37.11. How can I insert Unicode characters into a file using Vim? |
| 7665 |
|
| 7666 |
Several methods are available: |
| 7667 |
|
| 7668 |
- Characters present on your keyboard can be typed in the usual way, even |
| 7669 |
those which require a "dead-key" prefix, like (for instance) the |
| 7670 |
circumflex on French keyboards. |
| 7671 |
- Characters for which a digraph is defined can be typed as two characters |
| 7672 |
prefixed by <Ctrl-K>. |
| 7673 |
- If you have set the 'digraph' option, you can enter the characters for |
| 7674 |
which a digrph is defined as <char1><BS><char2>. |
| 7675 |
- Any character can be entered by using a <Ctrl-V> prefix (or <Ctrl-Q> if |
| 7676 |
<Ctrl-V> is remapped to paste from the clipboard). |
| 7677 |
|
| 7678 |
For more information, read |
| 7679 |
|
| 7680 |
:help digraphs |
| 7681 |
:help 'digraph' |
| 7682 |
:help i_CTRL-V_digit |
| 7683 |
|
| 7684 |
|
| 7685 |
37.12. How can I know which digraphs are defined and for which characters? |
| 7686 |
|
| 7687 |
First set the 'encoding' option properly (for instance, to utf-8), then use |
| 7688 |
the :digraphs command to list the currently defined digraphs. |
| 7689 |
|
| 7690 |
For more information, read |
| 7691 |
|
| 7692 |
:help :digraphs |
| 7693 |
:help 'encoding' |
| 7694 |
|
| 7695 |
|
| 7696 |
============================================================================= |
| 7697 |
|
| 7698 |
Current Maintainer: Yegappan Lakshmanan |
| 7699 |
Last updated on: 2 January 2005 |
| 7700 |
|