Reformatting text with Vim

| | Comments (0) | TrackBacks (0)

I like to keep my code cleanly formatted, that means lines less than 80 characters so that they fit nicely inside of a terminal window. When I edit text or HTML content I also want to keep the same clean formatting. Sometimes when I paste in content from somewhere else I would have to reformat it which took quite so time so I went looking for a way to do it with vim.

Not surprisingly the functionality was already present with the gq command. It takes care of reformatting text by wrapping it to the width of the screen. It also plays nicely with autoindent.

The syntax to use it is gq{motion} in command mode. Some examples of it’s use is gqq to reformat a single line, gq} to reformat a paragraph, and gq4j to reformat the next 4 lines. If you’re using visual mode, like I often do, just typing gq after making a selection will reformat it.

This will be a great timesaver.

Updated Flash Perl Modules

| | Comments (0) | TrackBacks (0)

I have just uploaded new versions of CGI::Session::Flash and CGI::Application::Plugin::Flash to CPAN.

These releases are mostly just documentation and internal cleanups. However there was a bug in CGI::Application::Plugin::Flash::flash_config returning undef when no config had been specified.

Pasting in VIM with autoindent

| | Comments (0) | TrackBacks (0)

When using Vim from a terminal it can not distinguish directly typed text compared to pasted text. This can be a problem if you are using any of the autoindent options and pasting already formatted code, as it will get continually indented. The graphical version of Vim, gVim, does not have this problem since it can detect pastes.

Example:

You want to paste the following code:

sub check_weather
{
    my $weather = shift;

    if ($weather eq 'sunny')
    {
        print "You should get out of the house.\n";
    }
}

However, since vim doesn’t recognize you are pasting it continues to indent each line more and more. What you end up with is this mess:

sub check_weather
{
        my $weather = shift;

            if ($weather eq 'sunny')
                {
                            print "You should get out of the house.\n";
                                }
                            }

You could try to disable autoindent before pasting, but there are so many different options that it would be difficult.

Luckily Vim has a solution! In command mode you can type :set paste to signal that you are pasting and enter paste mode. When paste mode is on the sutoindent features (and others) will be disabled. Once you are done pasting you can type :set nopaste to disable paste mode. When in paste mode you will notice -- INSERT (paste) -- in the status line, but only if showmode is on (default for vim).

To make this even easier you can set up a pastetoggle key which will allow you to toggle between regular insert and paste modes with a single key.

:set pastetoggle=<F3>

For more details see Vim tip #330 and the Vim help for paste and pastetoggle.

Vim Tip: Scrolling Relative to Cursor

| | Comments (0) | TrackBacks (0)

Sometimes the current line you are on is what you want to continue editing, but you want it to be repositioned on the screen. For instance I am on the 3rd to last line in a file, and I want to scroll and reposition the line so that it is the top line of the window.

The following 3 vi commands redraw the screen by moving the current line. One thing to note about them is that they move the cursor to the first non-blank character on the line.

+----------+-------------------------------------------------------------+
| z<ENTER> | Redraw the screen and move the current line to the top.     |
| z.       | Redraw the screen and move the current line to the center.  |
| z-       | Redraw the screen and move the current line to the bottom.  |
+----------+-------------------------------------------------------------+

Vim adds the following 3 commands which are similar to above, but keep the cursor in the same column it was when the command was entered. I prefer them because of that small difference, plus that fact that they are easier to remember.

+----------+-------------------------------------------------------------+
| zt       | Same as z<ENTER> but the cursor stays in the same column.   |
| zz       | Same as z. but cursor stays in same column.                 |
| zb       | Same as z- but cursor stays in same column.                 |
+----------+-------------------------------------------------------------+

All of these commands also support numeric prefixes, in which case they specify what line to use instead of the current line. Ie: 100z- will move line 100 to the bottom of the screen regardless of what line you are currently on.

For more information type :help scroll-cursor in Vim.

Vim Tip: Redrawing the Screen

| | Comments (0) | TrackBacks (0)

Sometimes you need to force a redraw of the screen. For instance if you are using a terminal and some other command outputs text, you will want to redraw the screen so that you can see the correct contents of the file you’re working on.

The ^L (control+L) command forces a redraw in INSERT or COMMAND mode. This is a commonly used command for other terminal based programs, such as bash, so it is useful to remember. You can also use the :redraw ex command, but I find it not as useful since it is longer to type and must be from within COMMAND mode. See the help for it for more information about it and the minor differences.

Vim Tip: Repositioning the Cursor

| | Comments (0) | TrackBacks (0)

Sometimes while editing it is nice to be able to reposition the cursor quickly to the top, middle, or last line. Vi provides 3 quick commands that may be used in command mode to do this, which I find useful.

+---+--------------------------------------------------------------------+
| H | Move to the first line. (Remember it as the Home line)             |
| M | Move to the middle of the screen.                                  |
| L | Move to the last line.                                             |
+---+--------------------------------------------------------------------+

The H and L commands also support numeric prefixes. 5H will move to 5 lines below the first line on the screen, and 3L will move to 3 lines above the last line on the screen. M does not support numeric prefixes since it would be ambiguous.

You can find more about these commands in the vim documentation by typing :help H or :help various-motions.

Vim Tip: Moving to Specific Lines

| | Comments (0) | TrackBacks (0)

This Vim tip is for something that most programmers need to do on a routine basis, moving between specific line numbers. Maybe you have an error message and need to go check out a specific line of code. Maybe you want to go to the last line and continue where you left off.

Opening the file

If you do not already have the file open, you can quickly open it to a specific line number using the following syntax.

$ vi +<num> <file>
$ vi + <file>

Replace <num> with the line number. If you do not specify a line number, it will go to the last line.

Another way of doing that is to use the -c option, which allows you to execute ex commands. The ex commands are discussed below, but basically they are the commands that start with a colon.

$ vi -c $ <file>
$ vi -c 12 <file>

File is open

Now that you have the file open, you can still easily and quickly navigate between line numbers. Below is a list of commands that can be used from within the command mode.

+---------+----------------------------------------------------------+
| <num>G  | Go to the specific line number.                          |
|         | Line numbers start at 1, if you use 0G it will actually  |
|         | go to the end of the file.                               |
+---------+----------------------------------------------------------+
| G       | Go to the last line in the file.                         |
+---------+----------------------------------------------------------+
| ^G      | Displays the current status of where you are.            |
|         | Note: ^ means CTRL key                                   |
+---------+----------------------------------------------------------+
| :<num>  | Go to specific line number.                              |
|         | Unlike the G command, both :0 and :1 will go to the      |
|         | first line.                                              |
+---------+----------------------------------------------------------+
| :$      | Go to the last line in the file.                         |
+---------+----------------------------------------------------------+
| :#      | Display the current line number.                         |
+---------+----------------------------------------------------------+

If you read through that list closely you will have noticed there is more than one way to do it. Use whatever is more comfortable for you to type, or easiest to remember.

Common Tasks

Now that you know the commands, here are some quick scenarios of when and how to use them.

Where am I?

When you’re editing the file you might want to know where you are. By default vi does not display this information, but it is a quick keystroke to find out. By pressing ^G, the current line number, how many lines in the file, and what percentage that is will be displayed. You could also use the ex command # to find out what line you are on, though that it not quite as useful.

^G
:#

I prefer to know where I am at all times so I enable an option to display that information in the status line. You can enable this by typing :set ruler or abbreviated as :set ru. I put it in my ~/.vimrc so that it is always enabled.

Another way to know where you are is to enable the option which precedes each line with the number. To turn this on type :set number or :set nu.

Remember to disable options you precede the option name with no, ie. :set no[option].

Get to line 100

There’s an error on line 100 and you had better fix it fast!

99j (assuming you are on line 1)
:100
100G

$ vi + 100 <FILE>
$ vi -c 100 <FILE>

Back to the beginning

You want to go to the first line in the file.

:1
1G

$ vi + 1 <FILE>
$ vi -c 1 <FILE>

To the end

Now to get to the last line in the file.

:$
G

$ vi + <FILE>
$ vi -c $ <FILE>

And with that, I am going to wrap this up. You can see there are a number of ways to quickly get to a specific position with just a couple of keystrokes.

Get Ready for Vim Tips

| | Comments (0) | TrackBacks (0)

I know it has been months since I have made a post. I am trying not to make this a stale blog, but the fact is that the summer time gets pretty busy for me with outdoor activities and I have not had a lot of time to play around on the computer or make posts.

That brings me to the point of this post. I have recently started reading Learning the Vi and Vim Editors, 7th Edition by O’reilly.

I just stopped to think about it, and I have been using Vim for 8 or 9 years now. I read the previous edition of this book about 6 years ago. I know enough to do what I do on a day to day basis, and I feel that I am way more productive using Vim than other editors. However, I know that I am weak in certain aspects and could be a lot more efficient. That is why I decided to read this book again and see what I could pick up. This edition also focuses on Vim, which is my preferred Vi clone so that is another benefit.

So with that said, I plan to write some posts about little tips and tricks that I have picked up while reading this book.

Firefox Default CSS

| | Comments (0) | TrackBacks (0)

Sometimes it is useful to know how the browser styles things by default. If you want to know how Firefox does it, check out the res/ directory in the install path. On Windows this is C:\Program Files\Mozilla Firefox\res\.

The html.css, forms.css, and quirk.css files are what you will be most interested in.

Alternatively you could check it out at http://lxr.mozilla.org/mozilla1.8.0/source/layout/style/

Launchy

| | Comments (0) | TrackBacks (0)

I’ve always been someone that prefers to use a shell. To me it’s easier to just type a few characters to start a program rather than click through a nested menu system. The less my hands have to leave the keyboard the better.

A program for Windows that I have been using for a while now is Launchy. It indexes your start menu and allows quick access to them. Just hit ALT+SPACE and a dialog pops up which you can enter in the command name. Now that isn’t too exciting, but it is smart. It performs fuzzy matching against the indexes so I can type ‘vi’ instead of ‘gvim’, or ‘ie’ instead of ‘Internet Explorer’.

It has some other built in functionality too such as a simple calculator. Just enter an expression and it will display the result. I really enjoy that since I used to always hit WIN+R, then type ‘calc’

Now that I have been using it for a while I really find it indispensable.

There are some others that I have heard mentioned, but I have not tried them.

Colibri

KeyBreeze

findrun

Skylight