Showing posts with label characters. Show all posts
Showing posts with label characters. Show all posts

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 5, 2008

Swap Characters

To swap two adjacent characters in Vim you can issue an "xp" command in normal mode.

Friday, April 18, 2008

Stripping Non-Printable Characters

Here's a quick way to strip non-printable characters in PHP. This is pretty handy for cleaning data before putting it in a DB.

$val = preg_replace('/[^\r\n\t\x20-\x7E\xA0-\xFF]/', ' ', $val)


Being a Perl compatible regexp, you can use it in your language of choice so long as it supports PCRE.

UPDATE:

Steve Laniel reminded me that you can use a Posix regexp to do roughly the same thing:

$val = preg_replace( '/[^[:print:]]/', '', $val )

This is a lot simpler for most cases; although, the patterns are slightly different (Posix is \x20-\x7E).

Friday, November 16, 2007

Diagraphs

Sometimes during a text-editing session, you may need to substitute binary characters like the infamous ^M out of your file. Or alternatively, you may just want to insert an internationalized character like ΓΌ. Vim makes this easy to do. During an editing session, typing :digraph will give you a list of available special characters. Pick the desired character from the list, go into insert mode, and press ctrl-k followed by the character code listed in the digraph table. To insert the umlauted u mentioned above I did ctrl-k u:. Note that this works when typing in ex commands too such as substitutions. A final bonus is you type a macro in insert mode, insert control characters as needed, such as ctrl-w k to jump up a window, yank the macro into a register, and execute as needed.