Showing posts with label Editor. Show all posts
Showing posts with label Editor. Show all posts

Sunday, July 8, 2012

Shell script - Capture snippets from vi or Vim

Here is a utility that may be useful to people who use vi or Vim.
Perl Squirrel has a common tendency of collecting useful code samples and this tool enables that without too much time or effort.  The general idea is this... While editing code, highlight the code sample worth keeping and file it in a text file without leaving the vi or Vim environment. 

Here are the requirements for this utility:
  • You use 'vi' or 'Vim'
  • Create a subdirectory called "notes" under your home directory
  • Place the "snip.sh" utility in your personal 'bin' directory or somewhere in your PATH

Here is the code: (See Instructions in the code comments)
#!/bin/bash #Script: snip.sh #Purpose: Grab certain lines of text from the file # you are editing/viewing in vi/vim, # and append this snippet to a notes file. # #How to use in vi/vim session: # Position cursor on 1st line to save, press the letters # ma # Position cursor on last line to save, press the letters # mb # Capture the snippet to the default ~/notes/snippet.txt file # :'a,'b w !snip.sh # Or, Capture the snippet to a specified file ~/notes/favorite.txt # :'a,'b w !snip.sh favorite ["Optional Short description"] # #This code assumes you will store the snippets in a "notes" #subdirectory under your home directory. #Place this code in your personal bin directory, or in your PATH. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SNIP=${1:-snippet} DESC="${2:-UNNAMED SNIPPET}" echo '#[SNIP]------------------------------------' >> ~/notes/${SNIP}.txt echo "#[SNIP] ${DESC} " >> ~/notes/${SNIP}.txt echo '#[SNIP]------------------------------------' >> ~/notes/${SNIP}.txt cat - >> ~/notes/${SNIP}.txt

Saturday, December 25, 2010

Open Multiple Text Files With One Click using ActiveState Perl

This is a convenient way of opening many text files at once in an editor that supports multiple tabs.  For those that use a text editor for taking notes, logs, work files, journaling, etc. and often reference these files frequently this technique is very easy to use.

Requirements are:
  • An editor that supports multiple files open at once in separate tabs.
  • ActiveState Perl installed (and in your PATH)
  • The sourcecode below
  • setup a Windows Shortcut to the perl code
In the code below you will see that it is configured to open 4 files when "log.pl" runs. 
The files are:
YYYY_MM.txt (each month it will create a new file)
JOURNAL.txt
INFO.txt
TIPS.txt

You can change the names and the number of files you wish to open and the script will build the appropriate command line to run (see lines 20-23 below).

Make sure you specify the correct editor you are using... In this case it is using TextPad.exe, but it can use other editors if you specify the correct path to your editor (see line 14 below).

Source Code:
#!/usr/bin/perl
#Script : log.pl
#Purpose: To open multiple files at once in the editor that supports tabs
#Requirements: ActivePerl (in your path)
#Create a shortcut for this script
# Target: path to your script
# Start in: directory your script is in
use POSIX qw(strftime);

$scnt = 0; #string count
@strings = (); #array of strings
$cmd = ""; #command to run

$strings[$scnt++] = "C:\\Progra~1\\TextPa~1\\TextPad.exe"; #Use Textpad editor
#$strings[$scnt++] = "C:\\Progra~1\\EditPl~1\\editplus.exe"; #Use Editplus editor

#--------------------------------------------------------------------
# FILES YOU ENTER HERE WILL BE OPENED IN SEPARATE TABS IN YOUR EDITOR
#--------------------------------------------------------------------
$strings[$scnt++] = strftime("c:\\dir1\\notes\\%Y_%m.txt", localtime);
$strings[$scnt++] = "c:\\dir1\\notes\\JOURNAL.txt";
$strings[$scnt++] = "c:\\dir1\\notes\\INFO.txt";
$strings[$scnt++] = "c:\\dir1\\notes\\TIPS.txt";

foreach $str (@strings) { #Build Command string
$cmd .= chr(34) . $str . chr(34) . " ";
}

system ($cmd); #Open files in editor
  

Enjoy!