Friday, October 30, 2009

Quick Exits

Hitting ZZ in normal mode will exit and save the current file. Hitting ZQ will exit without saving.

Handy Shell Pattern

When piping commands, you can often benefit from breaking your data into multiple lines. A combination of sed and xargs can make this very easy to do. Take the following commands for example:

cd /tmp && mkdir blah && cd blah;
touch file-1 file-2 file-3;
ls | sed 'p; s/-//' | xargs -n2

Breaking this down, we create a directory and touch three empty files. From there, we use sed to print two lines. The first line is the original file name, the second line is the filename without the dash. From here, we can use the -n argument in conjunction with xargs to merge the two lines back into a single line. The resulting output would be:

file-1 file1
file-2 file2
file-3 file3

Once you have the lines joined, you could do something like:

... | while read a b; do mv $a $b; done

Bonus tip: the "read" bash builtin takes a line and associates each word with a given variable. Type "help read" from the shell for the complete documentation.

In summary the full command-chain for this tip would be:

cd /tmp && mkdir blah && cd blah;
touch file-1 file-2 file-3;
ls | sed 'p; s/-//' | xargs -n2 | while read a b; do mv $a $b; done

Thanks to Chris Sutter for contributing this handy shell pattern, and thanks to Andeers for the coffee!

Friday, October 23, 2009

Fun With Unicode ٩(●̮̮̃•̃)۶

Following up on the last post, you can also use CTRL-V to enter unicode characters. All you have to do is the following:

CTRL-V u (unicode character hex code)

Note that you can also combine multi-byte characters as demonstrated in the title of this post.

Wednesday, October 21, 2009

CTRL-V for Literal Characters

When typing in insert or ex mode, hiting CTRL-V allows you to enter a literal character. Some examples:

CTRL-V CTRL-M # enters a literal carriage-return \r
CTRL-V 10 <enter> # enters a null character

After entering the CTRL-V, you can either enter the character explicitly or type in its decimal code. A list of decimal codes is available through the :digraph ex command. If you're curious why this might be useful, I've used it on a number of occasions in conjunction with a substitution to remove binary characters from a file. An example might be:

:%s/CTRL-V CTRL-M//g # remove erroneous CTRL-M from the current file

You can find out the code for any character by putting it under the cursor and hitting "ga" in normal mode. For entering digraphs, you can also hit CTRL-K from insert mode and type the two letter character code preceeding the desired character.

Friday, September 18, 2009

Resizing with GNU Screen

I know a lot of you use screen frequently. My friend Chris gave me a tip recently related to resizing split windows in a screen session.

In screen, you can do <C-a>:resize [+-]N<CR> to resize a window +/- N lines

This is handy if you've got a cmd running and want to see when it's done. You can split the screen, load it in one window, and resize it to be small; thereby, preventing it from occupying valuable real estate on your monitor.

Screen has a handful of : commands such as :number N to change the numeric position of the current window. Take a look at the DEFAULT KEY BINDINGS section of man(1) screen for the the full list (parenthesized next to each key binding).

For more screen tips, check out the screen extravaganza posted back in 2008.

Mouse Tips

This has been mentioned on the blog in the past, but readers email me about it often enough that it deserves a formal tip. You can use the mouse to resize windows in Vim if you set your mouse as follows.

:set mouse=a

This will even work over a remote ssh session if you also set your term appropriately.

:set term=[the appropriate setting]

Options for term settings are as follows:

in the GUI: "builtin_gui"
on Amiga: "amiga"
on BeOS: "beos-ansi"
on Mac: "mac-ansi"
on MiNT: "vt52"
on MS-DOS: "pcterm"
on OS/2: "os2ansi"
on Unix: "ansi"
on VMS: "ansi"
on Win 32: "win32"

If the mouse setting interferes with your operating system's copy and paste ability such as the select to copy feature under X windows, you can revert to the standard behavior by holding shift while selecting text.

Setting your mouse as described also prevents accidental terminal scrolling with the mouse wheel when editing a file remotely and causes Vim to scroll within the current buffer instead.

Thanks to mmmattos for emailing me and providing the motivation for this tip.

Monday, September 14, 2009

Diffing From Inside Vim

To compare two files without resorting to launching vidiff from the command-line, you can do the following.

1) open one of the two files in a standard buffer
2) type in :vert diffsplit filename

Put the name of the file you'll be comparing in place of filename. Prefixing the command with vert gives you a vertical split. If you'd prefer a horizontal split, simply omit the vert.