# HG changeset patch # User Steve Losh # Date 1318200347 14400 # Node ID 68f40c419a18d6400669f8f023b2473a3fa90564 # Parent 848c8d85f138c6115efce59c2e039eec09f82b99 Status Lines. diff -r 848c8d85f138 -r 68f40c419a18 chapters/17.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapters/17.markdown Sun Oct 09 18:45:47 2011 -0400 @@ -0,0 +1,144 @@ +Status Lines +============ + +Vim allows you to customize the text in the status line at the bottom of each +window. This is done through the `statusline` option. Run the following +command: + + :set statusline=%f + +You should see the path to the file (relative to the current directory) in the +status line. Now run this command: + + :set statusline=%f\ -\ FileType:\ %y + +Now you'll see something like "foo.markdown - FileType: [markdown]" in the +status line. + +If you're familiar with C's `printf` or Python's string interpolation the format +of this option may look familiar. If not, the only trick is that things that +start with `%` are expanded to different text depending on what comes after +them. In our example `%f` is replaced with the filename and `%y` is replaced +with the type of the file. + +Notice how the spaces in the status line need to be escaped with backslashes. +This is because `set` allows you to set multiple options at once, as we saw in +the second chapter. + +Status lines can get extremely complicated very quickly, so there's a better way +to set them that will let us be more clear. Run the following commands: + + :set statusline=%f " Path to the file + :set statusline+=\ -\ " Separator + :set statusline+=FileType: " Label + :set statusline+=%y " Filetype of the file + +In the first command we used `=` to wipe out any existing value present. In the +rest we used `+=` to build up the option one piece at a time. We also added +comments explaining each piece for other people reading the code (or ourselves +several months later). + +Run the following command: + + :set statusline=%l " Current line + :set statusline+=/ " Separator + :set statusline+=%L " Total lines + +Now the status line contains only the current line number and number of lines in +the file, and looks something like "12/223". + +Width and Padding +----------------- + +Additional characters can be used in some of the various `%` codes to change how +the information is displayed. Run the following command: + + :set statusline=%4l + +The line number in the status line will now be proceeded by enough spaces to +make it at least four characters wide (for example: " 12"). This can be useful +to prevent the text in the status line from shifting around distractingly. + +By default the padding spaces are added on the left side of the value. Run this +command: + + :set statusline=Current:\ %4l\ Total:\ %4L + +Your status line will now look like this: + + Current: 12 Total: 223 + +You can use `-` to place padding on the right instead of the left. Run this +command: + + :set statusline=Current:\ %-4l\ Total:\ %-4L + +Your status line will now look like this: + + Current: 12 Total: 223 + +This looks much nicer because the numbers are next to their labels. + +For codes that result in a number you can tell Vim to pad with zeros instead of +spaces. Run the following command: + + :set statusline=%04l + +Now your status line will read "0012" when on line twelve. + +Finally, you can also set the maximum width of a code's output. Run this +command: + + :set statusline=%F + +`%F` displays the *full* path to the current file. Now run this command to +change the maximum width: + + :set statusline=%.20F + +The path will be truncated if necessary, looking something like this: + +