Posts Tagged ‘gsub’

Vi(m) tip #5: convert list of image files into latex figures

Wednesday, November 11th, 2009

If I select and copy (CMD+C) a bunch of image files in Finder
then paste (CMD+V) in TextEdit, then recopy and paste into vi(m), I can run the follow commands to make each file into a full blown latex figure. (Of course you can also just copy and paste the list using ls on any unix/linux machine.)

For eps files:


:%s/^\(.*eps\)$/\r\\begin{figure}[tbp]\r\\centering\r\\epsfig{file=\1,width=0.9\\linewidth,clip=}\r\\caption{}\r\\end{figure}/

and for others (png here, just replace with jpg etc.):


:%s/^\(.*png\)$/\r\\begin{figure}[tbp]\r\\centering\r\\includegraphics[width=0.9\\textwidth]{\1}\r\\caption{}\r\\end{figure}/

Example:

The first one-liner substitutes:


bilapl_all_bnds_ex.eps
bilapl_all_bnds_ey.eps
trilapl_all_bnds_ex.eps
trilapl_all_bnds_ey.eps
trilapl_all_bnds_ez.eps

with this:


\begin{figure}[tbp]
\centering
\epsfig{file=bilapl_all_bnds_ex.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=bilapl_all_bnds_ey.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=trilapl_all_bnds_ex.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=trilapl_all_bnds_ey.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=trilapl_all_bnds_ez.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

List audiobooks and/or music based on directories: Ruby Version

Thursday, November 5th, 2009

I previously posted a bash script to count and print plain text and html listings of a directory: intended for displaying audiobook and music libraries. I recently tried to use the bash script on a large directory and it stalled because sed can’t handle large input. Here’s a ruby version that does the same thing:


#!/usr/bin/ruby -w
usage = "Usage: list [filename] [directory] 
If filename ends with .html then output will be html,
else output will be plain text."
if not ARGV[0] or not ARGV[1]
  puts usage
elsif not File.directory? ARGV[1]
  puts "list: #{ARGV[1]} is not a directory"
  puts usage
else
  dir = ARGV[1]
  # should be quoted?
  plain = (`ls -1 "#{dir}"/*`+"\n")
  if(ARGV[0] =~ /.html$/i)
    plain = plain.split("\n")
    author_count = plain.find_all{|line| line=~/:$/}.length
    title_count = plain.find_all{|line| line=~/[^:]$/}.length
    body = plain.collect do |line|
      line.gsub(
        /(.*)([^:])$/,'      \1\2</br>').gsub(
        /^\s*$/,'    </div>').gsub(
        /.*\/(.*):$/,'    <h3>\1</h3>'+"\n"+'    <div class="books">')
    end
    output = "<html>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/> 
  <head> 
    <style type='text/css'>
      body {
        font : 10pt verdana;
        background: white;
        width: 95%
      }
      h2 {
        margin: 15px 0px 5px 0px;
      }
      h3 {
        margin: 15px 0px 5px 0px;
      }
      .books {
        border: 1px solid #dddddd;
        padding: 5px 5px 5px 10px;
        background: #eeeeff;
      }
    </style>
  </head>
  <body>
    <h2>#{title_count} titles and #{author_count} authors</h2>
  #{body.join("\n")}</div>
  </body> 
</html>"
  else
    output = plain;
  end
  File.open(ARGV[0], 'w') {|f| f.write(output) }
end

Escape code for html <pre> or <code> tag into clipboard: Ruby Version

Thursday, November 5th, 2009

I previously posted a bash script to speed up posting code in a <pre> or <code> tag in an html page or blog. The script escapes all less than and greater than symbols (< with &lt; and > with &gt;) in a given file then puts the results in the clipboard to facilitate pasting into a text area or text editor. I have now rewritten this idea in ruby. Now you can either cat in a file to the ruby script using pipe, or enter (paste) the input text directly. Save the following the ruby source in a file called escape-copy.rb:


#!/usr/bin/ruby -w
# Usage:
# cat input_file | ruby escape-copy.rb | pbcopy
# or
# ruby escape-copy.rb | pbcopy
# (enter or paste your text then CTRL-D on its own line as EOF)
print ARGF.read.gsub(/</,"<").gsub(/>/,">")

Note: I of course used escape-copy.rb to make posting the source above super easy!

Vi(m) tip #4: word count in human files

Monday, November 2nd, 2009

I’ve begun writing my Nanowrimo novel in vim. To make sure I’m writing at least 1666.66666666666666666666667 words a day I’m constantly checking my word count. Here’s one way to save (write) your file and check word count in a single command:


:w !wc -w

This of course assumes you are using vim on a unix/linux machine with the tool wc. Another way to count words is with only vi(m) functions:


:%s/[^ ]\+//gn

I prefer the first because the gsub method highlights every word in my document.

Note: I get the same count for both of these. Any reason why I wouldn’t?

source

Vi(m) tip #3: convert document to lowercase or uppercase

Friday, October 16th, 2009

Convert every matching regex (or line) to lowercase or uppercase using vims gsub command. In normal mode this will convert the entire document to lowercase (ie downcase all capitals):


:%s/[A-Z]/\L&/g

Likewise for to upcase:


:%s/[a-z]/\U&/g

source