Friday, October 30, 2009

More Posts Coming

I know Daily Vim hasn't exactly been "daily". I've got a bunch of posts prepared for the next few weeks. If the response continues to be good, I'm going to try and increase the post frequency on this blog. I appreciate the feedback and contributions you guys and gals have offered so far. If anyone wants to contact me directly with suggestions for posts, please send an email to tinymountain at gmail dot com.

Repeat Last Substitution

If you want to repeat your last substitution on the current line hit the "&" character from normal mode.

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.