Archive

Posts Tagged ‘paste’

Show the absolute path of a file

March 17, 2011 Leave a comment

When working in a terminal, a common task is to get the absolute path of a file. For instance, you want to open the given file with another (already running) application.

Old method:

Well, to get the absolute path of a file, I was doing the following:

C:\usr\local\lib\python2.6\dist-packages> ls -al
total 32
drwxrwsr-x  4 root staff 4096 2011-03-16 14:41 .
drwxrwsr-x  4 root staff 4096 2010-10-25 21:22 ..
drwx--S--- 10 root staff 4096 2010-09-29 09:30 pyglet
-rw-------  1 root staff 1039 2010-09-29 09:30 pyglet-1.1.4.egg-info
drwx--S---  2 root staff 4096 2011-03-16 14:41 termcolor-1.1.0.egg-info
-rw-r--r--  1 root staff 5044 2011-03-16 14:41 termcolor.py
-rw-------  1 root staff 3672 2011-03-16 14:41 termcolor.pyc
C:\usr\local\lib\python2.6\dist-packages> pwd
/usr/local/lib/python2.6/dist-packages
C:\usr\local\lib\python2.6\dist-packages>

First, find the file (termcolor.py in this example). Second, get the current working directory with the pwd command. Third, copy and paste the current working directory. Finally, copy and paste the filename. At each copy/paste, I had to switch between the terminal and the other application where I needed the absolute path. Too much work…

New method:

C:\usr\local\lib\python2.6\dist-packages> ls -al
total 32
drwxrwsr-x  4 root staff 4096 2011-03-16 14:41 .
drwxrwsr-x  4 root staff 4096 2010-10-25 21:22 ..
drwx--S--- 10 root staff 4096 2010-09-29 09:30 pyglet
-rw-------  1 root staff 1039 2010-09-29 09:30 pyglet-1.1.4.egg-info
drwx--S---  2 root staff 4096 2011-03-16 14:41 termcolor-1.1.0.egg-info
-rw-r--r--  1 root staff 5044 2011-03-16 14:41 termcolor.py
-rw-------  1 root staff 3672 2011-03-16 14:41 termcolor.pyc
C:\usr\local\lib\python2.6\dist-packages> sp termcolor.py
/usr/local/lib/python2.6/dist-packages/termcolor.py
C:\usr\local\lib\python2.6\dist-packages> sp termcolor.py | tocb
C:\usr\local\lib\python2.6\dist-packages>

The script “sp” (see below) takes one argument (a file name) and it prints the concatenation of the current directory and the file name, i.e. the absolute path of the file. Of course, you need to use the script in the directory where the given file is located. If no argument is given, it will print the current working directory.

If your goal is to paste the absolute path to somewhere else, you can copy the output of sp to the clipboard with tocb.

The script

The name of the script stands for “show path”.

#!/usr/bin/env python

# sp.py ("show path")

import os
import os.path
import sys

if len(sys.argv) == 1:
    print os.getcwd()
else:
    print os.path.join( os.getcwd(), sys.argv[1] )

Update (20110402)

The script is pushed to GitHub: https://github.com/jabbalaci/Bash-Utils.

Categories: bash, python Tags: , , , ,

Copy and paste the output of a program

February 24, 2011 Leave a comment

It is a common task to copy the output of a program/script and paste it somewhere else. If the output is short (just some lines), we can select the text easily with the mouse. What about longer outputs?

Old method

Until now I’ve been using the following method:

  • ./produce-long-output >a (redirect the output to a temporary file)
  • gedit a (open the temp file with a text editor)
  • CTRL+A (select the whole text)
  • copy selected text to the clipboard
  • paste text to somewhere else
  • rm a (remove the temp file)

New method

Recently, I discovered some tools (xclip and xsel) that can copy a text to the clipboard. With them, it is unnecessary to use a temporary file.

./produce-long-output | xsel

It’ll copy the text to the “primary” clipboard that you can paste with the middle mouse button. If you prefer pasting with Shift + Insert, then copy texts to the 3rd clipboard (it is simply called “clipboard”).

./produce-long-output | xsel -b

Life has become a little bit less complicated :)

Categories: bash Tags: , , , ,

Copy string to X clipboard in shell

February 24, 2011 6 comments

This entry is based on the post Copy Shell Prompt Output To Linux / UNIX X Clipboard Directly.

Problem

Under Linux, you want to copy a given text to the X clipboard. How to do that?

Three different clipboards

First of all, we should know that under X there are three clipboards :)

  • “primary”: The primary one’s paste operation is usually accessed with the middle mouse button.
  • “secondary”: This one didn’t really work for me…
  • “clipboard”: Its paste operation is accessed with Shift + Insert (or right mouse click and Paste in popup menus).

The xclip command

Reads from standard in, or from one or more files, and makes the data available as an X selection for pasting into X applications. Prints current X selection to standard out.

Installation:

sudo apt-get install xclip

Copy a string to the clipboard. By default, xclip copies to the “primary” clipboard.

echo "this is a test" | xclip
echo "this is a test" | xclip -selection primary
echo "this is a test" | xclip -selection p

The three variations are equivalent.

Copy a string to the “clipboard” (3rd clipboard).

echo "another test" | xclip -selection clipboard
echo "another test" | xclip -selection c

The two variations are equivalent.

Copy the contents of a file to the clipboard.

cat file.txt | xclip

Print the contents of the clipboard to the standard output.

xclip -o

Of course, you can use redirection.

xclip -o > out.txt

The xsel command

Retrieve and set the X selection.

xsel is similar to xclip, but it offers some additional features like append, clear, etc.

Installation:

sudo apt-get install xsel

Copy a string to the primary clipboard (default).

echo "1" | xsel
echo "1" | xsel -p

The two variations are equivalent.

Copy a string to “clipboard” (3rd clipboard).

echo "1" | xsel -b

Copy the contents of a file to the clipboard.

cat file.txt | xsel

Print the contents of the clipboard to the standard output.

xsel -o

Homework

Find a solution to copy a string to the “primary” and “clipboard” clipboards, i.e. on both. Hint: “echo homework | xsel -p -b” and “echo homework | xclip -selection p -selection c” won’t work. Post your solution in the comments.

Categories: bash Tags: , , ,

My vimperator settings

January 6, 2011 1 comment

Update (20110502): This post was updated for Vimperator 3.1. Copy/paste is corrected in this new version, thus the plugin mswin.vimp is not needed anymore. I removed that part from the post.

Vimperator is a Firefox browser extension with strong inspiration from the Vim text editor, with a mind towards faster and more efficient browsing. It has similar key bindings and you could call it a modal web browser, as key bindings differ according to which mode you are in. For example, it has a special Hint mode, where you can follow links easily with the keyboard only. Also most functionality is available as commands, typing :back will go back within the current page history, just like hitting the back button in the toolbar.” (source)

Vimperator is a very useful Firefox add-on. When I reinstall a system, this is one of the first add-ons that I need to install in order to be able to work efficiently :)

Here are my config files:

Useful commands:

To tell the truth, I only use a very small subset of the features offered by Vimperator. Here are my favourites:

  • ESC (hitting the ESC key => enter command mode)
  • j, k (Scroll down and up. To memorize: j looks like a down arrow.)
  • r (reload the page)
  • o URL (open URL in the current tab)
  • t URL (open URL in a focused new tab)
  • T URL (Open URL in an unfocused new tab. This is a custom feature added to my config file.)
  • / SEARCH_TERM (Vim-like, case-insensitive incremental search. Pressing n you can jump to the next the search hit.)
  • d (close the current tab)
  • u (Restore a closed tab. Can be used repeatedly to restore several closed tabs.)
  • :res (Restart the browser. It’ll restore the tab set.)
  • gu (From the current URL, remove the part after the last ‘/’. Example: http://www.mozilla.com/en-US/firefox/central/ becomes http://www.mozilla.com/en-US/firefox/)
  • gU (In the current URL, leave the host name only. Example: http://www.mozilla.com/en-US/firefox/central/ becomes http://www.mozilla.com/)
  • gg (jump to the top of the page)
  • G (jump to the end of the page)
  • y (copy the current URL to the clipboard)
  • ALT+y (Copy page title to the clipboard. This is a custom feature added to my config file.)
  • Shortcuts; I just love them. You have a favourite site, say http://slashdot.org/, and you want to assign an alias to this site, for instance “sd”. Then, you’d like to open this site by using the command “:o sd“. To this end, visit the site (here http://slashdot.org/), and assign a bookamark to it by “:bma -k=sd“. From now on, “sd” is associated to http://slashdot.org/. You can type “sd” in the address bar too.

If you know some cool tips, please share with us in the comments.

Follow

Get every new post delivered to your Inbox.

Join 72 other followers