[vim] Save and use vim sessions

Tags

, , , , , , , ,

Save your vim session while working on a lot of files opened in different tabs or screens by using

:mksession saved_session.vim

in one of the vim windows. Note that it does not have to have “.vim” extension.

After that you can do pretty much anything with your computer (close all windows, reboot, etc.) as long as you do not deleted the file saved above. You can restore the session while you are in vim by using:

:source saved_session.vim

or if you want to start vim with the saved session, use this

$ vim -S saved_session.vim

in the command line.

Application: (Directly copied from here)

The obvious way to use sessions is when working on different projects.
Suppose you store you session files in the directory "~/.vim".  You are
currently working on the "secret" project and have to switch to the "boring"
project:

	:wall
	:mksession! ~/.vim/secret.vim
	:source ~/.vim/boring.vim

This first uses ":wall" to write all modified files.  Then the current session
is saved, using ":mksession!".  This overwrites the previous session.  The
next time you load the secret session you can continue where you were at this
point.  And finally you load the new "boring" session.

Further reading: here and here.

[linux] How to install a package from the source code

Tags

, , , , , , , , , , , , , , , , , ,

Installing a package (AKA software) from source code is often considered to be the secret cult of the worshipers of the Geek God. But, you know, it’s not that difficult. Let’s see how we can do it.

First, some preliminaries. Why should I build it form the source code? Well, source codes are more or less platform independent. If you have a suitable compiler and necessary libraries, you may install it on any platform of your choice. Source code files are available  in ASCII plain text  format (not binaries) — so you can study it and understand what it does and whether/ how it may affect your system. Reading  the description of a software distributed as a binary does not even come close to that — you really don’t know what that piece of  software does until you actually run it. Needless to add, you may include your changes to the open-source code to suit your needs as well in accordance with the relevant license.

Secondly, where can I find the source code? The source code for a open-source package is often distributed in compressed format (i.e., with an extension “tar.gz”, “tgz” or “tar.bz2” — that’s why it’s also called tarball). You may download the tarball from a reliable source, e.g. homepage of the package developer, Free Software Directory, Open Source Software directory, linux.org, Ubuntu archive, Darwin ports, Fink and Open Source Apple website. Last three are specifically intended for OSX running Darwin. Wikipedia also has a nice list of Free and Open Source softwares.

Now, let’s do the installation step-by step. Create a directory (say, ~/packages) where you want to save the packages, navigate to that directory, save the tarball (say, newpack.tar.gz) there and uncompress it:

$ tar zxvf newpack.tar.gz

Replace “zxvf” by “jxvf” if it has a “tar.bz2” extension instead of “tar.gz” or “tgz”.

Then navigate to the uncompressed directory (usually newpack, but be sure of  that first using the ls command) using

$ cd newpack

and read the instruction files (e.g., INSTRUCTIONS, INSTALL, README), if any, for special instructions using your favorite text editor. Otherwise, the normal procedure for installation is the following series of commands: (wait for each one to finish before you issue the next command!)

$ ./configure
$ make
$ sudo make install

You need to enter the superuser password in the last step as it copies the executable in the system’s executable directory (e.g., /bin, /sbin/). That’s it!

However, you may want to clean up the mess (especially the object files) created by the installation process using:

$ make clean

In a spree of cleaning up, however, don’t delete the file named Makefile: you need this if you want to uninstall the package (the above command won’t delete it).

In order to uninstall the package, navigate to the same directory (~/packages/newpack/), and issue the following command:

$ sudo make uninstall

You need to enter the superuser password for this.

Reference: here.

[osx] Eject a volume ‘unwilling’ to be ejected!

Tags

, , , , , , , , , , , , , ,

I sometime have problem finding which persistent process is not letting me eject a volume (CD/DVD/USB drive). The error message is not very helpful at all:

Try quitting applications

OK, but which one(s)? I often have hard time figuring that out using htop/ top or a combination of ps and grep.

But actually this is what I want:

$ lsof | grep -i Volume_Name

where Volume_Name is the name of the volume (or an ‘identifying’ part of the name) I want to eject. This gives me the name(s) and process ID(s) of the run-away process(es) as well as the path to the file(s) in use on the volume, Volume_Name.

Now that I know the names I should first try to save the documents and quit the applications involved in the normal way. However, if a particular application does not oblige, I can always kill it (with the risk of potential data loss!) using its process ID (say, 123):

$ kill -9 123

After that, the ejection of the volume should not be difficult at all.

Needless to say, being a Unix utility, lsof (=“list open files”) may be used on other *nix-based systems as well.

[vim] Simple calculator in vim

Tags

, , , , , ,

Need a simple calculator during editing a file using vim and don’t want to click away from it? Then first add the following to your ~/.vimrc:

imap <silent> <C-C> <C-R>=string(eval(input("Calculate: ")))<CR>

In order to use the calculator  while you are editing some file using vim, hit CTRL-C in the insert mode and then enter what you want to calculate (e.g., 2+3 or 4.5*7.9).

(It) first calls the built-in input() function to request the user to type in their calculation, which input() then returns as a string. That input string is then passed to the built-in eval(), which evaluates it as a Vimscript expression and returns the result. Next, the built-in string() function converts the numeric result back to a string, which the key-mapping’s <C-R>= sequence is then able to insert.

Reference: here.

[cli] Find out the public IP in the command line

Tags

, , , , , , , , , , , ,

You may find out your IP using commands like ifconfig, but if your computer is behind a firewall, that is not the IP that the world sees. Here’s a quick how-to to find out what your external IP is.

First, let’s create an alias: (link)

alias getip="wget -q -O - http://whatismyip.com/automation/n09230945.asp"

Next time when you need the public IP of your computer, just type getip in the command line and hit enter.

The above command gets the IP from http://whatismyip.com/automation/n09230945.asp (just copy and paste this URL to your location bar and hit enter, and see what it does!). The “-q” is to suppress verbose information (quiet mode) and “-O -” causes the output to be written to STDOUT.

You may use dyndns.org too to find out the IP, but in this case you may need some trimming: substitute the text within the code above by the following:


wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1

A few words about various flags above:
The output of the first command (everything before the first pipe) is

<code><html><head><title>Current IP Check</title></head><body>Current IP Address: xxx.xxx.xx.xx</body></html>

(the actual IP address is masked by x).
The “-o /dev/null“ part redirects the STDERR of wget to /dev/null. The “-d :” option in first “cut” tells it to use colon (:) to be used as the delimiter and “-f 2” causes it to print second of the delimited fields. Similarly, the flags of the second “cut” cause it to use “<” as the delimiter for the piped output from the first “cut” and choose the first of the delimited fields.

UPDATE: Another one using curl [link]. You may again change the quoted text within the alias above by the following:


curl --connect-timeout 3 http://www.whatismyip.org/

Design a site like this with WordPress.com
Get started