Sunday, April 27, 2014

Block editing text in Notepad++ or Geany

The other day I showed the concept of block editing text to 3 different people in 3 different contexts.  All were unaware of it, and agreed it was a very useful feature.

Block editing allows you to drag your cursor down across several rows of text and edit them all at once.  Two main uses are to either type on several rows at once, or to select a column of data to be copied to somewhere else.

In Notepad++ it is Alt + Shift, in Geany is it Ctrl + Shift.

Consider this data:
-7.1382    6918.30976
-7.4637    7244.35
-7.7947    7585.7757
-8.1307    7943
-8.4715    8317.637
-8.8169    8709.635
-9.1664    9120.1
-9.5199    9549.9258
-9.8770   10000

If you wanted to remove those negatives, there are a few ways you could go about it, but with the ability to drag your cursor across several rows it is a simple matter to drag it down those rows and delete the negative.

If you wanted to paste this into a spreadsheet it may be able to figure out how to separate the columns, but by just selecting one column at a time and pasting it you can guarantee each column will end up organized properly.

I think block editing is pretty self explanatory, and the stolen gif does a good job of showing its potential.  However, I'd like to also touch on using find and replace.

Ctrl+H will bring up the find and replace box.  There is an option for regular, extended (escape sequences), or regex.  I prefer the extended option.  Let's say we wanted to enclose all the above data in quotes, separated by commas, and semicolons for each pair.  In other words we want this output:
"-7.1382", "6918.30976";
"-7.4637", "7244.35";
"-7.7947", "7585.7757";
"-8.1307", "7943";
"-8.4715", "8317.637";
"-8.8169", "8709.635";
"-9.1664", "9120.1";
"-9.5199", "9549.9258";
"-9.8770", "10000"

This is a pretty common format for input to various programs.  First, I would use block editing to change the spaces to ", " which admittedly wouldn't work for the last one.  If there were more of varied length I'd have to use a regex to check for multiple spaces (\s+).  The real usefulness comes from find and replace with newlines.

The newline character (\n) represents the each line break.  Search for that, and in the replace box enter ";\n"  If you want the output to just be on the same line leave out the \n from the replace text, maybe use a space instead.  On Windows you will likely need to use \r\n instead of just \n (\r = return, \n = newline).

These two techniques have saved me countless hours manually formating text.

Jack Churchill
Lieutenant Colonel John Malcolm Thorpe Fleming "Jack" Churchill, DSO & Bar, MC & Bar (16 September 1906 – 8 March 1996), nicknamed Fighting Jack Churchill and Mad Jack, was a British soldier who fought throughout the Second World War armed with a longbow, and a Scottish sword (a basket-hilted claybeg commonly but incorrectly called a claymore). He is known for the motto "any officer who goes into action without his sword is improperly dressed." Churchill also carried out the last recorded bow and arrow killing in action, shooting a German officer in 1940 in a French village.