Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 1 | *usr_25.txt* For Vim version 9.1. Last change: 2025 Jun 10 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Editing formatted text |
| 6 | |
| 7 | |
| 8 | Text hardly ever comes in one sentence per line. This chapter is about |
| 9 | breaking sentences to make them fit on a page and other formatting. |
| 10 | Vim also has useful features for editing single-line paragraphs and tables. |
| 11 | |
| 12 | |25.1| Breaking lines |
| 13 | |25.2| Aligning text |
| 14 | |25.3| Indents and tabs |
| 15 | |25.4| Dealing with long lines |
| 16 | |25.5| Editing tables |
| 17 | |
| 18 | Next chapter: |usr_26.txt| Repeating |
| 19 | Previous chapter: |usr_24.txt| Inserting quickly |
| 20 | Table of contents: |usr_toc.txt| |
| 21 | |
| 22 | ============================================================================== |
| 23 | *25.1* Breaking lines |
| 24 | |
| 25 | Vim has a number of functions that make dealing with text easier. By default, |
| 26 | the editor does not perform automatic line breaks. In other words, you have |
| 27 | to press <Enter> yourself. This is useful when you are writing programs where |
| 28 | you want to decide where the line ends. It is not so good when you are |
| 29 | creating documentation and want the text to be at most 70 character wide. |
| 30 | If you set the 'textwidth' option, Vim automatically inserts line breaks. |
| 31 | Suppose, for example, that you want a very narrow column of only 30 |
| 32 | characters. You need to execute the following command: > |
| 33 | |
| 34 | :set textwidth=30 |
| 35 | |
| 36 | Now you start typing (ruler added): |
| 37 | |
| 38 | 1 2 3 |
| 39 | 12345678901234567890123456789012345 |
| 40 | I taught programming for a whi ~ |
| 41 | |
| 42 | If you type "l" next, this makes the line longer than the 30-character limit. |
| 43 | When Vim sees this, it inserts a line break and you get the following: |
| 44 | |
| 45 | 1 2 3 |
| 46 | 12345678901234567890123456789012345 |
| 47 | I taught programming for a ~ |
| 48 | whil ~ |
| 49 | |
| 50 | Continuing on, you can type in the rest of the paragraph: |
| 51 | |
| 52 | 1 2 3 |
| 53 | 12345678901234567890123456789012345 |
| 54 | I taught programming for a ~ |
| 55 | while. One time, I was stopped ~ |
| 56 | by the Fort Worth police, ~ |
| 57 | because my homework was too ~ |
| 58 | hard. True story. ~ |
| 59 | |
| 60 | You do not have to type newlines; Vim puts them in automatically. |
| 61 | |
| 62 | Note: |
| 63 | The 'wrap' option makes Vim display lines with a line break, but this |
| 64 | doesn't insert a line break in the file. |
| 65 | |
| 66 | |
| 67 | REFORMATTING |
| 68 | |
| 69 | The Vim editor is not a word processor. In a word processor, if you delete |
| 70 | something at the beginning of the paragraph, the line breaks are reworked. In |
| 71 | Vim they are not; so if you delete the word "programming" from the first line, |
| 72 | all you get is a short line: |
| 73 | |
| 74 | 1 2 3 |
| 75 | 12345678901234567890123456789012345 |
| 76 | I taught for a ~ |
| 77 | while. One time, I was stopped ~ |
| 78 | by the Fort Worth police, ~ |
| 79 | because my homework was too ~ |
| 80 | hard. True story. ~ |
| 81 | |
| 82 | This does not look good. To get the paragraph into shape you use the "gq" |
| 83 | operator. |
| 84 | Let's first use this with a Visual selection. Starting from the first |
| 85 | line, type: > |
| 86 | |
| 87 | v4jgq |
| 88 | |
Bram Moolenaar | b133208 | 2013-10-06 14:22:40 +0200 | [diff] [blame] | 89 | "v" to start Visual mode, "4j" to move to the end of the paragraph and then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | the "gq" operator. The result is: |
| 91 | |
| 92 | 1 2 3 |
| 93 | 12345678901234567890123456789012345 |
| 94 | I taught for a while. One ~ |
| 95 | time, I was stopped by the ~ |
| 96 | Fort Worth police, because my ~ |
| 97 | homework was too hard. True ~ |
| 98 | story. ~ |
| 99 | |
| 100 | Note: there is a way to do automatic formatting for specific types of text |
| 101 | layouts, see |auto-format|. |
| 102 | |
| 103 | Since "gq" is an operator, you can use one of the three ways to select the |
| 104 | text it works on: With Visual mode, with a movement and with a text object. |
| 105 | The example above could also be done with "gq4j". That's less typing, but |
| 106 | you have to know the line count. A more useful motion command is "}". This |
| 107 | moves to the end of a paragraph. Thus "gq}" formats from the cursor to the |
| 108 | end of the current paragraph. |
| 109 | A very useful text object to use with "gq" is the paragraph. Try this: > |
| 110 | |
| 111 | gqap |
| 112 | |
| 113 | "ap" stands for "a-paragraph". This formats the text of one paragraph |
| 114 | (separated by empty lines). Also the part before the cursor. |
| 115 | If you have your paragraphs separated by empty lines, you can format the |
| 116 | whole file by typing this: > |
| 117 | |
| 118 | gggqG |
| 119 | |
| 120 | "gg" to move to the first line, "gqG" to format until the last line. |
| 121 | Warning: If your paragraphs are not properly separated, they will be joined |
Bram Moolenaar | 381ffae | 2007-05-12 14:06:39 +0000 | [diff] [blame] | 122 | together. A common mistake is to have a line with a space or tab. That's a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | blank line, but not an empty line. |
| 124 | |
Bram Moolenaar | 9964e46 | 2007-05-05 17:54:07 +0000 | [diff] [blame] | 125 | Vim is able to format more than just plain text. See |fo-table| for how to |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 126 | change this. See the 'joinspaces' option to change the number of spaces used |
| 127 | after a full stop. |
| 128 | It is possible to use an external program for formatting. This is useful |
| 129 | if your text can't be properly formatted with Vim's builtin command. See the |
| 130 | 'formatprg' option. |
| 131 | |
| 132 | ============================================================================== |
| 133 | *25.2* Aligning text |
| 134 | |
| 135 | To center a range of lines, use the following command: > |
| 136 | |
| 137 | :{range}center [width] |
| 138 | |
| 139 | {range} is the usual command-line range. [width] is an optional line width to |
| 140 | use for centering. If [width] is not specified, it defaults to the value of |
| 141 | 'textwidth'. (If 'textwidth' is 0, the default is 80.) |
| 142 | For example: > |
| 143 | |
| 144 | :1,5center 40 |
| 145 | |
| 146 | results in the following: |
| 147 | |
| 148 | I taught for a while. One ~ |
| 149 | time, I was stopped by the ~ |
| 150 | Fort Worth police, because my ~ |
| 151 | homework was too hard. True ~ |
| 152 | story. ~ |
| 153 | |
| 154 | |
| 155 | RIGHT ALIGNMENT |
| 156 | |
| 157 | Similarly, the ":right" command right-justifies the text: > |
| 158 | |
| 159 | :1,5right 37 |
| 160 | |
| 161 | gives this result: |
| 162 | |
| 163 | I taught for a while. One ~ |
| 164 | time, I was stopped by the ~ |
| 165 | Fort Worth police, because my ~ |
| 166 | homework was too hard. True ~ |
| 167 | story. ~ |
| 168 | |
| 169 | LEFT ALIGNMENT |
| 170 | |
| 171 | Finally there is this command: > |
| 172 | |
| 173 | :{range}left [margin] |
| 174 | |
| 175 | Unlike ":center" and ":right", however, the argument to ":left" is not the |
| 176 | length of the line. Instead it is the left margin. If it is omitted, the |
| 177 | text will be put against the left side of the screen (using a zero margin |
| 178 | would do the same). If it is 5, the text will be indented 5 spaces. For |
| 179 | example, use these commands: > |
| 180 | |
| 181 | :1left 5 |
| 182 | :2,5left |
| 183 | |
| 184 | This results in the following: |
| 185 | |
| 186 | I taught for a while. One ~ |
| 187 | time, I was stopped by the ~ |
| 188 | Fort Worth police, because my ~ |
| 189 | homework was too hard. True ~ |
| 190 | story. ~ |
| 191 | |
| 192 | |
Hirohito Higashi | 195fcc9 | 2025-02-01 10:26:58 +0100 | [diff] [blame] | 193 | JUSTIFYING TEXT *justify* *:Justify* *Justify()* *package-justify* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | |
| 195 | Vim has no built-in way of justifying text. However, there is a neat macro |
| 196 | package that does the job. To use this package, execute the following |
Christian Brabandt | 9598a63 | 2025-01-11 10:14:24 +0100 | [diff] [blame] | 197 | command: >vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 199 | :packadd justify |
| 200 | |
Christian Brabandt | 9598a63 | 2025-01-11 10:14:24 +0100 | [diff] [blame] | 201 | Or put this line in your |vimrc|: >vim |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 202 | |
| 203 | packadd! justify |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 205 | This Vim script file defines a new visual command "_j". To justify a block of |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 206 | text, highlight the text in Visual mode and then execute "_j". |
| 207 | Look in the file for more explanations. To go there, do "gf" on this name: |
Bram Moolenaar | 7db8f6f | 2016-03-29 23:12:46 +0200 | [diff] [blame] | 208 | $VIMRUNTIME/pack/dist/opt/justify/plugin/justify.vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | |
| 210 | An alternative is to filter the text through an external program. Example: > |
| 211 | |
| 212 | :%!fmt |
| 213 | |
| 214 | ============================================================================== |
| 215 | *25.3* Indents and tabs |
| 216 | |
| 217 | Indents can be used to make text stand out from the rest. The example texts |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 218 | in this manual, for example, are indented by eight columns. You would |
| 219 | normally enter this by typing <Tab> at the start of each line. Take this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | text: |
| 221 | the first line ~ |
| 222 | the second line ~ |
| 223 | |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 224 | This is entered by typing <Tab>, some text, <Enter>, <Tab> and more text. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | The 'autoindent' option inserts indents automatically: > |
| 226 | |
| 227 | :set autoindent |
| 228 | |
| 229 | When a new line is started it gets the same indent as the previous line. In |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 230 | the above example, pressing the <Tab> key after <Enter> is not needed anymore. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | INCREASING INDENT |
| 234 | |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 235 | To increase the amount of indent in a line, use the ">" operator, in Normal |
| 236 | mode. Often this is used as ">>", which adds indent to the current line. |
| 237 | In Insert mode, use <C-t>. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | The amount of indent added is specified with the 'shiftwidth' option. The |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 239 | default value is 8. To make ">>" insert four columns worth of indent, for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 240 | example, type this: > |
| 241 | |
| 242 | :set shiftwidth=4 |
| 243 | |
| 244 | When used on the second line of the example text, this is what you get: |
| 245 | |
| 246 | the first line ~ |
| 247 | the second line ~ |
| 248 | |
| 249 | "4>>" will increase the indent of four lines. |
| 250 | |
| 251 | |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 252 | SOFT TAB STOPS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 253 | |
| 254 | If you want to make indents a multiple of 4, you set 'shiftwidth' to 4. But |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 255 | when pressing a <Tab> you still get 8 columns worth of indent. To change |
| 256 | this, set the 'softtabstop' option: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 257 | |
| 258 | :set softtabstop=4 |
| 259 | |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 260 | Vim now creates invisible tab stops for your cursor every 4 columns; hitting |
| 261 | <Tab> jumps to the next stop and inserts the exact mix of spaces or tabs |
| 262 | needed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 263 | |
| 264 | Note: |
| 265 | You could set the 'tabstop' option to 4. However, if you edit the |
| 266 | file another time, with 'tabstop' set to the default value of 8, it |
| 267 | will look wrong. In other programs and when printing the indent will |
| 268 | also be wrong. Therefore it is recommended to keep 'tabstop' at eight |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 269 | all the time. That's the standard value everywhere on UNIX-like |
| 270 | systems. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 271 | |
| 272 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 273 | ============================================================================== |
| 274 | *25.4* Dealing with long lines |
| 275 | |
| 276 | Sometimes you will be editing a file that is wider than the number of columns |
| 277 | in the window. When that occurs, Vim wraps the lines so that everything fits |
| 278 | on the screen. |
| 279 | If you switch the 'wrap' option off, each line in the file shows up as one |
| 280 | line on the screen. Then the ends of the long lines disappear off the screen |
| 281 | to the right. |
| 282 | When you move the cursor to a character that can't be seen, Vim will scroll |
| 283 | the text to show it. This is like moving a viewport over the text in the |
| 284 | horizontal direction. |
| 285 | By default, Vim does not display a horizontal scrollbar in the GUI. If you |
| 286 | want to enable one, use the following command: > |
| 287 | |
| 288 | :set guioptions+=b |
| 289 | |
| 290 | One horizontal scrollbar will appear at the bottom of the Vim window. |
| 291 | |
| 292 | If you don't have a scrollbar or don't want to use it, use these commands to |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 293 | scroll the text. The cursor will stay in the same place, but it's moved back |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 294 | into the visible text if necessary. |
| 295 | |
| 296 | zh scroll right |
| 297 | 4zh scroll four characters right |
| 298 | zH scroll half a window width right |
| 299 | ze scroll right to put the cursor at the end |
| 300 | zl scroll left |
| 301 | 4zl scroll four characters left |
| 302 | zL scroll half a window width left |
| 303 | zs scroll left to put the cursor at the start |
| 304 | |
| 305 | Let's attempt to show this with one line of text. The cursor is on the "w" of |
| 306 | "which". The "current window" above the line indicates the text that is |
| 307 | currently visible. The "window"s below the text indicate the text that is |
| 308 | visible after the command left of it. |
| 309 | |
| 310 | |<-- current window -->| |
| 311 | some long text, part of which is visible in the window ~ |
| 312 | ze |<-- window -->| |
| 313 | zH |<-- window -->| |
| 314 | 4zh |<-- window -->| |
| 315 | zh |<-- window -->| |
| 316 | zl |<-- window -->| |
| 317 | 4zl |<-- window -->| |
| 318 | zL |<-- window -->| |
| 319 | zs |<-- window -->| |
| 320 | |
| 321 | |
| 322 | MOVING WITH WRAP OFF |
| 323 | |
| 324 | When 'wrap' is off and the text has scrolled horizontally, you can use the |
| 325 | following commands to move the cursor to a character you can see. Thus text |
| 326 | left and right of the window is ignored. These never cause the text to |
| 327 | scroll: |
| 328 | |
| 329 | g0 to first visible character in this line |
| 330 | g^ to first non-blank visible character in this line |
Bram Moolenaar | 8b530c1 | 2019-10-28 02:13:05 +0100 | [diff] [blame] | 331 | gm to middle of screen line |
| 332 | gM to middle of the text in this line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 333 | g$ to last visible character in this line |
| 334 | |
Bram Moolenaar | 8b530c1 | 2019-10-28 02:13:05 +0100 | [diff] [blame] | 335 | |<-- window -->| |
| 336 | some long text, part of which is visible in one line ~ |
| 337 | g0 g^ gm gM g$ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | |
| 339 | |
| 340 | BREAKING AT WORDS *edit-no-break* |
| 341 | |
| 342 | When preparing text for use by another program, you might have to make |
| 343 | paragraphs without a line break. A disadvantage of using 'nowrap' is that you |
| 344 | can't see the whole sentence you are working on. When 'wrap' is on, words are |
| 345 | broken halfway, which makes them hard to read. |
| 346 | A good solution for editing this kind of paragraph is setting the |
| 347 | 'linebreak' option. Vim then breaks lines at an appropriate place when |
| 348 | displaying the line. The text in the file remains unchanged. |
| 349 | Without 'linebreak' text might look like this: |
| 350 | |
| 351 | +---------------------------------+ |
| 352 | |letter generation program for a b| |
| 353 | |ank. They wanted to send out a s| |
| 354 | |pecial, personalized letter to th| |
| 355 | |eir richest 1000 customers. Unfo| |
| 356 | |rtunately for the programmer, he | |
| 357 | +---------------------------------+ |
| 358 | After: > |
| 359 | |
| 360 | :set linebreak |
| 361 | |
| 362 | it looks like this: |
| 363 | |
| 364 | +---------------------------------+ |
| 365 | |letter generation program for a | |
| 366 | |bank. They wanted to send out a | |
| 367 | |special, personalized letter to | |
| 368 | |their richest 1000 customers. | |
| 369 | |Unfortunately for the programmer,| |
| 370 | +---------------------------------+ |
| 371 | |
| 372 | Related options: |
| 373 | 'breakat' specifies the characters where a break can be inserted. |
| 374 | 'showbreak' specifies a string to show at the start of broken line. |
| 375 | Set 'textwidth' to zero to avoid a paragraph to be split. |
| 376 | |
| 377 | |
| 378 | MOVING BY VISIBLE LINES |
| 379 | |
| 380 | The "j" and "k" commands move to the next and previous lines. When used on |
| 381 | a long line, this means moving a lot of screen lines at once. |
| 382 | To move only one screen line, use the "gj" and "gk" commands. When a line |
| 383 | doesn't wrap they do the same as "j" and "k". When the line does wrap, they |
| 384 | move to a character displayed one line below or above. |
| 385 | You might like to use these mappings, which bind these movement commands to |
| 386 | the cursor keys: > |
| 387 | |
| 388 | :map <Up> gk |
| 389 | :map <Down> gj |
| 390 | |
| 391 | |
Bram Moolenaar | 32efaf6 | 2014-11-05 17:02:17 +0100 | [diff] [blame] | 392 | TURNING A PARAGRAPH INTO ONE LINE *edit-paragraph-join* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | |
| 394 | If you want to import text into a program like MS-Word, each paragraph should |
| 395 | be a single line. If your paragraphs are currently separated with empty |
| 396 | lines, this is how you turn each paragraph into a single line: > |
| 397 | |
| 398 | :g/./,/^$/join |
| 399 | |
| 400 | That looks complicated. Let's break it up in pieces: |
| 401 | |
| 402 | :g/./ A ":global" command that finds all lines that contain |
| 403 | at least one character. |
| 404 | ,/^$/ A range, starting from the current line (the non-empty |
| 405 | line) until an empty line. |
| 406 | join The ":join" command joins the range of lines together |
| 407 | into one line. |
| 408 | |
| 409 | Starting with this text, containing eight lines broken at column 30: |
| 410 | |
| 411 | +----------------------------------+ |
| 412 | |A letter generation program | |
| 413 | |for a bank. They wanted to | |
| 414 | |send out a special, | |
| 415 | |personalized letter. | |
| 416 | | | |
| 417 | |To their richest 1000 | |
| 418 | |customers. Unfortunately for | |
| 419 | |the programmer, | |
| 420 | +----------------------------------+ |
| 421 | |
| 422 | You end up with two lines: |
| 423 | |
| 424 | +----------------------------------+ |
| 425 | |A letter generation program for a | |
| 426 | |bank. They wanted to send out a s| |
| 427 | |pecial, personalized letter. | |
| 428 | |To their richest 1000 customers. | |
| 429 | |Unfortunately for the programmer, | |
| 430 | +----------------------------------+ |
| 431 | |
| 432 | Note that this doesn't work when the separating line is blank but not empty; |
| 433 | when it contains spaces and/or tabs. This command does work with blank lines: |
| 434 | > |
| 435 | :g/\S/,/^\s*$/join |
| 436 | |
| 437 | This still requires a blank or empty line at the end of the file for the last |
| 438 | paragraph to be joined. |
| 439 | |
| 440 | ============================================================================== |
| 441 | *25.5* Editing tables |
| 442 | |
| 443 | Suppose you are editing a table with four columns: |
| 444 | |
| 445 | nice table test 1 test 2 test 3 ~ |
| 446 | input A 0.534 ~ |
| 447 | input B 0.913 ~ |
| 448 | |
| 449 | You need to enter numbers in the third column. You could move to the second |
| 450 | line, use "A", enter a lot of spaces and type the text. |
| 451 | For this kind of editing there is a special option: > |
| 452 | |
| 453 | set virtualedit=all |
| 454 | |
| 455 | Now you can move the cursor to positions where there isn't any text. This is |
| 456 | called "virtual space". Editing a table is a lot easier this way. |
| 457 | Move the cursor by searching for the header of the last column: > |
| 458 | |
| 459 | /test 3 |
| 460 | |
| 461 | Now press "j" and you are right where you can enter the value for "input A". |
| 462 | Typing "0.693" results in: |
| 463 | |
| 464 | nice table test 1 test 2 test 3 ~ |
| 465 | input A 0.534 0.693 ~ |
| 466 | input B 0.913 ~ |
| 467 | |
| 468 | Vim has automatically filled the gap in front of the new text for you. Now, |
| 469 | to enter the next field in this column use "Bj". "B" moves back to the start |
| 470 | of a white space separated word. Then "j" moves to the place where the next |
| 471 | field can be entered. |
| 472 | |
| 473 | Note: |
| 474 | You can move the cursor anywhere in the display, also beyond the end |
| 475 | of a line. But Vim will not insert spaces there, until you insert a |
| 476 | character in that position. |
| 477 | |
| 478 | |
| 479 | COPYING A COLUMN |
| 480 | |
| 481 | You want to add a column, which should be a copy of the third column and |
| 482 | placed before the "test 1" column. Do this in seven steps: |
| 483 | 1. Move the cursor to the left upper corner of this column, e.g., with |
| 484 | "/test 3". |
| 485 | 2. Press CTRL-V to start blockwise Visual mode. |
| 486 | 3. Move the cursor down two lines with "2j". You are now in "virtual space": |
| 487 | the "input B" line of the "test 3" column. |
| 488 | 4. Move the cursor right, to include the whole column in the selection, plus |
| 489 | the space that you want between the columns. "9l" should do it. |
| 490 | 5. Yank the selected rectangle with "y". |
| 491 | 6. Move the cursor to "test 1", where the new column must be placed. |
| 492 | 7. Press "P". |
| 493 | |
| 494 | The result should be: |
| 495 | |
| 496 | nice table test 3 test 1 test 2 test 3 ~ |
| 497 | input A 0.693 0.534 0.693 ~ |
| 498 | input B 0.913 ~ |
| 499 | |
| 500 | Notice that the whole "test 1" column was shifted right, also the line where |
| 501 | the "test 3" column didn't have text. |
| 502 | |
| 503 | Go back to non-virtual cursor movements with: > |
| 504 | |
| 505 | :set virtualedit= |
| 506 | |
| 507 | |
| 508 | VIRTUAL REPLACE MODE |
| 509 | |
| 510 | The disadvantage of using 'virtualedit' is that it "feels" different. You |
| 511 | can't recognize tabs or spaces beyond the end of line when moving the cursor |
| 512 | around. Another method can be used: Virtual Replace mode. |
| 513 | Suppose you have a line in a table that contains both tabs and other |
| 514 | characters. Use "rx" on the first tab: |
| 515 | |
| 516 | inp 0.693 0.534 0.693 ~ |
| 517 | |
| 518 | | |
| 519 | rx | |
| 520 | V |
| 521 | |
| 522 | inpx0.693 0.534 0.693 ~ |
| 523 | |
| 524 | The layout is messed up. To avoid that, use the "gr" command: |
| 525 | |
| 526 | inp 0.693 0.534 0.693 ~ |
| 527 | |
| 528 | | |
| 529 | grx | |
| 530 | V |
| 531 | |
| 532 | inpx 0.693 0.534 0.693 ~ |
| 533 | |
| 534 | What happens is that the "gr" command makes sure the new character takes the |
| 535 | right amount of screen space. Extra spaces or tabs are inserted to fill the |
| 536 | gap. Thus what actually happens is that a tab is replaced by "x" and then |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 537 | blanks added to make the text after it keep its place. In this case a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 538 | tab is inserted. |
| 539 | When you need to replace more than one character, you use the "R" command |
| 540 | to go to Replace mode (see |04.9|). This messes up the layout and replaces |
| 541 | the wrong characters: |
| 542 | |
| 543 | inp 0 0.534 0.693 ~ |
| 544 | |
| 545 | | |
| 546 | R0.786 | |
| 547 | V |
| 548 | |
| 549 | inp 0.78634 0.693 ~ |
| 550 | |
| 551 | The "gR" command uses Virtual Replace mode. This preserves the layout: |
| 552 | |
| 553 | inp 0 0.534 0.693 ~ |
| 554 | |
| 555 | | |
| 556 | gR0.786 | |
| 557 | V |
| 558 | |
| 559 | inp 0.786 0.534 0.693 ~ |
| 560 | |
Damien Lejay | bfa1636 | 2025-06-10 21:12:31 +0200 | [diff] [blame] | 561 | |
| 562 | REFORMATTING TABS IN TABLES |
| 563 | |
| 564 | You edit a file that contains tabular data and the original author of the file |
| 565 | decided to align the tabular data using tab characters (instead of spaces). |
| 566 | Alas, they were using tab stops separated by 4 columns and Vim's default |
| 567 | is 8 columns; the table looks wrong! What can be done? |
| 568 | To fix the appearance without modifying the file, adjust the setting |
| 569 | temporarily: > |
| 570 | |
| 571 | :set tabstop=4 |
| 572 | |
| 573 | This updates the visual layout, but the file itself remains unchanged. |
| 574 | Another possibility is to permanently reformat the file. For this Vim |
| 575 | provides the |:retab| command. First, set 'tabstop' to match original layout |
| 576 | (as above), then run: > |
| 577 | |
| 578 | :retab 8 |
| 579 | |
| 580 | The ":retab" command will change 'tabstop' to 8, while changing the text such |
| 581 | that it looks the same. It changes spans of white space into tabs and spaces |
| 582 | for this. You can now write the file. |
| 583 | Warning: When using ":retab" on a program, it may change white space inside |
| 584 | a string constant. Therefore it's a good habit to use "\t" instead of a |
| 585 | real tab. |
| 586 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 587 | ============================================================================== |
| 588 | |
| 589 | Next chapter: |usr_26.txt| Repeating |
| 590 | |
Bram Moolenaar | d473c8c | 2018-08-11 18:00:22 +0200 | [diff] [blame] | 591 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |