tag:blogger.com,1999:blog-40846212301707748062024-03-08T05:30:42.834+01:00The Big Browser<i>M y b r a i n h u r t s !</i> <small><small><small>w e r e a l l y t h i n k w h a t y o u k n o w</small></small></small>Unknownnoreply@blogger.comBlogger23125tag:blogger.com,1999:blog-4084621230170774806.post-51326693022997551772021-03-27T00:03:00.003+01:002022-04-05T17:42:36.133+02:00Bulk convert APE (MonkeyAudio) files to FLAC recursively from command lineA oneliner that will search recursively for all <tt>.APE</tt> files under the current folder (ie <tt>.</tt>) and convert them to <tt>.FLAC</tt>. This will handle correctly file names containing special characters such as single and double quotes, brackets, etc.
<pre>
find . -type f -iname "*.ape" -exec sh -c 'ffmpeg -i "${1}" "${1%.*}.flac"' _ {} \;
</pre>
In this way you can convert between any format that ffmpeg can handle, for exaple convert all FLAC files to MP3:
<pre>
find . -type f -iname "*.flac" -exec sh -c 'ffmpeg -i "${1}" "${1%.*}.mp3"' _ {} \;
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-9113143784974078772020-11-29T22:15:00.010+01:002022-04-05T17:47:51.624+02:00Convert all MOV video files in the current folder to MP4<p>Convert a single MOV video file to MP4 format:</p>
<pre>
ffmpeg -i my_video.mov my_video.mp4
</pre>
Convert all MOV videos in the current folder and sub-folders to MP4 and delete the orignal .MOV files: For Linux or Mac, or on Windows under msys, cygwin, cmder etc.
<pre>
for video in `find . -iname "*.mov"` ; do
ffmpeg -i "${video}" "${video%.*}.mp4" && rm "${video}"
done
</pre>
NOTE: While the script above might work with most files it will fail with files containing special characters, such as spaces, single or double quotes, slashes, dashes or <tt>!#&</tt> and others.
This is a more robust version that will handle correctly special characters in file names:
<pre>
find . -iname '*.mov' -print0 | xargs -0 -I {} ffmpeg -i '{}' '{}.mp4' && rm '{}'
</pre>
And another version of the same that avoids the use of <tt>xargs</tt> by using a sub-shell:
<pre>
find . -type f -iname "*.mov" -exec sh -c 'ffmpeg -i "${1}" "${1%.*}.mp4" && rm "${1}" ' _ {} \;
</pre>
If <tt>ffmpeg</tt> is not present on your system install it with <tt>apt install ffmpeg</tt> (on Linux) or <tt>brew install ffmpeg</tt> on Mac.
Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-4084621230170774806.post-7210381509571121922020-11-22T23:38:00.025+01:002020-11-24T10:29:09.514+01:00CSS Style Sheet HTML Preview Generator (python script)<p>How do I see an HTML preview of every single CSS rule defined in a CSS file?
<p>There is no fixed way to do it, especially if your CSS style sheets make abundant use of <tt>absolute</tt> and <tt>fixed</tt> positioning. This script below is a quick and dirty attempt to generate HTML tags for all the rules defined in a CSS file. If the rule is defined for a known HTML tag then that tag is rendered as itself, otherwise a <tt>div</tt> tag is created.
<p>The script does not cover every possible scenario and only supports the most common CSS constructs. It wraps all fixed CSS rules into a dedicated <tt>iframe</tt> in order to prevent elements from overlapping. You can disable wrapping fixed elements into frames by passing <tt>--no-frames</tt>.
<p>If you have a large CSS file the HTML generation can take up to several minutes since nested rules are iterated recursively.
<div style="border: 1px dotted #e6e3e3;border-radius: 13px;-moz-border-radius: 13px;-webkit-border-radius:13px;padding:20px;margin:10px;background: #f7ecdc;-webkit-box-shadow: rgb(95 10 10 / 36%) 4px 5px 11px 0px; 0px;-moz-box-shadow:rgba(204, 0, 0, 0.355469) 4px 5px 11px 0px;box-shadow: rgb(216 184 184 / 36%) 4px 5px 11px 0px;">
⚠️ For complex CSS style sheets (1MB+) the generated HTML can cause the browser to freeze or rendering to take a signficant time even on a performant computer. You have been warned!</div>
<h4>Dependencies</h4>
<p>The CSS preview generator depends on the <tt><a target="_blank" href="https://pypi.org/project/cssutils/">cssutil</a></tt> python module which can be installed with:
<pre>pip3 install cssutils</pre>
<p>You are welcome to <a target="_blank" href="https://github.com/glowinthedark/css_stylesheet_preview_generator">fork</a>, improve and provide feedback.
<h4>Usage</h4>
<pre>css_stylesheet_preview_generator.py mystyles.css -o mypreview.html</pre>
For more options run with the <tt>-h</tt> or <tt>--help</tt> switch.
<h4>Example output</h4>
<div class="separator" style="clear: both;"><a href="https://1.bp.blogspot.com/-zey3DZwUsUc/X7rv8CRZ9VI/AAAAAAAAqsU/GPxsIh7ZSbQ_rmiGD8LmTqx2V60OYTDbgCLcBGAsYHQ/s750/css_stylesheet_html_preview.png" style="display: block; padding: 1em 0; text-align: left; "><img alt="" border="0" width="320" data-original-height="466" data-original-width="750" src="https://1.bp.blogspot.com/-zey3DZwUsUc/X7rv8CRZ9VI/AAAAAAAAqsU/GPxsIh7ZSbQ_rmiGD8LmTqx2V60OYTDbgCLcBGAsYHQ/s320/css_stylesheet_html_preview.png"/></a></div>
<h4>Python CSS preview generator script</h4>
<script src="http://legbehindneck.com/assets/js/embed.js?target=https%3A%2F%2Fgithub.com%2Fglowinthedark%2Fcss_stylesheet_preview_generator%2Fblob%2Fmaster%2Fcss_preview_generator.py&style=github&showBorder=on&showLineNumbers=on&showFileMeta=on"></script>
<!-- github gist embed
<hr>
<p><script src="https://gist.github.com/glowinthedark/7fa0594f47142bc603137c92703a5855.js"></script>
-->
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-77546068226806490802017-10-16T00:05:00.001+02:002021-04-27T21:34:00.665+02:00Barcelona city bikes availability: a console REST client with curl and jq<p>How do you consume a REST api in the terminal? All you can do is just <tt>curl</tt> a REST end point which will spit some uglified JSON into the console, right?</p><p>Let's say you want to get a list of public bike stations in Barcelona:</p><pre>curl -s https://bicing.barcelona/get-stations</pre><div style="clear: both;"><br />
<p><img border="0" data-original-height="143" data-original-width="600"
src="https://4.bp.blogspot.com/-JQao_QUcRW0/WePXGIyL6yI/AAAAAAAAZQM/76mfcM44-HwDTZWVS8ug-xhGOC0AR_RSACLcBGAs/s1600/terminal-bikes-bcn-raw-json.png"/><br />
</p></div><p>Hmmm, that's not very readable... Can you do better than this?<br />
</p><p>Yes, you can!</p><p>Meet <b><a href="https://stedolan.github.io/jq/">jq</a></b> — a full-fledged console JSON parser, as powerful as <tt>awk</tt> but for JSON.</p><p>Available in standard linux repos:</p><pre>sudo apt install jq</pre><p>as well as in homebrew on Mac:<br />
</p><pre>brew install jq</pre><p>Now we can display the list of all bike stations in Barcelona with JSON pretty-printing and colorization:</p><pre>curl -s https://bicing.barcelona/get-stations | jq</pre><p><div style="clear: both;"><a href="https://3.bp.blogspot.com/-ug1Y3D6rLys/WePR825bj9I/AAAAAAAAZQE/_wwPMJjomD8LW1DQieSQxpzhwxLMReK-gCPcBGAYYCw/s1600/terminal-bikes.png"><br />
<img src="https://3.bp.blogspot.com/-ug1Y3D6rLys/WePR825bj9I/AAAAAAAAZQE/_wwPMJjomD8LW1DQieSQxpzhwxLMReK-gCPcBGAYYCw/s1600/terminal-bikes.png"/><br />
</a></div></p><p>Now, this is more readable. But how about filtering for specific bike stations, say the ones closest to our daily destinations?</p><p>I pulled the station IDs at <a href="https://www.bicing.barcelona/mapa-de-disponibilitat">offical Barcelona bicing<br />
site</a> and pass the IDs as parameters to <tt>jq</tt>:</p><pre>curl -s https://bicing.barcelona/get-stations --compressed | jq -r '.stations[] | select(.id==("153", "154", "339", "382","150"))'</pre><p>Since I don't want all the fields in the JSON, I pass in the JSON keys for the data I want to keep.</p><p>Get bikes by station IDs, and extract station address, number of bikes, and number of parking slots:</p><pre>curl -s https://bicing.barcelona/get-stations --compressed | jq -r '.stations[] | select(.id==("153", "154", "339", "382","150")) | .streetName,.bikes,.slots'</pre><p>This looks a bit better, but by default <tt>jq</tt> prints one key per line, and I want to add some formatting, so that data for one station shows in a single line. I can use <tt>jq</tt> string interpolation to achieve this. Here is the final version which prints our data in a tabular format (available bikes, parking slots, station id, and address):</p><pre>echo 'AVL\tEMP\tSID\tADR'
curl -s https://www.bicing.barcelona/get-stations --compressed | jq -r '.stations[] | select(.id==("153", "154", "339", "382","150")) | "\(.bikes)\t\(.slots)\t\(.id)\t\(.streetName), \(.streetNumber)"'</pre><p>And here is the final result:</p><div style="clear: both;"><p><a href="https://3.bp.blogspot.com/-DKY0wNxpvmE/WePTcNsfGPI/AAAAAAAAZQI/YSpGzh7c098srwdMbC61kAQ8XPoYmH4BgCLcBGAs/s1600/terminal-curl-jq-bikes-barcelon-final.png"><br />
<img src="https://3.bp.blogspot.com/-DKY0wNxpvmE/WePTcNsfGPI/AAAAAAAAZQI/YSpGzh7c098srwdMbC61kAQ8XPoYmH4BgCLcBGAs/s320/terminal-curl-jq-bikes-barcelon-final.png"/><br />
</a></div><p><i>And, finally</i>, what's the point of adding the <tt>--compressed</tt> parameter to <tt>curl</tt>?</p><p>Setting <tt>--compressed</tt> is equivalent to setting <tt>--header 'Accept-Encoding: gzip,deflate'</tt> — as a result the server will compress the output with gzip or deflate (if capable of compression), and <tt>curl</tt> will decompress the response on the fly, which will make the result appear faster since less bytes will be sent over the network.<br />
</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-65384193820197853982016-07-13T08:09:00.001+02:002016-07-13T08:18:43.642+02:00Simple webserver with twisted<p>The Python oneliner webserver <tt>python -m SimpleHTTPServer</tt> (for Python 2.x) or <tt>python -m http.server</tt> (for Python 3.x) is useful in many scenarios, but sometimes it simply does not cut it because it is meant for development only, is not secure, and is single-threaded.</p><p>A better oneline webserver comes with <a href="https://twistedmatrix.com/documents/15.0.0/web/howto/using-twistedweb.html">Twisted</a>, the library that the original bittorrent reference implementation was written in.<br />
Installing twisted for the current user only:</p><pre>pip install twisted --user</pre><p>Installing twisted system-wide:</p><pre>sudo pip install twisted</pre><p>Once installed you can run the default twisted webserver with a command like <tt>twistd -n web --path .</tt> where the dot represents the current folder. Replace with an absolute path if you need.</p><p>The version below additionally displays the internal and external IP addresses, so that you can launch the server and instantly connect to it by entering the displayed IP on your mobile devices or another computers in the local network.</p><pre>echo 'IP Address:' $(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'):8080
echo 'External IP:' $(wget http://ipinfo.io/ip -qO -):8080
twistd -n web --path .</pre><p>In order to be able to connect via the external IP you'll need to set up port forwarding on your router as described at <a href="http://portforward.com/">portforward.com</a>.</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-84018420021265009442016-03-03T23:14:00.001+01:002020-11-23T13:22:42.984+01:00Creating native dictionaries for the OSX Dictionary.appOSX has an integrated dictionary app which can be accessed by selecting some text in any application, right-clicking and then picking <b>Look up</b> from the context menu (three-finger-tap on a selected word should work too).<br />
<br />
The OSX Dictionary comes pre-installed with several dictionaries but creating your own dictionaries or converting existing ones is surprisingly complex.<br />
<br />
This post is an attempt to make this process easier for everyone. <br />
<br />
Most third-party dictionaries that I use are in DSL (ABBY Lingvo) and BGL (Babylon) formats. I have collected them over the years, and have been using them both on the desktop via the open-source app <a target="_new" href="http://goldendict.org/">GoldenDict</a>, and on my smartphone thanks to <a target="_new" href="https://play.google.com/store/apps/details?id=mobi.goldendict.android" target="_new">GoldenDict for android</a>. The OSX version of GoldenDict is still buggy, and apparently not properly maintained, so I wanted to find a way to migrate my existing dictionaries to the native OSX dictionary app.<br />
<br />
After a bit of research I finally figured out the toolchain for converting my existing dictionaries in Babylon and Lingvo DSL formats to the AppleDict format which the OSX Dictionary.app expects.<br />
<h3>Prerequisites</h3><h4>Auxiliary Tools for Xcode</h4><blockquote>Download the DMG for Auxiliary Tools for Xcode from <a target="_new" href="https://developer.apple.com/download/more/">developer.apple.com/download/more</a>. You'll need to sign in with your Apple ID in order to download. If you already have an iCloud account then you can use it. Once you sign in you can use the direct link to <a href="http://adcdownload.apple.com/Developer_Tools/Auxiliary_Tools_for_Xcode_7/Auxiliary_Tools_for_Xcode_7.dmg">Auxiliary Tools for Xcode 7</a> or use the latest version by searching for it on the <a href="https://developer.apple.com/download/more/">Apple downloads page</a>.<br />
<br />
Mount the DMG file by double-clicking it in Finder. Next, As the root user create a folder at <tt>/Developer/Extras</tt> and copy the <b>Dictionary Development Kit </b>folder from the Auxiliary Tools into that folder.<br />
<pre>sudo mkdir -p /Developer/Extras
sudo cp -r "/Volumes/Auxiliary Tools/Dictionary Development Kit" /Developer/Extras
</pre></blockquote><h4>Python 3</h4><blockquote>Install Python 3 via <a href="http://brew.sh/">homebrew</a> (OSX comes with Python 2.x preinstalled):<br />
<pre>brew cask install python3
</pre></blockquote><h4>Python 3 dependencies</h4><blockquote>Install lxml and BeautifulSoup, the parsers that pyglossary depends on:<br />
<pre>## for current user only
pip3 install lxml beautifulsoup4 --user
## system-wide, for all users
sudo pip3 install lxml beautifulsoup4
</pre></blockquote><h4>PyGlossary</h4><blockquote>Clone the <a target="_new" href="https://github.com/ilius/pyglossary" target="_new">pyGlossary</a> project to a folder on your drive:<br />
<pre>mkdir -p ~/projects
cd ~/projects
git clone --depth 1 git@github.com:ilius/pyglossary.git
</pre>Next, locate the path to <tt>pyglossary.pyw</tt> — that's the script you'll need for converting the DSL file in UTF-8 encoding to AppleDict XML.<br />
</blockquote><h3>DSL to AppleDict conversion</h3>The DSL to AppleDict sequence: <ol><li>DSL files come in Unicode Little Endian encoding, so the first step would be converting your DSL file from UTF-16 to UTF-8:</li>
<pre>iconv -f UTF-16 -t UTF-8 webster-original-utf16.dsl > webster.dsl
</pre><li>Now do the actual conversion to AppleDict source format. Make sure you include the correct path to <tt>pyglossary.pyw</tt>. When the command completes (which might take several minutes if the source file is big), a new file <tt>webster.xml</tt> will be created along with some make files.</li>
<pre>~/projects/pyglossary/pyglossary.pyw --read-format=ABBYYLingvoDSL --write-format=AppleDict webster.dsl webster.xml
</pre><li>The generated AppleDict source file will be created in a subfolder with the same name as the source file, i.e. <tt>webster</tt>. Next, we move into that folder and do the compilation of AppleDict source to the OSX binary dictionary format:</li>
<pre>cd webster
make
make install
</pre></ol>Running <tt>make install</tt> will copy the compiled dictionary to <tt>~/Library/Dictionaries</tt>. Restart the Dictionary.app, open preferences (⌘ + ,), and you should see the new dictionary available in the list. Enable the corresponding checkbox to make the dictionary active.<br />
<br />
Here is a bash script that automates the process of DSL to AppleDict conversion: <a target="_new" href="https://gist.github.com/glowinthedark/da0a026ce0fb691e418c306abc67e525">https://gist.github.com/glowinthedark/da0a026ce0fb691e418c306abc67e525</a>. Run the bash script without arguments to see usage notes.<br />
<script src="https://gist.github.com/glowinthedark/da0a026ce0fb691e418c306abc67e525.js"></script><br />
<br />
IMPORTANT: You will need to edit the script and set the correct value of <tt>PYGLOSSARY_HOME</tt>, by default it is <tt>~/projects/pyglossary</tt>.<br />
<br />
<h3>Babylon BGL to AppleDict conversion</h3>The conversion steps for BGL dictionaries:<br />
<br />
<script src="https://gist.github.com/glowinthedark/cbb9941f72ae1776b9fbbfff39666ce7.js"></script><br />
<br />
A bash script for BGL to AppleDict is available at <a target="_new" href="https://gist.github.com/glowinthedark/cbb9941f72ae1776b9fbbfff39666ce7">https://gist.github.com/glowinthedark/cbb9941f72ae1776b9fbbfff39666ce7</a>. To get this toolchain to work I recommend reading the readme files which are included both with pyglossary and the Apple Dictionary Development Kit. I have included the most important points in the help section of the bash script for DSL.<br />
<h3>Where to get the dictionaries?</h3>There are literally hundreds, maybe thousands of dictionaries in <a target="_new" href="http://informationworker.ru/lingvo.en/dsl_main_dlg.htm">DSL</a>, <a target="_new" href="http://download.huzheng.org/">Stardict</a>, and <a target="_new" target="_new" href="http://www.babylon.com/free-dictionaries/">Babylon BGL</a> formats. A search engine query with the format and language pairs will most likely return some links where you'll be able to download the files. For example try <a target="_blank" href="https://www.google.es/search?q=stardict+english-spanish">stardict+english+spanish</a>. The <a target="_blank" href="http://goldendict.org/dictionaries.php">Goldendict dictionaries download page</a> also has some useful links.<br />
<br />
There are also a couple of command line utilities for converting between free dictionary formats: <a target="_new" href="http://freecode.com/projects/dictconv">dictconv</a>, <a target="_new" href="https://sourceforge.net/projects/xdxf/files/Converter_%20many%20to%20many/">makedict</a>.Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-4084621230170774806.post-1792907643705757622012-08-16T17:22:00.000+02:002016-06-16T20:33:21.547+02:00Firebug Lite Bookmarklet for Internet Explorer<p>Firebug is what makes Firefox great, but how about other dysfunctional browsers like Internet Explorer? There is a way to use a stripped-down version of Firebug on IE, thanks to <a href="http://getfirebug.com/firebuglite/">Firebug Lite</a>.</p><p>To use it, just drag the bookmarklet icon below to the Internet Explorer Favorites bar.</p><div style="text-align:center; width:80px; padding:5px; border:1px solid #C4D3CC; background:#F0FFF8; -moz-border-radius: 1em; -webkit-border-radius: 1em; border-radius:1em; -webkit-box-shadow: 2px 2px 6px rgba(0,0,0,0.6);"><a href="javascript:var%20firebug=document.createElement('script');firebug.setAttribute('src','http://fbug.googlecode.com/svn/lite/branches/firebug1.4/content/firebug-lite-dev.js');document.body.appendChild(firebug);(function(){if(window.firebug.version){firebug.init();}else{setTimeout(arguments.callee);}})();void(firebug);">Firebug</a><br />
</div><br />
NOTE: It might take some time to load the bookmarklet, i.e. ten, twenty seconds or even more.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-55645359294410789402012-04-14T22:02:00.006+02:002016-04-16T13:48:49.460+02:00Linux: How to export Google Chrome passwords to CSV file<p>Update: A simpler method is described in this stackoverflow.com post <a href="http://superuser.com/a/675167/65975">How can I export chrome passwords?</a></p><p>Having spent some time googling for a way to export Chrome passwords to an external file to my surprise I could not find a straightforward solution that would work irregardless of the version of Chrome/Chromium. Finally, <a href="http://code.google.com/p/chromium/wiki/LinuxPasswordStorage">this page</a> provided a hint to a solution which worked for me.</p><p>Older versions of Chrome/Chromium for Linux stored unencrypted passwords in an SQLite database under:</p><pre>### Google Chrome:
~/.config/google-chrome/Default/Login Data
### Chromium:
~/.config/chromium/Default/Login Data
</pre><p>In newer versions of Chrome this file is still present, but the passwords are not there anymore - they are stored using the encrypted password storage provided by the system, either Gnome Keyring or KWallet, which means that simply dumping the tables will not work. On OSX the Chrome passwords are in the system keychain, and can be viewed from <i>System Keychain</i></p>The method below relies on forcing Chrome to use a temporary profile folder in combination with unencrypted password storage. This approach will not affect your current Chrome profile in any way.</p><p>So, to export your passwords to a human readable text file:</p><ol><li>Connect to your Google Account in Chrome Settings so that your passwords are synched with the Google cloud storage. Make sure that you have ticked <i>Passwords</i> in <i>Advanced Sync Settings</i>.</li>
<li>Wait for a while until the data is synched, and then close all the Chrome windows.</li>
<li>Start Chrome using one of the two command lines below. Google Chrome has a green-red-yellow icon <img src="https://www.google.com/images/icons/product/chrome-16.png">, while the Chromium icon is in shades of blue <img width="16" height="16" src="http://www.chromium.org/_/rsrc/1302286216006/config/customLogo.gif">. <pre>### Google Chrome
google-chrome --user-data-dir=/tmp/chrome-tmp --password-store=basic
### Chromium
chromium --user-data-dir=/tmp/chrome-tmp --password-store=basic
</pre>This will launch Chrome with a custom profile folder without affecting your current profile. </li>
<li>Setup Google Synching for the new temporary profile and wait until everything is synced from the cloud, i.e. your bookmarks appear, extensions show up, the theme is applied, etc. Verify that the passwords are restored from the Google cloud by looking under Settings → Personal Stuff → Manage Saved Passwords. If they do not appear, then wait a couple of minutes more. Note: the fastest way to get to the stored passwords page in settings is to type <i>password</i> in the Search box in the top right.</li>
<li>Exit Chrome.</li>
<li>Next, open a terminal and change to the newly created Chrome profile <pre>cd /tmp/chrome-tmp/Default
</pre></li>
<li>Now, open the <tt>Login Data</tt> SQLite database using the <tt><a title="SQLite is a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world." href="http://www.sqlite.org/">sqlite3</a></tt> command line utility and dump the <tt>logins</tt> table. For this to work, you need to have <tt>sqlite3</tt> installed on your system. <pre>sqlite3 'Login Data'
</pre></li>
<li>Next, at the <tt>SQLite</tt> prompt enter the commands below. For help on available commands type <tt>.help</tt> at the prompt. <br />
<pre>.mode csv # other options are `html', `tabs', etc.
.headers on
.separator ","
.output chrome_passwords.csv
select * from logins;
.exit
</pre></li>
</ol><p>Now you should have a file named <tt>chrome_passwords.csv</tt> containing all your Chrome passwords. To open it with LibreOffice, type: <pre>libreoffice --calc chrome_passwords.csv
</pre>Unknownnoreply@blogger.com8tag:blogger.com,1999:blog-4084621230170774806.post-67806939742244295192011-06-25T19:28:00.010+02:002012-02-09T01:47:16.799+01:00Bash: Run command on each line in a file<p>This script runs a user-defined command on each line in a text file. The filename should come as the first argument, everything following it will be interpreted as the command name with arguments, if any.
<p>For example, if you have a list of file names saved to a file, and want to do something with them, e.g. see file details, you could run <tt>`foreach filelist.txt ls -alh`</tt>.
<h3>Long Version</h3>
<pre>
#!/bin/bash
if [ "$#" == "0" ]; then
echo -e "USAGE:\n `basename $0` <filename.txt> <cmd args...>"
exit 1
fi
list=$1
shift 1
args="$*"
while read -r line <&3
do
echo $args $line
eval $args $line
done 3<"$list"
</pre>
<h3>Shorter Version — the power of xargs</h3>
<p>Things are much simplier with <code>xargs</code> - it will read a list of arguments from standard input and run a command on each argument. By default arguments are separated by space, but you can specify your own separator with the <code>-d</code> (delimiter) option.
<p>If you work with file names you might want to use <code>xargs -0</code> to specify that the parameters are delimited by the the null separator. This is useful for example when you need to correctly handle file names containing spaces or other wierd characters. To generate the parameters with the null separators you could use <code>find -print0</code> to find the files matching your criteria.
<p>Here is an example that finds all .avi files in the current folder and print detailed info using `file` utility.
<pre>
find . -iname "*.avi" -print0 | xargs -0 file
</pre>
<p>But <code>find</code> by itself is pretty powerful and can handle this particular use case on its own without using <code>xargs</code>.
<pre>
find . -iname "*.avi" -exec file "{}" \;
</pre>
Note that there is an important difference between the two commands: the <code>xargs</code>-version will only run <code>file</code> <b>once</b> passing all the parameters in one go, while <code>find -exec</code> will run the <code>file</code> command multiple times for each argument, so if the program you are running takes a while to start the performance in the second case might be significantly worse.Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-4084621230170774806.post-84715356307080850432010-08-31T18:52:00.037+02:002012-04-21T22:21:45.181+02:00Using Google Translate from console<p>If you have ever used <a href="http://translate.google.com/translate_tools">Google Translate</a> and wished you could do the same from console, here is a Python script that does just that.
</p>
<p>The script will translate words and entire sentences between any language pair known to Google Translate. It will accept both text passed in as shell arguments, as well as data from standard input.
<p>NOTE: This script stopped working after the translation API version 1 was discontinued on December 2011. See the updated script below for a working version.
</p>
<span style='padding:30px 20px 10px 0px'>NOTE: According to <a href="http://code.google.com/apis/language/translate/v1/reference.html">http://code.google.com/apis/language/translate/v1/reference.html</a></span>
<div style="border:1px dotted red;border-radius: 13px;-moz-border-radius: 13px;-webkit-border-radius:13px;padding:20px;margin:10px;background:#FFC3D3;-webkit-box-shadow:rgba(204, 0, 0, 0.355469) 4px 5px 11px 0px;
-moz-box-shadow:rgba(204, 0, 0, 0.355469) 4px 5px 11px 0px;
box-shadow:rgba(204, 0, 0, 0.355469) 4px 5px 11px 0px;">
<b>Important:</b> Google Translate API v1 was officially deprecated on May 26, 2011; it was shut off completely on December 1, 2011. For text translations, you can use the Google Translate API v2, which is now <b>available as a paid service</b>. For website translations, we encourage you to use the Google Website Translator gadget.
</div>
<pre>
#!/usr/bin/env python
from urllib2 import urlopen
from urllib import urlencode
import sys
import os
# The google translate API can be found here:
# http://code.google.com/apis/ajaxlanguage/documentation/#Examples
# Language codes are listed here:
#http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
if len(sys.argv) < 3:
name = os.path.basename(sys.argv[0])
print '''
Usage:
%s en es lovely spam
%s es en < file.txt
Available language codes are listed here:
http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
''' % (name,name)
sys.exit(-1)
## hack to be able to display UTF-8 in Windows console
if sys.platform == "win32":
## set utf8 console
if not sys.stdin.encoding == 'cp65001':
os.system('chcp 65001 > nul')
class UniStream(object):
__slots__= "fileno", "softspace",
def __init__(self, fileobject):
self.fileno= fileobject.fileno()
self.softspace = False
def write(self, text):
if isinstance(text, unicode):
os.write(self.fileno, text.encode("utf_8"))
else:
os.write(self.fileno, text)
sys.stdout= UniStream(sys.stdout)
sys.stderr= UniStream(sys.stderr)
lang1=sys.argv[1]
lang2=sys.argv[2]
langpair='%s|%s'%(lang1,lang2)
if len(sys.argv) > 3:
text=' '.join(sys.argv[3:])
else:
text=sys.stdin.read()
base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
params=urlencode( (('v',1.0),
('q',text),
('langpair',langpair),) )
url=base_url+params
content=urlopen(url).read()
start_idx=content.find('"translatedText":"')+18
translation=content[start_idx:]
end_idx=translation.find('"}, "')
translation=translation[:end_idx]
sys.stdout.write(translation + '\n')
</pre>
<p>This is the updated script that uses the web API. Should work <i>after</i> December 2011.</p>
<pre>
#!/usr/bin/env python
import sys
import os
import urllib2
from urllib import urlencode
import cookielib
import re
# The google translate API can be found here (***NOT OPERATIONAL SINCE DECEMBER 2011***):
# http://code.google.com/apis/ajaxlanguage/documentation/#Examples
# Language codes are listed here:
#http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
if len(sys.argv) < 3:
name = os.path.basename(sys.argv[0])
print '''
Usage:
%s en es lovely spam
%s es en < file.txt
Available language codes are listed here:
http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
''' % (name,name)
sys.exit(-1)
## hack to be able to display UTF-8 in Windows console
def fix_win32_console():
## set utf8 console
if not sys.stdin.encoding == 'cp65001':
os.system('chcp 65001 > nul')
class UniStream(object):
__slots__= "fileno", "softspace",
def __init__(self, fileobject):
self.fileno= fileobject.fileno()
self.softspace = False
def write(self, text):
if isinstance(text, unicode):
os.write(self.fileno, text.encode("utf_8"))
else:
os.write(self.fileno, text)
sys.stdout= UniStream(sys.stdout)
sys.stderr= UniStream(sys.stderr)
if sys.platform == "win32":
fix_win32_console()
lang1=sys.argv[1]
lang2=sys.argv[2]
if len(sys.argv) > 3:
text=' '.join(sys.argv[3:])
else:
text=sys.stdin.read()
base_url='http://translate.google.com.br/translate_a/t'
# sample browser request
#http://translate.google.com/translate_a/t?client=t&text=col&hl=en&sl=en&tl=es&multires=1&otf=2&ssel=4&tsel=0&sc=1
params=urlencode({'client':'t',
'text':text,
'hl':'en',
'sl':lang1,
'tl':lang2,
'otf':2,
'multires':1,
'ssel':0,
'tsel':0,
'sc':1,
})
url=base_url + '?' + params
cookiejar = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1018.0 Safari/535.19'),
('Referer', 'http://translate.google.com/')
]
response = opener.open(url)
translation=response.read()
matcher = re.search('\[\[\["(?P<human_readable_chunk>[^")]*)', translation)
sys.stdout.write(matcher.group('human_readable_chunk'))
</pre>
<p>Save the script to a file such as gtrans.py and run it as follows (assuming you have <a href="http://python.org/download/">Python</a> in your path):</p>
<pre>
python gtrans.py en es Nobody expects the Spanish Inquisition
</pre>
<p>The first two parameters are the language codes. A list of codes known to google translate is available here: <a href="http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray">http://code.google.com/apis/ajaxlanguage/documentation/reference.html</a>. For some reason, not all of the listed codes are actually accepted, for example, <span style="font-family:Courier;font-size:70%">bo</span> for Tibetan</p>
<p>To pipe a text file through the script:</p>
<pre>
python gtrans.py en es < myfile.txt
</pre>
<p>It is also possible to enter multi-line text directly from the console. To do so, call the script with the language codes only, i.e:</p>
<pre>
python gtrans.py en es
</pre>
<p>Enter your text and use the Enter key to start a new line. When you are done, press CTR+d (on Linux) or CTR+z followed by Enter (on Windows).</p>
<p>Note: On Windows input in other languages than English is not going to work. This is due to poor support of Unicode input in cmd.exe. On Linux international input works fine, provided that the console is UTF-8.
</p>
<p>Keep in mind though that google has a limit on the size of text to be translated.</p>
<a name="bash"></a>
<h3>Console Google Translate — curl-based version</h3>
<p>As an alternative, here is a bash script which uses curl and sed. Updated to work via Google Translate Web API.</p>
<pre>
#! /bin/bash
USAGE="Usage:
$0 en es Lovely spam!
Some codes: en|fr|de|ru|nl|it|es|ja|la|pl|bo
All language codes:
http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray"
if [ "$#" == "0" ]; then
echo "$USAGE"
exit 1
fi
FROM_LNG=$1
TO_LNG=$2
shift 2
QUERY=$*
UA="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040803"
URL="http://translate.google.com.br/translate_a/t?client=t&hl=en&sl=$FROM_LNG&tl=$TO_LNG&otf=2&multires=1&ssel=0&tsel=0&sc=1"
curl --data-urlencode "text=$QUERY" -A $UA -s -g -4 $URL | sed 's/","/\n/g' | sed 's/\]\|\[\|"//g' | sed 's/","/\n/g' | sed 's/,[0-9]*/ /g'
</pre>Unknownnoreply@blogger.com5tag:blogger.com,1999:blog-4084621230170774806.post-34648315592746621902010-08-28T23:08:00.013+02:002012-06-30T09:08:05.171+02:00Linux console online dictionary lookupI often need to lookup words in various online dictionaries, but in many cases I would prefer to get the results right in the console, instead of having to launch the browser, type in the URL and wait for the page to load.<br />
<br />
For example, here is a bash oneliner that translates an English word to Spanish right in the console:<br />
<br />
<b>Wordreference English-Spanish: </b><br />
<pre>curl -j -s -A "Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.13337/458; U; en) Presto/2.2.0" "http://www.wordreference.com/es/translation.asp?tranword=`echo $* | sed 's/ /%20/g'`" | html2text -utf8 | less -R
</pre><br />
<b>Wordreference Spanish to English:</b><br />
<pre>curl -j -s -A "Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.13337/458; U; en) Presto/2.2.0" "http://www.wordreference.com/es/en/translation.asp?spen=`echo $* | sed 's/ /%20/g'`" | html2text -utf8 | less -R
</pre><br />
<b>Merriam Webster Online English Dictionary: </b><br />
<pre>curl -j -s -A "Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.13337/458; U; en) Presto/2.2.0" "http://www.merriam-webster.com/dictionary/`echo $* | sed 's/ /%20/g'`" | html2text -utf8 | less -R
</pre><br />
<b>Wikipedia (EN):</b><br />
<pre>curl -j -s -A "Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.13337/458; U; en) Presto/2.2.0" "http://en.wikipedia.org/wiki/`echo $* | sed 's/ /%20/g'`" | html2text -utf8 | less -R </pre><br />
<br />
Usage: Save the required script to a file, make it executable (<span style="font-family: 'Courier New', Courier, monospace;">chmod +x webster</span>), and then type the script name from the console with the word(s) you need translated as arguments.<br />
<br />
Instead of creating a separate script for each dictionary it might be more practical to add the scriptlets as aliases in your <i>.bashrc</i> file, e.g:<br />
<br />
<pre>alias webster="curl -j -s -A "Opera/9.60 (J2ME/MIDP; Opera Mini/4.2.13337/458; U; en) Presto/2.2.0" "http://www.merriam-webster.com/dictionary/`echo $* | sed 's/ /%20/g'`" | html2text -utf8 | less -R"
</pre>Make sure you have <i>curl</i>, <i>html2text</i> , <i>sed</i>, and <i>less</i> installed. <p>A similar result can be achieved by using some console browser like lynx or w3m.</> Install w3m: <pre>sudo apt-get install w3m # for Ubuntu
sudo pacman -S w3m # Archlinux
</pre><p>Create a wrapper script which includes the URL:</p><pre>echo 'w3m "http://www.wordreference.com/es/translation.asp?tranword=$*"' > ~/bin/en2es
chmod +x ~/bin/en2es
</pre><p>Now the script can be run like this:</p><pre>en2es word of mouth
</pre><p>The advantage in this case is that you can use all the standard browser features, like clicking links, etc.</p>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-4084621230170774806.post-19763681106100885172010-04-30T17:42:00.025+02:002012-10-26T23:51:42.277+02:00Bash oneliners or helpful aliases for your .bashrcUseful bash oneliners that might or might not be worth including as aliases in your <tt>~/.bashrc</tt> file.<br />
<br />
<div style="color: #373742; font-family: calibri,arial,helvetica; font-size: 9pt;"><table border="0" style="border-collapse: collapse; border: 0px; margin: 5px; padding: 10px;"><tbody>
<tr><td colspan="2" style="color: gray;"><b><i>Bash Oneliners</i></b></td> </tr>
<tr><th width="70%"><span style="font-style: italic; text-align: left;">command</span> <br />
</th><th><span style="font-style: italic; text-align: left;">action</span> <br />
</th></tr>
<tr></tr>
<tr><td><pre>grep -v "^#" $* | grep -v "^$"</pre></td><td>print file contents excluding comments starting with # and empty lines</td></tr>
<tr><td><pre>netstat -tpauln</pre></td><td>print open TCP connections *and* application names</td></tr>
<tr><td><pre>find . -maxdepth 1 -type d -exec du -sh {} \; | sort -hr</pre></td><td>print sorted directory sizes starting with the biggest</td></tr>
<tr><td><pre>find . -maxdepth 1 -mindepth 1 -type d -print0 | xargs -0 du -sh | sort -hr</pre></td><td>same as above, but supposedly more efficient for lots of files</td></tr>
<tr><td><pre>help test | less</td><td>does not do much work on itself but it is good reading</pre></td></tr>
<tr><td><pre>[ -n $SOMEVAR ] && echo "IS_NOT_EMPTY" || echo "IS_EMPTY"</pre></td><td>execute first command on empty string, or execute second command otherwise (the Python way)</td></tr>
<tr><td><pre>mkdir $(echo {01..22}spam)</pre></td><td>create folders 01spam, 02spam, 03spam, etc.</td></tr>
<tr><td><pre>cp filename{,.bak}</pre></td><td>create backup of file</td></tr>
<tr><td><pre>!!</td><td>rerun previous command</pre></td></tr>
<tr><td><pre>sudo !!</td><td>rerun previous command as root</pre></td></tr>
<tr><td><pre>python -m SimpleHTTPServer</pre></td><td>run web server for current dir on port 8000 (Python 2.x version)</td></tr>
<tr><td><pre>python -m http.server</pre></td><td>run web server for current dir (Python 3 version) </td></tr>
<tr><td><pre>:w !sudo tee %</pre></td><td>write file in vim as root</td></tr>
<tr><td><pre>ssh-copy-id remote-machine</pre></td><td>copy rsa/dsa key to remote machine for public authentication</td></tr>
<tr><td><pre>ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg</pre></td><td>record current desktop to mpeg file</td></tr>
<tr><td><pre>echo -e ${PATH//\:/\\n}</pre></td><td>string replace with bash (the example replaces colon with end-of-line </td></tr>
<tr><td><pre>curl -O http://rss.timegenie.com/forex.xml</pre></td><td>download file to local file <tt>forex.xml</tt></td></tr>
<tr><td><pre>tr -dc ' -~' < /dev/urandom | head -c 20</pre></td><td>generate random string</td></tr>
<tr><td><pre>echo $(($RANDOM % 100))</pre></td><td>generate random number in range</td></tr>
<tr><td><pre>sudo find . -user root -exec chown spamneggs {} \;</pre></td><td>find all files owned by root and change owner to spamneggs</td></tr>
<tr><td><pre>function mcd {
mkdir ${1} && cd ${1}
}</pre></td><td>a <tt>~/.bashrc</tt> shortcut to make a directory and move into it.</td></tr>
<tr><td><pre>function catw {
cat `which "${1}"`
}</pre></td><td>a <tt>~/.bashrc</tt> shortcut for typing <tt>cat `which script`</tt></td></tr>
<tr><td><pre>function vimw {
vim `which "${1}"`
}</pre></td><td>a <tt>~/.bashrc</tt> shortcut for typing <tt>vim `which script`</tt>; you get the idea...</td></tr>
<tr><td></td><td></td></tr>
</tbody></table></div>See also:
<ul><li><a href="http://www.catonmat.net/blog/top-ten-one-liners-from-commandlinefu-explained/" target="_new"><small>Top Ten One-Liners from CommandLineFu Explained </small></a></li>
<li><a href="http://blog.emson.co.uk/2009/06/18-useful-bash-scripts-for-web-developers/" target="_new"><small>18 Useful bash scripts </small></a></li>
<li> <a href="http://www.wlug.org.nz/BashOneLiners" target="_new"><small>BashOneLiners</small></a></li>
</ul>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-4084621230170774806.post-30868361386097835212010-04-15T10:28:00.017+02:002011-10-21T16:41:23.071+02:00Running a command recursively on all files in a directory tree<h5>
Windows solution</h5>
Here is a script which calls mplayer on every file in a directory including subfolders:<br />
<br />
The script will work on dirs containing spaces and non-ascii characters since all file names are converted to short dos names (via the `~s` modifier).<br />
<pre> set mplayer=z:\path_to_mplayer\mplayer.exe
for /f "delims=" %%f in ('dir /b /s "%cd%"') do (%mplayer% "%%~sf")
</pre>
<h5>
Linux bash solution</h5>
The bash shell solution is much easier to read than the windows one:<br />
<pre>
#!/bin/sh
cwd=`pwd`
for f in `find $cwd -type f -iname "*.mp3" `; do
mplayer $f
done
</pre>
<h3>
A Better Solution</h3>
An alternative solution for both Windows and Linux would be to create a playlist file first, and then run a single mplayer instance with the <code>-playlist</code> switch:
<br />
<h5>Linux bash</h5>
<pre> #!/bin/sh
find $pwd -type f -iname "*.mp3" > /tmp/allfiles.m3u
mplayer -playlist /tmp/allfiles.m3u
</pre>
<h5>
Windows Batch</h5>
<pre> set mplayer=z:\path_to_mplayer\mplayer.exe
set playlist=%TEMP%/mp3playlist.m3u
dir /b /s "%cd%" | find /I ".mp3" > "%playlist%"
%mplayer% -playlist "%playlist%"
</pre>
<h5>Shortest solution</h5>
<pre>
mplayer $(find . -iname "*.mp3")
</pre>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-4084621230170774806.post-52565101372935158092009-11-27T13:48:00.004+01:002009-11-30T15:44:53.009+01:00VIM usability: my .vimrcMy .vimrc compiled from various Internet sources. The goal was to make vim more user-friendly for someone who is familiar with popular shortcuts like Ctr+C for copy and Ctr+V for paste, but still retaining most classic vim shortcuts. Since Ctr+V cannot be used for vertical block selection it still can be done with Ctr+Q if your OS allows it.
<a name='more'></a>
<pre>
<span class="Comment">"set verbose=9</span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">Space</span><span class="Special">></span> :silent noh<span class="Special"><</span><span class="Special">Bar</span><span class="Special">></span>echo<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">autocmd</span> <span class="Type">VimEnter</span> * <span class="Statement">set</span> <span class="PreProc">vb</span> <span class="PreProc">t_vb</span>=
<span class="Comment">"use arrows to move within wrapped lines</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">Up</span><span class="Special">></span> gk
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">Down</span><span class="Special">></span> gj
<span class="Comment">" Make Alt+Left, Alt+Right work like in eclipse</span>
<span class="Comment">": retrace your movements in file (backward)</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">M-Left</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>
<span class="Comment">": retrace your movements in file (forward)</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">M-Right</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-I</span><span class="Special">></span>
<span class="Comment">"map <M-F3> :guu!<Bar>:gUU?<CR></span>
<span class="Comment">" toggle line numbers with F12</span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">F12</span><span class="Special">></span> :set number!<span class="Special"><</span><span class="Special">Bar</span><span class="Special">></span>set number?<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">" Toggle line wrapping</span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">F3</span><span class="Special">></span> :set wrap!<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">"nnoremap <F11> :set nonumber!<CR>:set foldcolumn=0<CR></span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">F10</span><span class="Special">></span> :set paste!<span class="Special"><</span><span class="Special">Bar</span><span class="Special">></span>set paste?<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">F11</span><span class="Special">></span> :set hls!<span class="Special"><</span><span class="Special">Bar</span><span class="Special">></span>set hls?<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">" run current file on windows <a href="http://vim.wikia.com/wiki/Best_Vim_Tips">http://vim.wikia.com/wiki/Best_Vim_Tips</a></span>
<span class="Statement">vmap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-F5</span><span class="Special">></span> :<span class="Special"><</span><span class="Special">C-U</span><span class="Special">></span>let old_reg=@"<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>gvy:silent!!start rundll32.exe url.dll,FileProtocolHandler <span class="Special"><</span><span class="Special">C-R</span><span class="Special">><</span><span class="Special">C-R</span><span class="Special">></span>"<span class="Special"><</span><span class="Special">CR</span><span class="Special">><</span><span class="Special">CR</span><span class="Special">></span>:let @"=old_reg<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">" Use CUA keystrokes for clipboard: CTRL-X, CTRL-C, CTRL-V and CTRL-z</span>
<span class="Comment">" source $VIMRUNTIME/mswin.vim</span>
<span class="Comment">" :::from mswin::</span>
<span class="Comment">" Alt-Space is System menu</span>
<span class="Statement">if</span> <span class="Identifier">has</span><span class="Statement">(</span><span class="Constant">"gui"</span><span class="Statement">)</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">M-Space</span><span class="Special">></span> :simalt ~<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">inoremap</span> <span class="Special"><</span><span class="Special">M-Space</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>:simalt ~<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">cnoremap</span> <span class="Special"><</span><span class="Special">M-Space</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">></span>:simalt ~<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">endif</span>
<span class="Comment">" CTRL-A is Select all</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">C-A</span><span class="Special">></span> gggH<span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>G
<span class="Statement">inoremap</span> <span class="Special"><</span><span class="Special">C-A</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>gg<span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>gH<span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>G
<span class="Statement">cnoremap</span> <span class="Special"><</span><span class="Special">C-A</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">></span>gggH<span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>G
<span class="Statement">onoremap</span> <span class="Special"><</span><span class="Special">C-A</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">></span>gggH<span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>G
<span class="Statement">snoremap</span> <span class="Special"><</span><span class="Special">C-A</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">></span>gggH<span class="Special"><</span><span class="Special">C-O</span><span class="Special">></span>G
<span class="Statement">xnoremap</span> <span class="Special"><</span><span class="Special">C-A</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">></span>ggVG
<span class="Comment">" CTRL-F4 is Close window</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">C-F4</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-W</span><span class="Special">></span>c
<span class="Statement">inoremap</span> <span class="Special"><</span><span class="Special">C-F4</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-O</span><span class="Special">><</span><span class="Special">C-W</span><span class="Special">></span>c
<span class="Statement">cnoremap</span> <span class="Special"><</span><span class="Special">C-F4</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">><</span><span class="Special">C-W</span><span class="Special">></span>c
<span class="Statement">onoremap</span> <span class="Special"><</span><span class="Special">C-F4</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">><</span><span class="Special">C-W</span><span class="Special">></span>c
<span class="Comment">" backspace and cursor keys wrap to previous/next line</span>
<span class="Statement">set</span> <span class="PreProc">backspace</span>=indent<span class="Statement">,</span>eol<span class="Statement">,</span>start <span class="PreProc">whichwrap</span>+=<span class="Special"><</span><span class="Special">,</span><span class="Special">></span><span class="Statement">,</span>[<span class="Statement">,</span>]
<span class="Comment">" backspace in Visual mode deletes selection</span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">BS</span><span class="Special">></span> d
<span class="Comment">" CTRL-X and SHIFT-Del are Cut</span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">C-X</span><span class="Special">></span> "+x
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">S-Del</span><span class="Special">></span> "+x
<span class="Comment">" CTRL-C and CTRL-Insert are Copy</span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">C-C</span><span class="Special">></span> "+y
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">C-Insert</span><span class="Special">></span> "+y
<span class="Comment">" CTRL-V and SHIFT-Insert are Paste</span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">C-V</span><span class="Special">></span> "+gP
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">S-Insert</span><span class="Special">></span> "+gP
<span class="Statement">cmap</span> <span class="Special"><</span><span class="Special">C-V</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-R</span><span class="Special">></span>+
<span class="Statement">cmap</span> <span class="Special"><</span><span class="Special">S-Insert</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-R</span><span class="Special">></span>+
<span class="Comment">" switch tabs with C+Tab and C+Sh+Tab</span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">C-Tab</span><span class="Special">></span> :tabn <span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">C-S-Tab</span><span class="Special">></span> :tabp <span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">set</span> <span class="PreProc">guitablabel</span>=%n/\ %t\ %M
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">C-t</span><span class="Special">></span> :tabnew<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">C-left</span><span class="Special">></span> :tabp<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">C-right</span><span class="Special">></span> :tabn<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">set</span> <span class="PreProc">noeb</span>
<span class="Statement">set</span> <span class="PreProc">vb</span> <span class="PreProc">t_vb</span>=<span class="Constant">""</span>
<span class="Statement">set</span> <span class="PreProc">noerrorbells</span>
<span class="Statement">set</span> <span class="PreProc">backupdir</span>=~/.vim_backup
<span class="Comment">" highlight current line</span>
<span class="Statement">set</span> <span class="PreProc">cul</span>
<span class="Comment">" from <a href="http://www.pixelbeat.org/vim.tips.html">http://www.pixelbeat.org/vim.tips.html</a></span>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">f9</span><span class="Special">></span> :w<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>:!python %<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">" Only run the script once</span>
<span class="Statement">if</span> <span class="Identifier">exists</span><span class="Statement">(</span><span class="Constant">"g:did_myvimrc"</span><span class="Statement">)</span>
<span class="Statement">finish</span>
<span class="Statement">endif</span>
<span class="Statement">function</span> SmartTab<span class="Statement">()</span>
<span class="Statement">if</span> <span class="Identifier">strpart</span><span class="Statement">(</span> getline<span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>, <span class="Constant">0</span>, col<span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)-</span><span class="Constant">1</span> <span class="Statement">)</span> <span class="Statement">=~</span> <span class="Constant">'^\s*$'</span>
<span class="Statement">return</span> <span class="Constant">"\<Tab>"</span>
<span class="Statement">else</span>
<span class="Statement">if</span> &omnifunc <span class="Statement">!=</span> <span class="Constant">''</span>
<span class="Statement">return</span> <span class="Constant">"\<C-X>\<C-O>"</span>
<span class="Statement">elseif</span> &dictionary <span class="Statement">!=</span> <span class="Constant">''</span>
<span class="Statement">return</span> <span class="Constant">"\<C-K>"</span>
<span class="Statement">else</span>
<span class="Statement">return</span> <span class="Constant">"\<C-N>"</span>
<span class="Statement">endif</span>
<span class="Statement">endif</span>
<span class="Statement">endfunction</span>
<span class="Comment">" Bind tab to the SmartTab function</span>
<span class="Comment">" inoremap <Tab> <C-R>=SmartTab()<cr></span>
<span class="Statement">if</span> <span class="Identifier">has</span><span class="Statement">(</span><span class="Constant">"autocmd"</span><span class="Statement">)</span>
<span class="Comment"> " When editing a file, always jump to the last cursor position</span>
<span class="Statement">autocmd</span> <span class="Type">BufReadPost</span> *
<span class="Special"> \</span> <span class="Statement">if</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">"'\""</span><span class="Statement">)</span> <span class="Statement">></span> <span class="Constant">0</span> <span class="Statement">&&</span> <span class="Identifier">line</span> <span class="Statement">(</span><span class="Constant">"'\""</span><span class="Statement">)</span> <span class="Statement"><=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">"$"</span><span class="Statement">)</span> |
<span class="Special"> \</span> <span class="Statement">exe</span> <span class="Constant">"normal g'\""</span> <span class="Statement">|</span>
<span class="Special"> \</span> <span class="Statement">endif</span>
<span class="Statement">endif</span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">M-Right</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-W</span><span class="Special">><</span><span class="Special">Right</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">M-Left</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-W</span><span class="Special">><</span><span class="Special">Left</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">M-Up</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-W</span><span class="Special">><</span><span class="Special">Up</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">M-Down</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-W</span><span class="Special">><</span><span class="Special">Down</span><span class="Special">></span>
<span class="Comment">"<a href="http://vim.wikia.com/wiki/Vimrc">http://vim.wikia.com/wiki/Vimrc</a></span>
<span class="Comment">" Press space to clear search highlighting and any message already displayed.</span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">Space</span><span class="Special">></span> :silent noh<span class="Special"><</span><span class="Special">Bar</span><span class="Special">></span>echo<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">set</span> <span class="PreProc">ignorecase</span>
<span class="Statement">set</span> <span class="PreProc">smartcase</span>
<span class="Statement">let</span> g:did_myvimrc <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Comment">"</span> <span class="PreProc">Overview:</span>
<span class="Comment">" - I use vim 7 in xterminals (gnome-terminal, console, gvim, etc) on light</span>
<span class="Comment">" backgrounds.</span>
<span class="Comment">" - In non-xterminals (like the typical linux terminal) I use dark</span>
<span class="Comment">" backgrounds.</span>
<span class="Comment">" - Generally, I don't change default vim keyboard shortcuts, such that I can</span>
<span class="Comment">" use 'pristine' vim installations as well.</span>
<span class="Comment">" - I enable all the 'cool' features: code folding, syntax highlighting,</span>
<span class="Comment">" automatic complete, filetype plugins, indentation, line numbering, custom</span>
<span class="Comment">" ruler, custom status line.</span>
<span class="Comment">" - Even if I use gvim, I actually don't use the GUI features. I disable</span>
<span class="Comment">" pretty much everything, except the menus.</span>
<span class="Comment">" - I use vim modelines.</span>
<span class="Comment">" - I use the longer names for commands and settings (e.g. 'tabstop' instead</span>
<span class="Comment">" of 'ts'), for readability.</span>
<span class="Comment">" - When saving files the RD_FileWritePre() function checks for $Date: ... $</span>
<span class="Comment">" to update the date.</span>
<span class="Comment">" - I enabled .exrc/.vimrc files per-directory, and I disabled any unsafe</span>
<span class="Comment">" commands in such files.</span>
<span class="Comment">" - New keyboard shortcuts:</span>
<span class="Comment">" _' _{ _( _[ _< _t _W _T _c _C _"</span>
<span class="Comment">" These are selection wrappers. Select something and type _' to have single</span>
<span class="Comment">" quotes around the selection. In normal mode type _W to unwrap the string.</span>
<span class="Comment">" For example move over the ' single quote character and type _W: this will</span>
<span class="Comment">" remove the ' and the other matching quote.</span>
<span class="Comment">" _t and _T are for XML/HTML tags. _t to wrap the selection around a given</span>
<span class="Comment">" tag name. _T to strip the tag under the cursor.</span>
<span class="Comment">" _c and _C are for comments. It will detect the file type and use propers</span>
<span class="Comment">" comments.</span>
<span class="Comment">" - New command: Fullscreen. Nice for editing text-only documents.</span>
<span class="Comment">" - I use the matchit and taglist plugins. I do not use XML edit plugins.</span>
<span class="Comment">" - I have my own XML edit functions, which I like better than those available</span>
<span class="Comment">" online in other scripts.</span>
<span class="Comment">" - New abbreviations:</span>
<span class="Comment">" isodate</span>
<span class="Comment">" Adds the date in ISO format: 2008-11-04 22:43:09 +0200.</span>
<span class="Comment">"</span>
<span class="Comment">" $Date$</span>
<span class="Comment">" Adds the date in ISO format, with a twist: $Date: 2009-05-29 13:18:26 E. Europe Daylight Time $ </span>
<span class="Comment">"</span>
<span class="Comment">" rdcopy</span>
<span class="Comment">" My usual 'copyright' notice.</span>
<span class="Comment">"</span>
<span class="Comment">"</span> <span class="PreProc">Problems:</span>
<span class="Comment">" - _c and _C (comment wrapping) is not very good at the moment.</span>
<span class="Comment">" - XML/HTML editing still needs improvements, but it's usable.</span>
<span class="Comment">" Script settings</span>
<span class="Comment">" ===============</span>
<span class="Comment">" Memory usage: I allow vim to use a lot of memory.</span>
<span class="Statement">let</span> s:RD_use_mem <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Comment">" I allow vim to use colors</span>
<span class="Statement">let</span> s:RD_use_conn <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Comment">" I allow vim to save as much information as possible.</span>
<span class="Statement">let</span> s:RD_privacy <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Comment">" I change the above values as needed when I copy my .vimrc to different servers.</span>
<span class="Comment">" Settings</span>
<span class="Comment">" ========</span>
<span class="Comment">" I do not like the vi-compatible mode.</span>
<span class="Statement">set</span> <span class="PreProc">nocompatible</span>
<span class="Comment">" Make backspace work as usual.</span>
<span class="Statement">set</span> <span class="PreProc">backspace</span>=indent<span class="Statement">,</span>eol<span class="Statement">,</span>start
<span class="Comment">" Diff option: ignore whitespace</span>
<span class="Statement">set</span> <span class="PreProc">diffopt</span>+=iwhite
<span class="Comment">" Allow changing the buffer without saving.</span>
<span class="Statement">set</span> <span class="PreProc">hidden</span>
<span class="Statement">if</span> s:RD_use_mem
<span class="Comment"> " How many lines of history to remember.</span>
<span class="Statement">set</span> <span class="PreProc">history</span>=1000
<span class="Comment"> " Use all the memory needed, for maximum performance.</span>
<span class="Statement">set</span> <span class="PreProc">maxmemtot</span>=2000000
<span class="Statement">set</span> <span class="PreProc">maxmem</span>=2000000
<span class="Statement">set</span> <span class="PreProc">maxmempattern</span>=2000000
<span class="Statement">endif</span>
<span class="Comment">" Do not place the cursor at the start of the line when using Page up/down.</span>
<span class="Statement">set</span> <span class="PreProc">nostartofline</span>
<span class="Statement">set</span> <span class="PreProc">nrformats</span>=alpha<span class="Statement">,</span>hex
<span class="Comment">" Allow the cursor to be positioned where there is no actual character.</span>
<span class="Comment">" Visual block mode only.</span>
<span class="Statement">set</span> <span class="PreProc">virtualedit</span>=block
<span class="Statement">if</span> s:RD_privacy <span class="Statement">==</span> <span class="Constant">1</span>
<span class="Statement">set</span> <span class="PreProc">viminfo</span>=''
<span class="Statement">else</span>
<span class="Comment"> " Remember information between vim restarts.</span>
<span class="Statement">set</span> <span class="PreProc">viminfo</span>='200<span class="Statement">,</span><300<span class="Statement">,</span>s150<span class="Statement">,</span>%<span class="Statement">,</span>h<span class="Statement">,</span>!
<span class="Statement">endif</span>
<span class="Comment">" Allow specified keys that move the cursor left/right to move to the previous/next line when the cursor is on the first/last character in the line.</span>
<span class="Comment">" b (backspace), s (space) and the arrows.</span>
<span class="Statement">set</span> <span class="PreProc">whichwrap</span>=b<span class="Statement">,</span>s<span class="Statement">,</span><span class="Special"><</span><span class="Special">,</span><span class="Special">></span>
<span class="Comment">" Auto-complete</span>
<span class="Comment">" =============</span>
<span class="Comment">" Scanning included files is really slow for Perl files</span>
<span class="Comment">" Maybe I should change this only for Perl files</span>
<span class="Statement">set</span> <span class="PreProc">complete</span>-=i
<span class="Comment">" Turn on wild menu which shows auto-complete options in the command mode</span>
<span class="Statement">set</span> <span class="PreProc">wildmenu</span>
<span class="Comment">" File-related</span>
<span class="Comment">" ============</span>
<span class="Comment">" Unicode support</span>
<span class="Statement">set</span> <span class="PreProc">encoding</span>=utf-8
<span class="Statement">set</span> <span class="PreProc">fileencodings</span>=ucs-bom<span class="Statement">,</span>utf-8<span class="Statement">,</span>latin1<span class="Statement">,</span>default
<span class="Comment">" Support all three, in this order</span>
<span class="Statement">set</span> <span class="PreProc">fileformats</span>=unix<span class="Statement">,</span>dos<span class="Statement">,</span>mac
<span class="Comment">" Detect the type of file and enable filetype-specific indenting.</span>
<span class="Statement">filetype</span> <span class="Type">plugin</span> <span class="Type">indent</span> <span class="Type">on</span>
<span class="Comment">" Backups</span>
<span class="Statement">set</span> <span class="PreProc">backup</span>
<span class="Statement">set</span> <span class="PreProc">backupcopy</span>=auto
<span class="Statement">set</span> <span class="PreProc">backupskip</span>=/tmp/*<span class="Statement">,</span>$TMPDIR/*<span class="Statement">,</span>$TMP/*<span class="Statement">,</span>$TEMP/*<span class="Statement">,</span>*test*<span class="Statement">,</span>*temp*<span class="Statement">,</span>*tmp*<span class="Statement">,</span>*tst*<span class="Statement">,</span>*~<span class="Statement">,</span>*bak
<span class="Comment">" (I decided I like the Visual mode more)</span>
<span class="Comment">" Start the selection mode with Shift+Arrows/Home/End/PageUp/PageDown, and with</span>
<span class="Comment">" the mouse</span>
<span class="Comment">"set selectmode=mouse,key</span>
<span class="Comment">" Start the Visual/Selection mode with using Shift+Arrows/etc</span>
<span class="Statement">set</span> <span class="PreProc">keymodel</span>=startsel
<span class="Comment">" User interface</span>
<span class="Comment">" ==============</span>
<span class="Statement">if</span> <span class="Identifier">has</span><span class="Statement">(</span><span class="Constant">"gui_win32"</span><span class="Statement">)</span>
<span class="Statement">set</span> <span class="PreProc">guifont</span>=Bitstream_Vera_Sans_Mono<span class="Statement">:</span>h9<span class="Statement">:</span>cANSI
<span class="Statement">endif</span>
<span class="Comment">"if (has("gui_running") || $TERM == 'xterm')</span>
<span class="Comment">" set background=light</span>
<span class="Comment">"else</span>
<span class="Comment">" set background=dark</span>
<span class="Comment">"endif</span>
<span class="Comment">" The command line height.</span>
<span class="Statement">set</span> <span class="PreProc">cmdheight</span>=2
<span class="Comment">" As much as possible of the last line in a window will be displayed. When not included, a last line that doesn't fit is replaced with </span><span class="Constant">"@"</span><span class="Comment"> lines.</span>
<span class="Statement">set</span> <span class="PreProc">display</span>=lastline
<span class="Comment">"</span> <span class="PreProc">GUI Options:</span>
<span class="Comment">"if has("gui_running")</span>
<span class="Comment">" " GUI cursor: no blinking</span>
<span class="Comment">" set guicursor+=a:blinkon0</span>
<span class="Comment">"</span>
<span class="Comment">" " no toolbar</span>
<span class="Comment">" set guioptions-=T</span>
<span class="Comment">" " no autoselect </span>
<span class="Comment">" set guioptions-=a</span>
<span class="Comment">"</span>
<span class="Comment">" " Use console messages instead of GUI dialogs</span>
<span class="Comment">" set guioptions+=c</span>
<span class="Comment">"endif</span>
<span class="Comment">" Always show the status line</span>
<span class="Statement">set</span> <span class="PreProc">laststatus</span>=2
<span class="Comment">" Don't blink!</span>
<span class="Statement">set</span> <span class="PreProc">novisualbell</span>
<span class="Comment">" Show the line numbers.</span>
<span class="Statement">set</span> <span class="PreProc">number</span>
<span class="Comment">" Threshold for reporting number of lines changed.</span>
<span class="Statement">set</span> <span class="PreProc">report</span>=0
<span class="Comment">" Enable the ruler with the format:</span>
<span class="Comment">" {buffer number}{modified/readonly flag}: {file type} [current line, current column] {position percentage in file}</span>
<span class="Statement">set</span> <span class="PreProc">ruler</span>
<span class="Statement">set</span> <span class="PreProc">rulerformat</span>=%25(%n%m%r<span class="Statement">:</span>\ %Y\ [%l<span class="Statement">,</span>%v]\ %p%%%)
<span class="Comment">" Minimal number of screen lines to keep above and below the cursor.</span>
<span class="Statement">set</span> <span class="PreProc">scrolloff</span>=3
<span class="Comment">" Shorter messages.</span>
<span class="Statement">set</span> <span class="PreProc">shortmess</span>=as
<span class="Comment">" Show (partial) command in status line.</span>
<span class="Statement">set</span> <span class="PreProc">showcmd</span>
<span class="Comment">" Status line format:</span>
<span class="Comment">" {buffer number}: {file name, relative path to the current working directory}{modified flag}{readonly flag}</span>
<span class="Comment">" {help flag}{preview flag} [file type, encoding, format] [current line-total lines, current column][position percentage in file]</span>
<span class="Statement">set</span> <span class="PreProc">statusline</span>=%n<span class="Statement">:</span>\ %f%m%r%h%w\ [%Y<span class="Statement">,</span>%{&fileencoding}<span class="Statement">,</span>%{&fileformat}]\ [%l-%L<span class="Statement">,</span>%v][%p%%]
<span class="Statement">if</span> s:RD_use_conn
<span class="Statement">set</span> <span class="PreProc">ttyfast</span>
<span class="Statement">endif</span>
<span class="Comment">" Do not redraw while running macros (much faster).</span>
<span class="Statement">set</span> <span class="PreProc">lazyredraw</span>
<span class="Comment">" What to show when you do :set list.</span>
<span class="Statement">set</span> <span class="PreProc">listchars</span>=tab<span class="Statement">:</span>\|\ ,trail:.,extends:>,precedes:<,<span class="PreProc">eol</span>:$
<span class="Comment">" Allow using the mouse everywhere.</span>
<span class="Statement">set</span> <span class="PreProc">mouse</span>=a
<span class="Comment">" Nice, but too slow..</span>
<span class="Comment">"</span>
<span class="Comment">"set cursorline</span>
<span class="Comment">"set cursorcolumn</span>
<span class="Comment">"highlight CursorLine guibg=#f7f7f7</span>
<span class="Comment">"highlight CursorColumn guibg=#f7f7f7</span>
<span class="Comment">" Formatting and indenting</span>
<span class="Comment">" ========================</span>
<span class="Statement">set</span> <span class="PreProc">formatoptions</span>=tcroqn <span class="Comment">" see help</span>
<span class="Comment">" Automatic indenting, on new line.</span>
<span class="Statement">set</span> <span class="PreProc">autoindent</span>
<span class="Comment">" Enable C-style indenting.</span>
<span class="Statement">set</span> <span class="PreProc">cindent</span>
<span class="Comment">" Real tab chars.</span>
<span class="Comment">"set noexpandtab</span>
<span class="Comment">" Smart indenting, based on the typed code.</span>
<span class="Statement">set</span> <span class="PreProc">smartindent</span>
<span class="Comment">" Tab spacing.</span>
<span class="Statement">set</span> <span class="PreProc">tabstop</span>=4
<span class="Statement">set</span> <span class="PreProc">shiftwidth</span>=4
<span class="Statement">set</span> <span class="PreProc">softtabstop</span>=4
<span class="Comment">" Use tabs at the start of a line, spaces elsewhere.</span>
<span class="Comment">"set smarttab </span>
<span class="Comment">" If on Vim will wrap long lines at a character in 'breakat' rather than at the last character that fits on the screen.</span>
<span class="Statement">set</span> <span class="PreProc">linebreak</span>
<span class="Comment">" Syntax highlighting and related</span>
<span class="Comment">" ===============================</span>
<span class="Statement">if</span> s:RD_use_conn
<span class="Comment"> " Enable syntax highlighting</span>
<span class="Statement">syntax</span> <span class="Type">on</span>
<span class="Statement">endif</span>
<span class="Comment">" Show matching brackets.</span>
<span class="Statement">set</span> <span class="PreProc">showmatch</span>
<span class="Comment">" How many tenths of a second to blink matching brackets for</span>
<span class="Statement">set</span> <span class="PreProc">mat</span>=5
<span class="Comment">" Do not highlight the string we searched.</span>
<span class="Statement">set</span> <span class="PreProc">nohlsearch</span>
<span class="Comment">" Highlight the searched string, while typing.</span>
<span class="Statement">set</span> <span class="PreProc">incsearch</span>
<span class="Comment">" Highlight advanced perl vars inside strings</span>
<span class="Comment">"let perl_extended_vars=1 </span>
<span class="Comment">" Highlight HTML code inside PHP strings</span>
<span class="Statement">let</span> php_htmlInStrings<span class="Statement">=</span><span class="Constant">1</span>
<span class="Comment">" Highlight mySQL queries inside PHP strings</span>
<span class="Comment">"let php_sql_query=1</span>
<span class="Comment">" Highlight PHP code inside ASP-like tags</span>
<span class="Comment">"let php_asp_tags=1</span>
<span class="Comment">" Code folding</span>
<span class="Comment">" ============</span>
<span class="Comment">"set foldenable </span>
<span class="Comment">" Fold column width</span>
<span class="Comment">"set foldcolumn=4</span>
<span class="Comment">" Make folding indent sensitive</span>
<span class="Comment">"set foldmethod=indent </span>
<span class="Comment">"set foldminlines=2</span>
<span class="Comment">" Folds with a higher level will be automatically closed.</span>
<span class="Comment">"set foldlevel=4</span>
<span class="Comment">" Don't auto-open folds</span>
<span class="Comment">"set foldopen=</span>
<span class="Comment">" (Auto) commands</span>
<span class="Comment">" ===============</span>
<span class="Comment">" Add the Eretidy command</span>
<span class="Comment">"command Eretidy call <SID>RD_EditRetidy()</span>
<span class="Comment">" This adds the new Fullscreen command.</span>
<span class="Statement">command</span> Fullscreen <span class="Statement">call</span> <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span><span class="Normal">RD_Fullscreen</span>()
<span class="Comment">" Skip file change notifications in mounted folders</span>
<span class="Comment">"autocmd FileChangedShell /mnt/*,~/mnt/* call <SID>RD_FileChanged()</span>
<span class="Comment">" Automatically update $Date: ... $ when the file is saved. </span>
<span class="Comment">"autocmd FileWritePre,BufWritePre * call <SID>RD_FileWritePre()</span>
<span class="Comment">"` Mappings</span>
<span class="Comment">" ========</span>
<span class="Comment">"</span> <span class="PreProc">DISABLED:</span><span class="Comment"> These cause small issues.</span>
<span class="Comment">" I like automatic completion for quotes and parentheses.</span>
<span class="Comment">"inoremap </span><span class="Constant">" "</span><span class="Comment">"<LEFT></span>
<span class="Comment">"inoremap ' ''<LEFT></span>
<span class="Comment">"inoremap ( ()<LEFT></span>
<span class="Comment">"inoremap [ []<LEFT></span>
<span class="Comment">"inoremap ` ``<LEFT></span>
<span class="Comment">"inoremap { {}<LEFT></span>
<span class="Comment">" Make the Home key jump to the first non-white space, or to the start of the</span>
<span class="Comment">" line.</span>
<span class="Statement">inoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">Home</span><span class="Special">></span> <span class="Special"><</span><span class="Special">C-o</span><span class="Special">></span>:call <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_map_home('i')<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">"vnoremap <silent> <Home> :<C-u>call <SID>RD_map_home('v')<CR></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">Home</span><span class="Special">></span> :call <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_map_home('n')<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">let</span> mapleader<span class="Statement">=</span><span class="Constant">'_'</span>
<span class="Comment">" Quick way to change the directory to the file I am currently editing</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">Leader</span><span class="Special">></span>. :exe 'cd ' . substitute(expand("%:p:h"), ' ', "\\\\ ", 'g')<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">" This allows me to quickly wrap any selection with some usual characters</span>
<span class="Comment">" (quotes, angle quotes, parentheses, etc)</span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">Leader</span><span class="Special">></span>" :<span class="Special"><</span><span class="Special">C-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_wrapper('"')<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>' :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper("'")<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>` :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper('`')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>{ :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper('{')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>( :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper('(')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>[ :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper('[')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>< :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper('<')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>t :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_wrapper('t')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>w :call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_unwrapper()<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>t :call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_unwrap_tag()<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Comment">" add/remove comments</span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>c :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_comment('v')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>c :<span class="Special"><</span><span class="Special">c-u</span><span class="Special">></span>call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_uncomment('v')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>c :call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_comment('n')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Statement">nnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">leader</span><span class="Special">></span>c :call <span class="Special"><</span><span class="Special">sid</span><span class="Special">></span>rd_uncomment('n')<span class="Special"><</span><span class="Special">cr</span><span class="Special">></span>
<span class="Comment">" Tag lists toggle.</span>
<span class="Comment">" If the taglist vim plugin is available.</span>
<span class="Statement">if</span> <span class="Statement">(</span>exists<span class="Statement">(</span><span class="Constant">'loaded_taglist'</span><span class="Statement">)</span> <span class="Statement">&&</span> loaded_taglist <span class="Statement">!=</span> <span class="Constant">'no'</span><span class="Statement">)</span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">F8</span><span class="Special">></span> :TlistToggle<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">noremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">S-F8</span><span class="Special">></span> :TlistOpen<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">endif</span>
<span class="Comment">" Autocommands</span>
<span class="Comment">" ============</span>
<span class="Comment">" Ensure every file does syntax highlighting (full)</span>
<span class="Comment">"autocmd BufEnter * :syntax sync fromstart</span>
<span class="Statement">autocmd</span> <span class="Type">FileType</span> html,xml,xhtml <span class="Statement">call</span> <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span><span class="Normal">RD_XML_edit</span><span class="Statement">()</span>
<span class="Statement">autocmd</span> <span class="Type">FileType</span> php <span class="Statement">setlocal</span> <span class="PreProc">iskeyword</span>+=@
<span class="Comment">" Sets the current working directory based on the file being edited.</span>
<span class="Statement">autocmd</span> <span class="Type">BufEnter</span> * <span class="Statement">lcd</span> <span class="Identifier">%</span><span class="Identifier">:p:h</span>
<span class="Comment">" Abbreviations</span>
<span class="Comment">" =============</span>
<span class="Comment">" Current date and time, ISO format.</span>
<span class="Statement">iab</span> isodate <span class="Special"><</span><span class="Special">C-r</span><span class="Special">></span>=<span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_isodate()<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">iab</span> $Date$ <span class="Special"><</span><span class="Special">C-r</span><span class="Special">></span>="$Date: " . <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_isodate() . " $"<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">iab</span> :vimset: <span class="Special"><</span><span class="Special">C-r</span><span class="Special">></span>=<span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_vimset_generate()<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Comment">"</span>
<span class="Comment">" My usual </span><span class="Constant">"copyright"</span><span class="Comment"> line. I put this in almost all files I code.</span>
<span class="Comment">"iab rdcopy <C-o>:call <SID>RD_copyright()<CR></span>
<span class="Comment">" Variables</span>
<span class="Comment">" =========</span>
<span class="Comment">" Changes for the :TOhtml command</span>
<span class="Statement">let</span> use_xhtml <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">let</span> html_use_css <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">let</span> html_ignore_folding <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">let</span> html_number_lines <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Comment">" Changes for the TagList</span>
<span class="Statement">let</span> Tlist_Enable_Fold_Column <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Statement">let</span> Tlist_File_Fold_Auto_Close <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">let</span> Tlist_Sort_Type <span class="Statement">=</span> <span class="Constant">'order'</span>
<span class="Statement">let</span> Tlist_Use_Right_Window <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">let</span> Tlist_Inc_Winwidth <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Comment">" Functions</span>
<span class="Comment">" =========</span>
<span class="Comment">" Output ISO date.</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_isodate <span class="Statement">()</span>
<span class="Statement">return</span> <span class="Identifier">strftime</span><span class="Statement">(</span><span class="Constant">"%Y-%m-%d %H:%M:%S %z"</span><span class="Statement">)</span>
<span class="Statement">endfunction</span>
<span class="Comment">" Output a vim:set ... : modeline, based on current settings.</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_vimset_generate <span class="Statement">()</span>
<span class="Statement">let</span> l:str <span class="Statement">=</span> <span class="Constant">'vim:set'</span>
<span class="Statement">if</span> <span class="Statement">(</span>&spell <span class="Statement">&&</span> <span class="Statement">(</span>&filetype <span class="Statement">==</span> <span class="Constant">'html'</span> <span class="Statement">||</span> &filetype <span class="Statement">==</span> <span class="Constant">'xhtml'</span><span class="Statement">))</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' spell'</span>
<span class="Statement">if</span> !<span class="Identifier">empty</span><span class="Statement">(</span>&spelllang<span class="Statement">)</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' spl='</span> <span class="Statement">.</span> &spelllang
<span class="Statement">endif</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' ts='</span> <span class="Statement">.</span> &tabstop <span class="Statement">.</span> <span class="Constant">' sw='</span> <span class="Statement">.</span> &shiftwidth <span class="Statement">.</span> <span class="Constant">' sts='</span> <span class="Statement">.</span> &softtabstop
<span class="Statement">if</span> &smarttab
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' sta'</span>
<span class="Statement">else</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' nosta'</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> &expandtab
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' et'</span>
<span class="Statement">else</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' noet'</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> !<span class="Identifier">empty</span><span class="Statement">(</span>&fileencoding<span class="Statement">)</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' fenc='</span> <span class="Statement">.</span> &fileencoding
<span class="Statement">endif</span>
<span class="Statement">if</span> !<span class="Identifier">empty</span><span class="Statement">(</span>&fileformat<span class="Statement">)</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">' ff='</span> <span class="Statement">.</span> &fileformat
<span class="Statement">endif</span>
<span class="Statement">let</span> l:str <span class="Statement">.=</span> <span class="Constant">':'</span>
<span class="Statement">return</span> l:str
<span class="Statement">endfunction</span>
<span class="Comment">" This function toggles the 'fullscreen' mode. I sometimes need this, so I can</span>
<span class="Comment">" just view my files, without all the 'clutter': line numbers, folding,</span>
<span class="Comment">" command line, status line, etc.</span>
<span class="Statement">let</span> s:fullscreen_toggle <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_Fullscreen <span class="Statement">()</span>
<span class="Statement">if</span> s:fullscreen_toggle <span class="Statement">==</span> <span class="Constant">0</span>
<span class="Statement">set</span> <span class="PreProc">cmdheight</span>=1
<span class="Statement">set</span> <span class="PreProc">nonumber</span>
<span class="Statement">set</span> <span class="PreProc">laststatus</span>=0
<span class="Statement">set</span> <span class="PreProc">foldcolumn</span>=0
<span class="Statement">let</span> s:fullscreen_toggle <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">else</span>
<span class="Statement">set</span> <span class="PreProc">cmdheight</span>=2
<span class="Statement">set</span> <span class="PreProc">number</span>
<span class="Statement">set</span> <span class="PreProc">laststatus</span>=2
<span class="Statement">set</span> <span class="PreProc">foldcolumn</span>=4
<span class="Statement">let</span> s:fullscreen_toggle <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">endfunction</span>
<span class="Comment">" This function is used for quickly opening my ReTidy script (often needed</span>
<span class="Comment">" when cleaning pages).</span>
<span class="Comment">" Check out: <a href="http://www.robodesign.ro/mihai/my-projects/retidy">http://www.robodesign.ro/mihai/my-projects/retidy</a></span>
<span class="Comment">"let s:eretidy_toggle = 0</span>
<span class="Comment">"function s:RD_EditRetidy ()</span>
<span class="Comment">"" if s:eretidy_toggle == 0</span>
<span class="Comment">" let l:cwd = getcwd()</span>
<span class="Comment">"" cd /path/to/retidy</span>
<span class="Comment">" e src/retidy.php</span>
<span class="Comment">" e examples/retidy-stdio.php</span>
<span class="Comment">" e profiles/awebitor.php</span>
<span class="Comment">" e profiles/maximum.php</span>
<span class="Comment">"" e profiles/custom.php</span>
<span class="Comment">"" cd `=l:cwd`</span>
<span class="Comment">"" let s:eretidy_toggle = 1</span>
<span class="Comment">" else</span>
<span class="Comment">" bdel src/retidy.php examples/retidy-stdio.php profiles/awebitor.php profiles/maximum.php profiles/custom.php</span>
<span class="Comment">" let s:eretidy_toggle = 0</span>
<span class="Comment">" endif</span>
<span class="Comment">"endfunction</span>
<span class="Comment">" This function updates the $Date: ... $ definition to always be the date and</span>
<span class="Comment">" time of the last file write.</span>
<span class="Comment">"function s:RD_FileWritePre ()</span>
<span class="Comment">" if (&readonly || !&modified)</span>
<span class="Comment">" return</span>
<span class="Comment">" endif</span>
<span class="Comment">"</span>
<span class="Comment">" let l:my_search = </span><span class="Constant">"\\$Date: [0-9- +:]\\+ \\$"</span>
<span class="Comment">" let l:my_cpos = getpos('.')</span>
<span class="Comment">"" call cursor(1,1)</span>
<span class="Comment">" let l:my_spos = search(l:my_search, 'n', 400)</span>
<span class="Comment">"</span>
<span class="Comment">" if l:my_spos > 0</span>
<span class="Comment">" let l:my_date = <SID>RD_isodate()</span>
<span class="Comment">"" let l:my_line = getline(l:my_spos)</span>
<span class="Comment">"" let l:my_line = substitute(l:my_line, l:my_search, </span><span class="Constant">"$Date: "</span><span class="Comment"> . l:my_date . </span><span class="Constant">" $"</span><span class="Comment">, </span><span class="Constant">"e"</span><span class="Comment">)</span>
<span class="Comment">"" call setline(l:my_spos, l:my_line)</span>
<span class="Comment">" endif</span>
<span class="Comment">"</span>
<span class="Comment">" call setpos('.', l:my_cpos)</span>
<span class="Comment">"endfunction</span>
<span class="Comment">"</span>
<span class="Comment">" This function avoids an annoying FileChanged dialog for some folders I want</span>
<span class="Comment">"" (cifs mounted folders)</span>
<span class="Comment">"function s:RD_FileChanged ()</span>
<span class="Comment">" echohl WarningMsg</span>
<span class="Comment">"</span>
<span class="Comment">" if v:fcs_reason == 'deleted'</span>
<span class="Comment">" let v:fcs_choice = 'ask'</span>
<span class="Comment">"" echo 'File deleted!'</span>
<span class="Comment">"" else</span>
<span class="Comment">" echo 'File changed!'</span>
<span class="Comment">" endif</span>
<span class="Comment">"</span>
<span class="Comment">" echohl None</span>
<span class="Comment">"endfunction</span>
<span class="Comment">"</span>
<span class="Comment">" This makes the Home key jump to the first non-whitespace character, or to</span>
<span class="Comment">" the start of the line. Inspired by Cream. (cream.sf.net)</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_map_home <span class="Statement">(</span>mode<span class="Statement">)</span>
<span class="Statement">let</span> l:oldcol <span class="Statement">=</span> <span class="Identifier">col</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">if</span> l:oldcol <span class="Statement">==</span> <span class="Constant">1</span>
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endif</span>
<span class="Statement">normal</span> ^
<span class="Statement">let</span> l:newcol <span class="Statement">=</span> <span class="Identifier">col</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">if</span> l:oldcol <span class="Statement">!=</span> l:newcol
<span class="Statement">return</span> l:newcol
<span class="Statement">endif</span>
<span class="Statement">normal</span> 0
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endfunction</span>
<span class="Comment">" This function nicely wraps a selection within quotes, curly braces, square</span>
<span class="Comment">" brackets, angle brackets, tags and more.</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_wrapper <span class="Statement">(</span>str<span class="Statement">)</span> range
<span class="Statement">let</span> l:allowed_strs <span class="Statement">=</span> {
<span class="Special"> \</span> <span class="Constant">'"'</span> : [<span class="Constant">'"'</span>, <span class="Constant">'"'</span>],
<span class="Special"> \</span> <span class="Constant">"'"</span> : [<span class="Constant">"'"</span>, <span class="Constant">"'"</span>],
<span class="Special"> \</span> <span class="Constant">'{'</span> : [<span class="Constant">'{'</span>, <span class="Constant">'}'</span>],
<span class="Special"> \</span> <span class="Constant">'('</span> : [<span class="Constant">'('</span>, <span class="Constant">')'</span>],
<span class="Special"> \</span> <span class="Constant">'['</span> : [<span class="Constant">'['</span>, <span class="Constant">']'</span>],
<span class="Special"> \</span> <span class="Constant">'`'</span> : [<span class="Constant">'`'</span>, <span class="Constant">'`'</span>],
<span class="Special"> \</span> <span class="Constant">'<'</span> : [<span class="Constant">'<'</span>, <span class="Constant">'>'</span>],
<span class="Special"> \</span> <span class="Constant">'t'</span> : [<span class="Constant">''</span>, <span class="Constant">''</span>]
<span class="Special"> \</span> }
<span class="Statement">if</span> <span class="Identifier">has_key</span><span class="Statement">(</span>l:allowed_strs, a:str<span class="Statement">)</span> <span class="Statement">!=</span> <span class="Constant">1</span>
<span class="Statement">echohl</span> <span class="Type">ErrorMsg</span>
<span class="Statement">echo</span> <span class="Constant">'Unknown wrapper: "'</span> . a:str . <span class="Constant">'".'</span>
<span class="Statement">echohl</span> <span class="Type">None</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Comment"> " Wrap the selection with a tag.</span>
<span class="Statement">if</span> a:str <span class="Statement">==</span> <span class="Constant">'t'</span>
<span class="Statement">let</span> l:tname <span class="Statement">=</span> <span class="Identifier">inputdialog</span><span class="Statement">(</span><span class="Constant">'Tag name: '</span><span class="Statement">)</span>
<span class="Statement">if</span> <span class="Identifier">strlen</span><span class="Statement">(</span>l:tname<span class="Statement">)</span> <span class="Statement"><</span> <span class="Constant">1</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> l:allowed_strs[a:str][<span class="Constant">0</span>] <span class="Statement">=</span> <span class="Constant">'<'</span> <span class="Statement">.</span> l:tname <span class="Statement">.</span> <span class="Constant">'>'</span>
<span class="Statement">let</span> l:allowed_strs[a:str][<span class="Constant">1</span>] <span class="Statement">=</span> <span class="Constant">'</'</span> <span class="Statement">.</span> l:tname <span class="Statement">.</span> <span class="Constant">'>'</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> l:prefix <span class="Statement">=</span> l:allowed_strs[a:str][<span class="Constant">0</span>]
<span class="Statement">let</span> l:suffix <span class="Statement">=</span> l:allowed_strs[a:str][<span class="Constant">1</span>]
<span class="Statement">let</span> l:lenp <span class="Statement">=</span> <span class="Identifier">strlen</span><span class="Statement">(</span>l:prefix<span class="Statement">)</span>
<span class="Statement">let</span> l:old_x <span class="Statement">=</span> @<span class="Statement">x</span>
<span class="Statement">let</span> l:old_reg <span class="Statement">=</span> <span class="Special">@"</span>
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:prefix
<span class="Statement">normal</span> `<"xP
<span class="Statement">let</span> l:line1 <span class="Statement">=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">normal</span> `>
<span class="Statement">let</span> l:line2 <span class="Statement">=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">if</span> l:line1 <span class="Statement">==</span> l:line2
<span class="Statement">exe</span> <span class="Constant">'normal '</span> <span class="Statement">.</span> lenp <span class="Statement">.</span> <span class="Constant">'l'</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:suffix
<span class="Statement">normal</span> "xp
<span class="Comment"> " If we are wrapping the selection with a tag, lets put the cursor exactly</span>
<span class="Comment"> " where the user can just press 'i' to start writing the attributes.</span>
<span class="Statement">if</span> a:str <span class="Statement">==</span> <span class="Constant">'t'</span>
<span class="Statement">normal</span> h%h%
<span class="Statement">endif</span>
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:old_x
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> l:old_reg
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endfunction</span>
<span class="Comment">" Similar to the RD_wrapper function, this one removes the quotes, curly</span>
<span class="Comment">" braces, etc.</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_unwrapper <span class="Statement">()</span> range
<span class="Statement">let</span> l:allowed_strs <span class="Statement">=</span> [<span class="Constant">'"'</span>, <span class="Constant">"'"</span>, <span class="Constant">'{'</span>, <span class="Constant">'}'</span>, <span class="Constant">'('</span>, <span class="Constant">')'</span>, <span class="Constant">'['</span>, <span class="Constant">']'</span>, <span class="Constant">'`'</span>, <span class="Constant">'<'</span>, <span class="Constant">'>'</span>]
<span class="Statement">let</span> l:xml_types <span class="Statement">=</span> [<span class="Constant">'xhtml'</span>, <span class="Constant">'html'</span>, <span class="Constant">'xml'</span>]
<span class="Statement">let</span> l:str <span class="Statement">=</span> <span class="Identifier">strpart</span><span class="Statement">(</span>getline<span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>, col<span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span> <span class="Statement">-</span> <span class="Constant">1</span>, <span class="Constant">1</span><span class="Statement">)</span>
<span class="Statement">if</span> <span class="Identifier">index</span><span class="Statement">(</span>l:allowed_strs, l:str<span class="Statement">)</span> <span class="Statement">==</span> <span class="Statement">-</span><span class="Constant">1</span>
<span class="Comment"> " If this is a known XML/SGML file type, then try to strip the current</span>
<span class="Comment"> " tag</span>
<span class="Statement">if</span> <span class="Identifier">index</span><span class="Statement">(</span>l:xml_types, &filetype<span class="Statement">)</span> <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span>
<span class="Statement">return</span> <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span><span class="Normal">RD_unwrap_tag</span><span class="Statement">()</span>
<span class="Statement">endif</span>
<span class="Statement">echohl</span> <span class="Type">ErrorMsg</span>
<span class="Statement">echo</span> <span class="Constant">'The character under the cursor was not recognized: "'</span> . l:str . <span class="Constant">'"'</span>
<span class="Statement">echohl</span> <span class="Type">None</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> l:old_x <span class="Statement">=</span> @<span class="Statement">x</span>
<span class="Statement">let</span> l:old_reg <span class="Statement">=</span> <span class="Special">@"</span>
<span class="Statement">exe</span> <span class="Constant">'normal vi'</span> <span class="Statement">.</span> l:str <span class="Statement">.</span> <span class="Constant">'v'</span>
<span class="Statement">normal</span> `<X
<span class="Statement">let</span> l:line1 <span class="Statement">=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">normal</span> `>
<span class="Statement">let</span> l:line2 <span class="Statement">=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">if</span><span class="Statement">(</span>l:line1 <span class="Statement">!=</span> l:line2<span class="Statement">)</span>
<span class="Statement">normal</span> l
<span class="Statement">endif</span>
<span class="Statement">normal</span> x
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:old_x
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> l:old_reg
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endfunction</span>
<span class="Comment">" This function makes the selection commented, based on &filetype.</span>
<span class="Comment">" This is something simple, sufficient for my needs.</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_comment <span class="Statement">(</span>mode<span class="Statement">)</span> range
<span class="Statement">let</span> l:known_types <span class="Statement">=</span> {
<span class="Special"> \</span> <span class="Constant">'vim'</span> : [<span class="Constant">1</span>, <span class="Constant">'"'</span>],
<span class="Special"> \</span> <span class="Constant">'sh'</span> : [<span class="Constant">1</span>, <span class="Constant">'#'</span>],
<span class="Special"> \</span> <span class="Constant">'php'</span> : [<span class="Constant">2</span>, <span class="Constant">'/*'</span>, <span class="Constant">'*/'</span>],
<span class="Special"> \</span> <span class="Constant">'css'</span> : [<span class="Constant">2</span>, <span class="Constant">'/*'</span>, <span class="Constant">'*/'</span>],
<span class="Special"> \</span> <span class="Constant">'javascript'</span> : [<span class="Constant">2</span>, <span class="Constant">'/*'</span>, <span class="Constant">'*/'</span>],
<span class="Special"> \</span> <span class="Constant">'html'</span> : [<span class="Constant">2</span>, <span class="Constant">'<</span>!<span class="Statement">--</span><span class="Constant">', '</span><span class="Statement">--></span>'],
<span class="Special"> \</span> <span class="Constant">'xhtml'</span> : [<span class="Constant">2</span>, <span class="Constant">'<</span>!<span class="Statement">--</span><span class="Constant">', '</span><span class="Statement">--></span>'],
<span class="Special"> \</span> <span class="Constant">'xml'</span> : [<span class="Constant">2</span>, <span class="Constant">'<</span>!<span class="Statement">--</span><span class="Constant">', '</span><span class="Statement">--></span>']
<span class="Special"> \</span> }
<span class="Statement">if</span> <span class="Identifier">has_key</span><span class="Statement">(</span>l:known_types, &filetype<span class="Statement">)</span> <span class="Statement">!=</span> <span class="Constant">1</span>
<span class="Statement">echohl</span> <span class="Type">ErrorMsg</span>
<span class="Statement">echo</span> <span class="Constant">'Unknown file type.'</span>
<span class="Statement">echohl</span> <span class="Type">None</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> <span class="Statement">(</span>a:mode <span class="Statement">!=</span> <span class="Constant">'v'</span> <span class="Statement">&&</span> a:mode <span class="Statement">!=</span> <span class="Constant">'n'</span><span class="Statement">)</span>
<span class="Statement">echohl</span> <span class="Type">ErrorMsg</span>
<span class="Statement">echo</span> <span class="Constant">'Unknown mode.'</span>
<span class="Statement">echohl</span> <span class="Type">None</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> l:old_x <span class="Statement">=</span> @<span class="Statement">x</span>
<span class="Statement">let</span> l:old_reg <span class="Statement">=</span> <span class="Special">@"</span>
<span class="Statement">let</span> l:ctype <span class="Statement">=</span> l:known_types[&<span class="Statement">filetype</span>][<span class="Constant">0</span>]
<span class="Statement">let</span> l:cprefix <span class="Statement">=</span> l:known_types[&<span class="Statement">filetype</span>][<span class="Constant">1</span>]
<span class="Statement">let</span> l:lenp <span class="Statement">=</span> <span class="Identifier">strlen</span><span class="Statement">(</span>l:cprefix<span class="Statement">)</span>
<span class="Statement">if</span> l:ctype <span class="Statement">==</span> <span class="Constant">2</span>
<span class="Statement">let</span> l:csuffix <span class="Statement">=</span> l:known_types[&<span class="Statement">filetype</span>][<span class="Constant">2</span>]
<span class="Statement">endif</span>
<span class="Statement">if</span> l:ctype <span class="Statement">==</span> <span class="Constant">1</span>
<span class="Statement">if</span> a:mode <span class="Statement">==</span> <span class="Constant">'v'</span>
<span class="Statement">exe</span> <span class="Constant">"'<,'>s~^~"</span> <span class="Statement">.</span> l:cprefix <span class="Statement">.</span> <span class="Constant">"~e"</span>
<span class="Statement">else</span>
<span class="Statement">exe</span> <span class="Constant">"s~^~"</span> <span class="Statement">.</span> l:cprefix <span class="Statement">.</span> <span class="Constant">"~e"</span>
<span class="Statement">endif</span>
<span class="Statement">elseif</span> l:ctype <span class="Statement">==</span> <span class="Constant">2</span>
<span class="Statement">if</span> a:mode <span class="Statement">==</span> <span class="Constant">'v'</span>
<span class="Statement">normal</span> `<
<span class="Statement">else</span>
<span class="Statement">normal</span> ^
<span class="Statement">endif</span>
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:cprefix
<span class="Statement">normal</span> "xP
<span class="Statement">let</span> l:line1 <span class="Statement">=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">if</span> a:mode <span class="Statement">==</span> <span class="Constant">'v'</span>
<span class="Statement">normal</span> `>
<span class="Statement">else</span>
<span class="Statement">normal</span> $
<span class="Statement">endif</span>
<span class="Statement">let</span> l:line2 <span class="Statement">=</span> <span class="Identifier">line</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span>
<span class="Statement">if</span> <span class="Statement">(</span>l:line1 <span class="Statement">==</span> l:line2 <span class="Statement">&&</span> a:mode <span class="Statement">==</span> <span class="Constant">'v'</span><span class="Statement">)</span>
<span class="Statement">exe</span> <span class="Constant">'normal '</span> <span class="Statement">.</span> l:lenp <span class="Statement">.</span> <span class="Constant">'l'</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:csuffix
<span class="Statement">normal</span> "xp
<span class="Statement">endif</span>
<span class="Statement">let</span> @<span class="Statement">x</span> <span class="Statement">=</span> l:old_x
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> l:old_reg
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endfunction</span>
<span class="Comment">" This function uncomments the text within the selection.</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_uncomment <span class="Statement">(</span>mode<span class="Statement">)</span> range
<span class="Statement">let</span> l:known_types <span class="Statement">=</span> {
<span class="Special"> \</span> <span class="Constant">'vim'</span> : {
<span class="Special"> \</span> <span class="Constant">'1l'</span> : <span class="Constant">'"'</span>
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'sh'</span> : {
<span class="Special"> \</span> <span class="Constant">'1l'</span> : <span class="Constant">'#'</span>
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'php'</span> : {
<span class="Special"> \</span> <span class="Constant">'1l'</span> : <span class="Constant">'//\|#'</span>,
<span class="Special"> \</span> <span class="Constant">'multi'</span> : [<span class="Constant">'/\*'</span>, <span class="Constant">'\*/'</span>]
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'css'</span> : {
<span class="Special"> \</span> <span class="Constant">'multi'</span> : [<span class="Constant">'/\*'</span>, <span class="Constant">'\*/'</span>]
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'javascript'</span> : {
<span class="Special"> \</span> <span class="Constant">'1l'</span> : <span class="Constant">'//'</span>,
<span class="Special"> \</span> <span class="Constant">'multi'</span> : [<span class="Constant">'/\*'</span>, <span class="Constant">'\*/'</span>]
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'html'</span> : {
<span class="Special"> \</span> <span class="Constant">'multi'</span> : [<span class="Constant">'<</span>!<span class="Statement">--</span><span class="Constant">', '</span><span class="Statement">--></span>']
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'xhtml'</span> : {
<span class="Special"> \</span> <span class="Constant">'multi'</span> : [<span class="Constant">'<</span>!<span class="Statement">--</span><span class="Constant">', '</span><span class="Statement">--></span>']
<span class="Special"> \</span> },
<span class="Special"> \</span> <span class="Constant">'xml'</span> : {
<span class="Special"> \</span> <span class="Constant">'multi'</span> : [<span class="Constant">'<</span>!<span class="Statement">--</span><span class="Constant">', '</span><span class="Statement">--></span>']
<span class="Special"> \</span> }
<span class="Special"> \</span> }
<span class="Statement">if</span> <span class="Identifier">has_key</span><span class="Statement">(</span>l:known_types, &filetype<span class="Statement">)</span> <span class="Statement">!=</span> <span class="Constant">1</span>
<span class="Statement">echohl</span> <span class="Type">ErrorMsg</span>
<span class="Statement">echo</span> <span class="Constant">'Unknown file type.'</span>
<span class="Statement">echohl</span> <span class="Type">None</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> <span class="Statement">(</span>a:mode <span class="Statement">!=</span> <span class="Constant">'v'</span> <span class="Statement">&&</span> a:mode <span class="Statement">!=</span> <span class="Constant">'n'</span><span class="Statement">)</span>
<span class="Statement">echohl</span> <span class="Type">ErrorMsg</span>
<span class="Statement">echo</span> <span class="Constant">'Unknown mode.'</span>
<span class="Statement">echohl</span> <span class="Type">None</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> l:ctype <span class="Statement">=</span> l:known_types[&<span class="Statement">filetype</span>]
<span class="Statement">if</span> <span class="Identifier">has_key</span><span class="Statement">(</span>l:ctype, <span class="Constant">'1l'</span><span class="Statement">)</span> <span class="Statement">==</span> <span class="Constant">1</span>
<span class="Statement">if</span> a:mode <span class="Statement">==</span> <span class="Constant">'v'</span>
<span class="Statement">exe</span> <span class="Constant">"'<,'>s~^</span><span class="Constant">\\</span><span class="Constant">(</span><span class="Constant">\\</span><span class="Constant">s*</span><span class="Constant">\\</span><span class="Constant">)"</span> <span class="Statement">.</span> l:ctype[<span class="Constant">'1l'</span>] <span class="Statement">.</span> <span class="Constant">"~</span><span class="Constant">\\</span><span class="Constant">1~e"</span>
<span class="Statement">else</span>
<span class="Statement">exe</span> <span class="Constant">"s~^</span><span class="Constant">\\</span><span class="Constant">(</span><span class="Constant">\\</span><span class="Constant">s*</span><span class="Constant">\\</span><span class="Constant">)"</span> <span class="Statement">.</span> l:ctype[<span class="Constant">'1l'</span>] <span class="Statement">.</span> <span class="Constant">"~</span><span class="Constant">\\</span><span class="Constant">1~e"</span>
<span class="Statement">endif</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> <span class="Identifier">has_key</span><span class="Statement">(</span>l:ctype, <span class="Constant">'multi'</span><span class="Statement">)</span> <span class="Statement">==</span> <span class="Constant">1</span>
<span class="Statement">if</span> a:mode <span class="Statement">==</span> <span class="Constant">'v'</span>
<span class="Statement">exe</span> <span class="Constant">"'<s~"</span> <span class="Statement">.</span> l:ctype[<span class="Constant">'multi'</span>][0] <span class="Statement">.</span> <span class="Constant">"~~e"</span>
<span class="Statement">exe</span> <span class="Constant">"'>s~"</span> <span class="Statement">.</span> l:ctype[<span class="Constant">'multi'</span>][1] <span class="Statement">.</span> <span class="Constant">"~~e"</span>
<span class="Statement">else</span>
<span class="Statement">exe</span> <span class="Constant">"s~"</span> <span class="Statement">.</span> l:ctype[<span class="Constant">'multi'</span>][0] <span class="Statement">.</span> <span class="Constant">"~~e"</span>
<span class="Statement">exe</span> <span class="Constant">"s~"</span> <span class="Statement">.</span> l:ctype[<span class="Constant">'multi'</span>][1] <span class="Statement">.</span> <span class="Constant">"~~e"</span>
<span class="Statement">endif</span>
<span class="Statement">endif</span>
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endfunction</span>
<span class="Comment">" This function nicely generates the copyright string I want, in a comment</span>
<span class="Comment">" suited for the current filetype</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_copyright <span class="Statement">()</span>
<span class="Statement">exe</span> <span class="Constant">"normal i Âc "</span> <span class="Statement">.</span> strftime<span class="Statement">(</span><span class="Constant">"%Y"</span><span class="Statement">)</span> <span class="Statement">.</span> <span class="Constant">" ROBO Design\r<a href="http://www.robodesign.ro">http://www.robodesign.ro</a> "</span>
<span class="Statement">normal</span> Vk_c$
<span class="Statement">endfunction</span>
<span class="Comment">" This function makes some changes which enhance the editing of XML/SGML</span>
<span class="Comment">" documents.</span>
<span class="Comment">" Inspired by <a href="http://www.pinkjuice.com/howto/vimxml/setup.xml">http://www.pinkjuice.com/howto/vimxml/setup.xml</a></span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_XML_edit<span class="Statement">()</span>
<span class="Statement">setlocal</span> <span class="PreProc">matchpairs</span>+=<<span class="Statement">:</span>>
<span class="Statement">inoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> <span class="Special"><</span><span class="Special">buffer</span><span class="Special">></span> > ><span class="Special"><</span><span class="Special">Esc</span><span class="Special">></span>:call <span class="Special"><</span><span class="Special">SID</span><span class="Special">></span>RD_autocomplete_tag()<span class="Special"><</span><span class="Special">Cr</span><span class="Special">></span>
<span class="Statement">inoremap</span> <span class="Special"><</span><span class="Special">buffer</span><span class="Special">></span> <span class="Special"><</span><span class="Special">LocalLeader</span><span class="Special">></span>> >
<span class="Statement">endfunction</span>
<span class="Comment">" This function automatically adds the close tag as you type, in (X)HTML and</span>
<span class="Comment">" XML</span>
<span class="Statement">function</span> <span class="Special">s:</span>RD_autocomplete_tag<span class="Statement">(</span> <span class="Statement">)</span>
<span class="Comment"> " Save registers</span>
<span class="Statement">let</span> l:old_reg <span class="Statement">=</span> <span class="Special">@"</span>
<span class="Statement">let</span> l:tmp <span class="Statement">=</span> <span class="Identifier">strpart</span> <span class="Statement">(</span>getline <span class="Statement">(</span><span class="Constant">"."</span><span class="Statement">)</span>, col <span class="Statement">(</span><span class="Constant">"."</span><span class="Statement">)</span> <span class="Statement">-</span> <span class="Constant">2</span>, <span class="Constant">2</span><span class="Statement">)</span>
<span class="Statement">if</span> <span class="Statement">l</span>:tmp <span class="Statement">==</span> <span class="Constant">'/>'</span>
<span class="Statement">silent</span>! <span class="Statement">normal</span> l
<span class="Statement">if</span> <span class="Identifier">col</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span> <span class="Statement">==</span> <span class="Statement">(</span>col<span class="Statement">(</span><span class="Constant">'$'</span><span class="Statement">)-</span><span class="Constant">1</span><span class="Statement">)</span>
<span class="Statement">startinsert</span>!
<span class="Statement">else</span>
<span class="Statement">startinsert</span>
<span class="Statement">endif</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> <span class="Statement">(</span>!exists<span class="Statement">(</span><span class="Constant">"g:xml_no_auto_nesting"</span><span class="Statement">)</span> <span class="Statement">&&</span> l:tmp <span class="Statement">==</span> <span class="Constant">'>>'</span><span class="Statement">)</span>
<span class="Statement">let</span> l:multiline <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">normal</span> X
<span class="Statement">else</span>
<span class="Statement">let</span> l:multiline <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">if</span> <span class="Statement">(</span>&filetype <span class="Statement">==</span> <span class="Constant">'html'</span> <span class="Statement">||</span> &filetype <span class="Statement">==</span> <span class="Constant">'xhtml'</span><span class="Statement">)</span>
<span class="Statement">let</span> l:html_mode <span class="Statement">=</span> <span class="Constant">1</span>
<span class="Statement">let</span> l:html_tags <span class="Statement">=</span> [<span class="Constant">'area'</span>, <span class="Constant">'base'</span>, <span class="Constant">'br'</span>, <span class="Constant">'frame'</span>, <span class="Constant">'hr'</span>, <span class="Constant">'img'</span>, <span class="Constant">'input'</span>, <span class="Constant">'link'</span>, <span class="Constant">'meta'</span>, <span class="Constant">'param'</span>]
<span class="Statement">else</span>
<span class="Statement">let</span> l:html_mode <span class="Statement">=</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">normal</span> y%%
<span class="Statement">let</span> l:<span class="Statement">ltag</span> <span class="Statement">=</span> <span class="Special">@"</span>
<span class="Statement">let</span> l:space <span class="Statement">=</span> <span class="Identifier">stridx</span><span class="Statement">(</span>l:ltag, <span class="Constant">' '</span><span class="Statement">)</span>
<span class="Statement">let</span> l:lbracket <span class="Statement">=</span> <span class="Identifier">stridx</span><span class="Statement">(</span>l:ltag, <span class="Constant">'<'</span>, <span class="Constant">1</span><span class="Statement">)</span>
<span class="Statement">let</span> l:rbracket <span class="Statement">=</span> <span class="Identifier">stridx</span><span class="Statement">(</span>l:ltag, <span class="Constant">'>'</span><span class="Statement">)</span>
<span class="Comment"> " Check if this is a malformed tag</span>
<span class="Statement">if</span> <span class="Statement">(</span>l:lbracket <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span> <span class="Statement">||</span> l:rbracket <span class="Statement">!=</span> <span class="Statement">(</span> strlen<span class="Statement">(</span>l:ltag<span class="Statement">)-</span><span class="Constant">1</span> <span class="Statement">)</span> <span class="Statement">||</span> <span class="Statement">(</span>l:space <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span> <span class="Statement">&&</span> l:space <span class="Statement"><</span> <span class="Constant">2</span><span class="Statement">)</span> <span class="Statement">)</span>
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> <span class="Statement">l</span>:old_reg
<span class="Statement">startinsert</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Comment"> " Get the tag name</span>
<span class="Statement">if</span> <span class="Statement">l</span>:space <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span>
<span class="Statement">let</span> l:tname <span class="Statement">=</span> <span class="Identifier">strpart</span><span class="Statement">(</span>l:ltag, <span class="Constant">1</span>, l:space<span class="Statement">-</span><span class="Constant">1</span><span class="Statement">)</span>
<span class="Statement">else</span>
<span class="Statement">let</span> l:tname <span class="Statement">=</span> <span class="Identifier">strpart</span><span class="Statement">(</span>l:ltag, <span class="Constant">1</span>, strlen<span class="Statement">(</span>l:ltag<span class="Statement">)-</span><span class="Constant">2</span><span class="Statement">)</span>
<span class="Statement">endif</span>
<span class="Comment"> " Tag name too short ... or it's the end tag.</span>
<span class="Comment"> " Also check if this is <!...> or <?...>.</span>
<span class="Comment"> " Additionally, don't autocomplete some known HTML tags</span>
<span class="Statement">if</span> <span class="Statement">(</span> strlen<span class="Statement">(</span>l:tname<span class="Statement">)</span> <span class="Statement"><</span> <span class="Constant">1</span> <span class="Statement">||</span> stridx<span class="Statement">(</span>l:tname, <span class="Constant">'/'</span><span class="Statement">)</span> <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span> <span class="Statement">||</span> stridx<span class="Statement">(</span>l:tname, <span class="Constant">'!'</span><span class="Statement">)</span> <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span> <span class="Statement">||</span> stridx<span class="Statement">(</span>l:tname, <span class="Constant">'?'</span><span class="Statement">)</span> <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span> <span class="Statement">||</span> <span class="Statement">(</span>l:html_mode <span class="Statement">==</span> <span class="Constant">1</span> <span class="Statement">&&</span> index<span class="Statement">(</span>l:html_tags, l:tname<span class="Statement">)</span> <span class="Statement">!=</span> <span class="Statement">-</span><span class="Constant">1</span><span class="Statement">)</span> <span class="Statement">)</span>
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> <span class="Statement">l</span>:old_reg
<span class="Statement">silent</span>! <span class="Statement">normal</span> l
<span class="Statement">if</span> <span class="Identifier">col</span><span class="Statement">(</span><span class="Constant">'.'</span><span class="Statement">)</span> <span class="Statement">==</span> <span class="Statement">(</span>col<span class="Statement">(</span><span class="Constant">'$'</span><span class="Statement">)-</span><span class="Constant">1</span><span class="Statement">)</span>
<span class="Statement">startinsert</span>!
<span class="Statement">else</span>
<span class="Statement">startinsert</span>
<span class="Statement">endif</span>
<span class="Statement">return</span> <span class="Constant">0</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> <span class="Statement">l</span>:tname
<span class="Comment"> " Add the ending tag if multiline!=1</span>
<span class="Statement">if</span> <span class="Statement">l</span>:multiline <span class="Statement">==</span> <span class="Constant">1</span>
<span class="Statement">exe</span> <span class="Constant">"silent! normal! ax\<Esc>r\nOx\<Esc>x"</span>
<span class="Statement">startinsert</span>!
<span class="Statement">else</span>
<span class="Statement">exe</span> <span class="Constant">"normal! a</\<Esc>pa>\<Esc>%"</span>
<span class="Statement">startinsert</span>
<span class="Statement">endif</span>
<span class="Statement">let</span> <span class="Special">@"</span> <span class="Statement">=</span> <span class="Statement">l</span>:old_reg
<span class="Statement">return</span> <span class="Constant">1</span>
<span class="Statement">endfunction</span>
<span class="Comment">" The end</span>
<span class="Comment">" =======</span>
<span class="Comment">" Enable .exrc files (.vimrc files per-directory)</span>
<span class="Statement">set</span> <span class="PreProc">exrc</span>
<span class="Comment">" Disable any unsafe commands in .exrc files</span>
<span class="Statement">set</span> <span class="PreProc">secure</span>
<span class="Comment">" vim:set spell spl=en ts=4 sw=4 sts=4 sta noet fenc=utf-8 ff=unix:</span>
<span class="Comment">" save with F2 </span>
<span class="Statement">nmap</span> <span class="Special"><</span><span class="Special">F2</span><span class="Special">></span> :w<span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">set</span> <span class="PreProc">wildmenu</span>
<span class="Statement">set</span> <span class="PreProc">wcm</span>=<span class="Special"><</span><span class="Special">Tab</span><span class="Special">></span>
<span class="Statement">menu</span> <span class="PreProc">Encoding.latin1</span> :<span class="Statement">e</span> ++enc=latin1<CR>
<span class="Statement">menu</span> <span class="PreProc">Encoding.koi8-r</span> :<span class="Statement">e</span> ++enc=koi8-<span class="Statement">r</span><CR>
<span class="Statement">menu</span> <span class="PreProc">Encoding.windows-1251</span> :<span class="Statement">e</span> ++enc=cp1251<CR>
<span class="Statement">menu</span> <span class="PreProc">Encoding.windows-1252</span> :<span class="Statement">e</span> ++enc=cp1252<CR>
<span class="Statement">menu</span> <span class="PreProc">Encoding.ibm-866</span> :<span class="Statement">e</span> ++enc=ibm866<CR>
<span class="Statement">menu</span> <span class="PreProc">Encoding.utf-8</span> :<span class="Statement">e</span> ++enc=utf-8 <CR>
<span class="Statement">map</span> <span class="Special"><</span><span class="Special">F8</span><span class="Special">></span> :emenu Encoding.<span class="Special"><</span><span class="Special">TAB</span><span class="Special">></span>
<span class="Comment">" Search for selected text, forwards or backwards.</span>
<span class="Comment">" replaced vsearch.vim plugin</span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> * :<span class="Special"><</span><span class="Special">C-U</span><span class="Special">></span>
<span class="Special"> \</span>let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
<span class="Special"> \</span>gvy/<span class="Special"><</span><span class="Special">C-R</span><span class="Special">><</span><span class="Special">C-R</span><span class="Special">></span>=<span class="Identifier">substitute</span>(
<span class="Special"> \</span><span class="Identifier">escape</span><span class="Statement">(</span><span class="Special">@"</span>, <span class="Constant">'/\.*$^~['</span><span class="Statement">)</span>, <span class="Constant">'\_s\+'</span>, <span class="Constant">'\\_s\\+'</span>, <span class="Constant">'g'</span><span class="Error">)</span><span class="Special"><</span><span class="Special">CR</span><span class="Special">><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Special"> \</span>gV:<span class="Statement">call</span> <span class="Identifier">setreg</span><span class="Statement">(</span><span class="Constant">'"'</span>, old_reg, old_regtype<span class="Statement">)</span><span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">vnoremap</span> <span class="Special"><</span><span class="Special">silent</span><span class="Special">></span> # :<span class="Special"><</span><span class="Special">C-U</span><span class="Special">></span>
<span class="Special"> \</span>let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
<span class="Special"> \</span>gvy?<span class="Special"><</span><span class="Special">C-R</span><span class="Special">><</span><span class="Special">C-R</span><span class="Special">></span>=<span class="Identifier">substitute</span>(
<span class="Special"> \</span><span class="Identifier">escape</span><span class="Statement">(</span><span class="Special">@"</span>, <span class="Constant">'?\.*$^~['</span><span class="Statement">)</span>, <span class="Constant">'\_s\+'</span>, <span class="Constant">'\\_s\\+'</span>, <span class="Constant">'g'</span><span class="Error">)</span><span class="Special"><</span><span class="Special">CR</span><span class="Special">><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Special"> \</span>gV:<span class="Statement">call</span> <span class="Identifier">setreg</span><span class="Statement">(</span><span class="Constant">'"'</span>, old_reg, old_regtype<span class="Statement">)</span><span class="Special"><</span><span class="Special">CR</span><span class="Special">></span>
<span class="Statement">vmap</span> <span class="Special"><</span><span class="Special">C-k</span><span class="Special">></span> *
<span class="Comment">" save both with :w and :W</span>
:<span class="Statement">command</span> <span class="Statement">-</span><span class="PreProc">nargs</span><span class="Statement">=?</span> <span class="Statement">-</span><span class="PreProc">range</span><span class="Statement">=</span><span class="Type">%</span> <span class="Statement">-</span><span class="PreProc">complete</span><span class="Statement">=</span><span class="Type">file</span> <span class="Statement">-</span><span class="PreProc">bang</span> W <span class="Special"><</span><span class="Special">line1</span><span class="Special">></span>,<span class="Special"><</span><span class="Special">line2</span><span class="Special">></span><span class="Statement">w</span><span class="Special"><</span><span class="Special">bang</span><span class="Special">></span> <span class="Special"><</span><span class="Special">args</span><span class="Special">></span>
:<span class="Statement">command</span> <span class="Statement">-</span><span class="PreProc">bang</span> Q <span class="Special"><</span><span class="Special">line1</span><span class="Special">></span>,<span class="Special"><</span><span class="Special">line2</span><span class="Special">></span><span class="Statement">q</span><span class="Special"><</span><span class="Special">bang</span><span class="Special">></span> <span class="Special"><</span><span class="Special">args</span><span class="Special">></span>
:<span class="Statement">command</span> WQ <span class="Statement">wq</span>
:<span class="Statement">command</span> Wq <span class="Statement">wq</span>
<span class="Statement">iab</span> MoN January February March April May June July August September October November December
<span class="Statement">iab</span> MoO Jan Feb Mar Apr May Jun Jul Aug Sep Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
<span class="Statement">iab</span> NuM 12345678901234567890123456789012345678901234567890123456789012345678901234567890
<span class="Statement">iab</span> RuL ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-34356151574222412362009-10-28T12:34:00.013+01:002010-04-29T18:08:09.432+02:00Entering special characters in LinuxOccasionally there is a need to enter special characters which are not on the keyboard, for example, an M-dash, ("—") or and N-dash ("–").<br />
<br />
This solution works on all recent xorg versions, so it should work on all modern Linux distros.<br />
<br />
<ol>
<li>Press Ctr+Shift+U.</li>
<li>Release U while still holding Ctr+Shift.</li>
<li>Enter the four digit character code, e.g. 2013 for an m-dash, or 2014 for a n-dash.</li>
</ol>
<div>
Some special characters:<br />
<div style="color: darkred; font-family: calibri,arial,helvetica; font-size: 9pt;">
<table border="1" style="border-collapse: collapse; margin: 5px 5px 5px 5px; padding: 5px 5px 5px 5px;">
<tbody>
<tr><th><span style="font-style: italic;">Unicode character code (HEX)</span> <br />
</th><th><span style="font-style: italic;">Special Character</span> <br />
</th></tr>
<tr>
</tr>
<tr><td><span style="border-collapse: separate;">00a9</span><br />
</td><td><span style="border-collapse: separate; font-family: monospace;"><b>©</b></span><br />
</td></tr>
<tr><td><span style="font-family: 'Times New Roman';border-collapse: separate;">2013</span>
</td><td><span style="border-collapse: separate;">– (en-dash)</span><br />
</td></tr>
<tr><td><span style="border-collapse: separate;">2014</span><br />
</td><td>— (em-dash)<br />
</td></tr>
<tr><td>20ac<br />
</td><td>€<br />
</td></tr>
<tr><td>20a4<br />
</td><td>₤<br />
</td></tr>
</tbody></table>
</div>
<br />
You can find a list of some character codes here:<br />
</div>
<div>
<a href="http://www.hermit.org/Linux/ComposeKeys.html">http://www.hermit.org/Linux/ComposeKeys.html</a><br />
<br />
A more extended character code list is here:<br />
<a href="http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references">http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references</a> <br/> or <br/>
<a href="http://unicode.org/charts/index.html">http://unicode.org/charts/index.html</a><br />
</div>
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-23360434138190319142009-09-30T20:34:00.008+02:002010-04-07T14:45:20.739+02:00WordReference.com Bookmarklet<p>
This bookmarklet opens a Word Reference popup window for the currently selected word on a page. Works in the same way as the Google Translate bookmarklets.</p>
<p>
<dd> <a href="javascript:(function(){var%20w=window,d=document,o=w.open('http://www.wordreference.com/es/en/translation.asp?spen='+((window.getSelection&&window.getSelection().toString()||window.frames.length&&window.frames[0].getSelection().toString())||(document.getSelection&&document.getSelection().toString()||document.frames.length&&document.frames[0].getSelection.toString())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)),'','left='+((w.screenX||w.screenLeft)+10)+',top='+((w.screenY||w.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1,scrollBars=1');w.setTimeout(function(){o.focus()},300)})();">Word Reference Es-En</a> </dd>
<address>05/2010: updated to work on pages with frames</address>
<p> </p>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-4084621230170774806.post-52856839292046814422009-09-21T16:34:00.034+02:002010-11-11T21:25:30.071+01:00Google Translate Popup Bookmarklet<p>
These bookmarklets open a Google Translate popup for the currently selected text on a page. The only difference from the standard google translate bookmarklets is that these bookmarklets will also work on pages that use frames or inline frames, for example in gmail.</p>
<p>
To use a bookmarklet drag the link (<span style="font-style:italic;">do not click</span>) to you Favorites toolbar. Once you have added the icon to your toolbar, select some text (e.g. in Spanish) on a page and then click the bookmark icon.</p>
<p>
To modify the language pair, edit the language code in the bookmarklet source from <b><tt>es|en</tt></b> to whatever language you want. Use <tt>auto</tt> to autodetect the source language.
<dd> <a href="javascript:(function(){var w=window,d=document,o=w.open('http://translate.google.com/translate_t#en|es|'+((window.getSelection&&window.getSelection().toString()||window.frames.length&&window.frames[0].getSelection().toString())||(document.getSelection&&document.getSelection().toString()||document.frames.length&&document.frames[0].getSelection.toString())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)),'','left='+((w.screenX||w.screenLeft)+10)+',top='+((w.screenY||w.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1');w.setTimeout(function(){o.focus()},300)})();">Google Translate En-Es</a> </dd>
<dd> <a href="javascript:(function(){var w=window,d=document,o=w.open('http://translate.google.com/translate_t#es|en|'+((window.getSelection&&window.getSelection().toString()||window.frames.length&&window.frames[0].getSelection().toString())||(document.getSelection&&document.getSelection().toString()||document.frames.length&&document.frames[0].getSelection.toString())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)),'','left='+((w.screenX||w.screenLeft)+10)+',top='+((w.screenY||w.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1');w.setTimeout(function(){o.focus()},300)})();">Google Translate Es-En</a> </dd>
<dd> <a href="javascript:(function(){var w=window,d=document,o=w.open('http://translate.google.com/translate_t#auto|en|'+((window.getSelection&&window.getSelection().toString()||window.frames.length&&window.frames[0].getSelection().toString())||(document.getSelection&&document.getSelection().toString()||document.frames.length&&document.frames[0].getSelection.toString())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)),'','left='+((w.screenX||w.screenLeft)+10)+',top='+((w.screenY||w.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1');w.setTimeout(function(){o.focus()},300)})();">Google Translate Any-En (autodetect)</a> </dd>
<dd> <a href="javascript:(function(){var w=window,d=document,o=w.open('http://translate.google.com/translate_t#auto|es|'+((window.getSelection&&window.getSelection().toString()||window.frames.length&&window.frames[0].getSelection().toString())||(document.getSelection&&document.getSelection().toString()||document.frames.length&&document.frames[0].getSelection.toString())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)),'','left='+((w.screenX||w.screenLeft)+10)+',top='+((w.screenY||w.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1');w.setTimeout(function(){o.focus()},300)})();">Google Translate Any-Es (autodetect)</a> </dd>
<p>
To see how this works select some text on this page and then click the link <a href="javascript:(function(){var w=window,d=document,o=w.open('http://translate.google.com/translate_t#en|es|'+((window.getSelection&&window.getSelection().toString()||window.frames.length&&window.frames[0].getSelection().toString())||(document.getSelection&&document.getSelection().toString()||document.frames.length&&document.frames[0].getSelection.toString())||(document.selection&&document.selection.createRange&&document.selection.createRange().text)),'','left='+((w.screenX||w.screenLeft)+10)+',top='+((w.screenY||w.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1');w.setTimeout(function(){o.focus()},300)})();">Google Translate En-Es</a> </dd> to get a Spanish translation in a popup window.
</p>
To get more google translate buttons for more languages go to the google's own page at <a href="http://translate.google.com/translate_buttons">http://translate.google.com/translate_buttons</a>.
<p> </p>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-4084621230170774806.post-77870218302476314882009-05-28T22:00:00.023+02:002011-03-09T21:16:37.429+01:00M4AtoMP3: Convert M4A files to MP3<p>I wanted to convert some M4A files to MP3, and found some examples of how to do it: <a href="http://pragmattica.wordpress.com/2008/01/17/convert-itunes-m4a-files-to-mp3-on-linux/">Convert iTunes M4A files to MP3 on Linux</a>, and especially <a href="http://gimpel.gi.funpic.de/wiki/index.php?title=Howto:convert_aac/mp4_to_wav/mp3/ogg_on_Linux">Howto:convert aac/mp4 to wav/mp3/ogg on Linux</a>.</p>
<p>The second link was close to what I wanted, and the script almost worked, but I wanted something even simpler, so here is the simplest script that worked for me. It converts all .M4A files in the current directory to MP3.</p>
<pre>
#!/bin/bash
# Usage: run without arguments from the folder containing M4A files
# Uses faad and lame (sudo apt-get install faad lame)
shopt -s nocaseglob
for file in *.m4a ; do
if [ -e "${file%%.*}.mp3" ]; then
echo "file ${file%%.*}.mp3 already exists! skipping!" ;
continue
fi
faad -o - $file | lame - "${file%%.*}.mp3" ;
done
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-86807910239238131842009-05-22T10:20:00.054+02:002016-04-25T10:35:58.968+02:00Screen Quick Howto<h5>What is screen?</h5>man page: "Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells)." Here is a screen <a href="http://www.rackaid.com/resources/linux-tutorials/general-tutorials/using-screen/" target="new">screen tutorial</a>. <br />
<h5>Screen quickref</h5>Most of the data below has been taken from the <a href="http://aperiodic.net/screen/quick_reference" target="new">screen quick reference</a>. <br />
<div style="color: darkred; font-family: calibri,arial,helvetica; font-size: 9pt;"><table border="1" style="border-collapse: collapse; border: 1ps solid gray; margin: 5px 5px 5px 5px; padding: 5px 5px 5px 5px;"><tbody>
<tr><td colspan="2" style="color: gray;"><b><i>Shortcuts</i></b></td> </tr>
<tr><th><span style="font-style: italic; text-align: left;">Action</span> <br />
</th><th><span style="font-style: italic; text-align: left;">Shortcut</span> <br />
</th></tr>
<tr></tr>
<tr><td>Change to last-visited active window<br />
</td><td>C-a C-a (commonly used to flip-flop between two windows)<br />
</td></tr>
<tr><td>Change to window by number<br />
</td><td>C-a <number> (only for windows 0 to 9)<br />
</td></tr>
<tr><td>Change to window by number or name'<br />
</td><td>C-a <number or title><br />
</td></tr>
<tr><td>Change to next window in list<br />
</td><td>C-a n or C-a <space><br />
</td></tr>
<tr><td>Change to previous window in list<br />
</td><td>C-a p<br />
</td></tr>
<tr><td>Split current screen session into two regions<br />
</td><td>C-A S<br />
</td></tr>
<tr><td>Remove current region<br />
</td><td>C-a X<br />
</td></tr>
<tr><td>remove all regions but the current one<br />
</td><td>C-a Q<br />
</td></tr>
<tr><td>Switch between displayed regions<br />
</td><td>C-a TAB<br />
</td></tr>
<tr><td>Create new session in split region<br />
</td><td>C-a c<br />
</td></tr>
<tr><td>Return to a single region<br />
</td><td>C-a Q<br />
</td></tr>
<tr><td>Detach <br />
</td><td>C-a d<br />
</td></tr>
<tr><td>Detach and logout (quick exit)<br />
</td><td>C-a D D<br />
</td></tr>
<tr><td>Enter copy mode<br />
</td><td>C-a [ (also used for viewing scrollback buffer) and with C-]<br />
</td></tr>
<tr><td>Paste<br />
</td><td>C-a ]<br />
</td></tr>
<tr><td>Rename current window<br />
</td><td>C-a A (note the uppercase 'A'!)<br />
</td></tr>
<tr><td colspan="2"><b><i style="color: gray;">Console commands</i></b></td> </tr>
<tr><td>Detach attached session<br />
</td><td>screen -D<br />
</td></tr>
<tr><td>Detach attached screen session (possibly running in another terminal) and reattach in current terminal<br />
</td><td>screen -Dr<br />
</td></tr>
<tr><td>Detach specific screen session (when more than one sessions are running)</td><td>screen -D 14493.ssh2others</td></tr>
<tr><td>Move cursor to beginning of line</td><td>Control+a (release Control) + a</td></tr>
<tr><td></td> <td></td></tr>
</tbody></table></div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-87066995010961213652009-04-27T17:22:00.024+02:002010-12-15T18:28:22.938+01:00Python UnicodeDecodeError: 'ascii' codec can't decode byte<p>You use Python 2.x and keep getting the notorious Python <code>UnicodeDecodeError, 'ascii' codec can't decode byte</code>?</p>
<p>Here is a <span style="font-style:italic;">hack</span> which is especially useful in cases when you need to work with HTML.</p>
<p>Instead of</p>
<pre>
print nasty_unicode_string
</pre>
<p>use this:</p>
<pre>
print nasty_unicode_string.encode('us-ascii','xmlcharrefreplace')
</pre>
<p>This code will replace all the exotic non-ascii characters with their HTML-escaped ascii representation, i.e. <tt>Sueño</tt> will be replaced with <tt>Sue&#241;o</tt>, which will display properly when rendered in HTML.</p>
<p>Bingo! No more UnicodeDecodeError!</p>
<em>Warning:</em> In most cases the UnicodeDecodeError is caused by programmer's lack of understanding of how Unicode is handled by Python, and is a signal that the entire approach to string handling should be revised. The hack above <b>is not</b> the proper way to fix it, and should be used with care.
On the other hand, this approach is totally justified in cases when you need to print something to the console and are not sure whether the terminal supports utf8 or other unicode-friendly encoding.Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-4084621230170774806.post-11615124380448421552009-02-18T21:08:00.030+01:002010-04-27T18:22:42.028+02:00Mouse emulation with keyboard<p>In many applications you can get around by pressing the Tab and arrow keys and then pressing Enter or Space when the widget you need is under focus. Remembering common shortcuts is always useful, but there are things which are impossible to do without a mouse, for example, clicking a button in a Flash applet.</p>
<p>As you probably know, you can emulate most mouse actions with the keyboard. To enable this feature in Kubuntu 8.10/9.04 </p>
<ol>
<li>Open <i>System Settings</i>:
<pre>
systemsettings
</pre>
or click the <i>Application Launcher > System > System Settings</i>.
<li>Under the <i>General</i> tab select <i>Keyboard & Mouse</i>. To do it with the keyboard use Tab/Shift+Tab and the arrow keys. Window tabs (or pages) can be switched with Control+Tab or with Alt + underlined letter.
<li>Next, select <i>Mouse</i> on the left, and then Alt+N for <i>Mouse <u>N</u>avigation</i>.
<li>Select the option <i>Move pointer with keyboard (using the num pad)</i>. (Alt+M)
<li>Click <i>Apply</i> (Alt+Y).
</ol>
<p>Note: If this looks complicated, there is actually a single shortcut for enabling mouse keys: Alt+Shift+NumLock.</p>
<p>Now you should be able to move the mouse pointer with number pad keys: 1, 2, 3, 4, 6, 7, 8, 9. If it does not work try pressing the NumLock key.</p>
<p>The table below shows the correspondence between mouse actions and keys on the number pad.</p>
<div style="font-size:9pt;font-family:calibri,arial,helvetica;color:darkred">
<table border="1" style="border-collapse: collapse;padding:5px 5px 5px 5px;margin:5px 5px 5px 5px;">
<tr><th> <span style="font-style:italic;">mouse action</span> </th><th> <span style="font-style:italic;">keyboard equivalent</span> </th><tr>
<tr><td> Left-click </td><td> 5 </td></tr>
<tr><td> Left double-click </td><td> '+' </td></tr>
<tr><td> Right click </td><td> '-' on the numpad and then 5 </td></tr>
<tr><td> Middle-click </td><td> '*' on the numpad and then 5 </td></tr>
<tr><td> Switch back to left click </td><td> '/' on the numpad and then 5 </td></tr>
</table>
</div>
<p>The '-', '*', and '/' on the number pad select which 'mouse button' will be clicked when you press '5', therefore after you do a right-click with '-' and 5, you will have to switch back to left-clicking mode by pressing '/', otherwise you next click will still be on the 'right mouse button'.</p>
<h4>Selecting text, dragging with keyboard</h4>
<p>For some strange reason I did not find any official documentation on how to emulate mouse dragging with keyboard.</p>
<p>I have not checked other distros but on Kubuntu 8.10 you can do it by consecutively pressing the Del and '0' (Ins) on the number pad. This enables drag mode and you can now use 1-4/6-9 keys to drag whatever you clicked on. To finish dragging click 5.</p>
<p>I suppose that these features have to do with the xorg server, and are not [K]Ununtu-specific, so this probably should work with most distros.</p>
<p>Most of these keys also work on a MS Windows machine with one exception: drag-mode on Windows is enabled with the '0' key alone.</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-63643059710524627732009-01-03T01:39:00.009+01:002009-09-03T14:05:15.335+02:00Bookmarklet: GeoWhois for current page<p>Javascript bookmarklets: GeoIP, whois</p>
<p>
Drag the link below (do <span style="font-style:italic;">not</span> click) to you Favorites toolbar:</p>
<dd>
<p><a href="javascript:location.href='http://www.geoiptool.com/en/?IP='+location.href.split('//')[1].split('/')[0]">GeoIP</a> </p>
</dd>
<dd>
<p><a href="javascript:(function(){var%20a=window,b=document,d=a.open('http://centralops.net/co/DomainDossier.aspx?dom_whois=true&net_whois=true&addr='+((b.selection!=undefined&&b.selection.createRange().text||b.getSelection())||document.location.href),'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=420px,width=650px,resizable=1,alwaysRaised=1,scrollbars=1');a.setTimeout(function(){d.focus()},300)})();">Whois (Firefox only, also works with selected text)</a> </p>
</dd>
<dd>
<p><a href="javascript:(function(){var%20a=window,b=document,d=a.open('http://centralops.net/co/DomainDossier.aspx?dom_whois=true&net_whois=true&addr='+((b.selection!=undefined&&b.selection.createRange().text||b.getSelection())||document.location.href),'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=420px,width=650px,resizable=1,alwaysRaised=1,scrollbars=1');a.setTimeout(function(){d.focus()},300)})();">Whois (Firefox and IE)</a> </p>
</dd>
<p>Opera: Shift+drag the link to the toolbar</p>
<p>Firefox: Drag to Bookmarks panel (enable with View > Toolbars > Bookmarks)</p>
<p><b>Usage:</b> To look up the domain of a page you are browsing simply click the bookmarklet in the Favorites toolbar. To lookup a URL that appears on a page, select the URL and click the bookmarklet in your Bookmarks bar (this only works with the Firefox-only bookmarklet).</p>
<p>In Firefox 3.x it is possible to drag the bookmarklet to any position you want, even to the menu bar. To do this, first add the bookmarklet to the Bookmarks bar, then right-click on the Firefox toolbar, select <i>Customize</i>, and drag the bookmarklet to wherever you want. After that you can disable the Bookmarks toolbar.</p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4084621230170774806.post-36782685607619445142008-12-30T14:19:00.017+01:002011-03-09T21:15:21.892+01:00Creating file timestamps<p>Here is a script that uses <a href="http://www.mplayerhq.hu" target="_new">mplayer</a> to dump a stream to a file with an appended timestamp. </p>
<h5>Linux bash</h5>
<dd>
<pre>
mplayer -dumpstream -dumpfile flamenco_radio_`date +%b-%d-%Y_%H_%M_%S`.mp3 \
http://canalflamenco.rtva.stream.flumotion.com/rtva/canalflamenco.mp3.m3u
</pre>
</dd>
<h5>Windows batch</h5>
<dd>
<pre>
mplayer.exe -dumpstream -dumpfile bbc1_%date:~10,4%-%date:~4,2%-%date:~7,2%_%time:~0,2%-%time:~3,2%-%time:~6,2% \
mms://livewmstream-ws.bbc.co.uk.edgestreams.net/reflector:47971
</pre>
</dd>
<p>ps. The Windows version is not very reliable since it will only work with the US locale. This script assumes the date format is set like this: <code>Mon 04/31/2012</code>. For locales that use different date formats the slicing will have to be adjusted accordingly.</p>Unknownnoreply@blogger.com0