Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Wednesday, November 6, 2013

Basic Recovery

The following commands are useful for recovering an editing session.

Retrieve a list of recoverable files.
vim -r
Recover a specific file.
vim -r .file.extension.swp (from swap files listed above)
Recover an unnamed (unsaved) file.
vim -r .swp
Or from inside of Vim:
:recover .file.extension.swp

Monday, November 2, 2009

Knowing Where You Are

If you want to know the current filename, what percentage of the current file you're editing, whether the current file has been modified, or the character or line position, all you have to do is hit ctrl-g from normal mode inside Vim.

Thursday, September 11, 2008

Jump to Percent of File

Sometimes you don't know the exact line you want to go to in a file, but you can infer an approximate percentage. I was browsing a 200 meg logfile with Vim yesterday and guestimated the area of interest was about 10% back from where I was at (85%). You can easily jump to a desired percentage in a file by typing in the percentage followed by a percent symbol.

Jump to 50% of the file:

50%

Easy to remember to say the least ;-)

Tuesday, August 19, 2008

Reverse A File

Sometimes it's useful to cat a file in reverse order. An example would be doing things like parsing log files and such. I had a need to do this today and stumbled onto the cleverly named tac program. Usage is simple:

tac somefile.txt > somefile_reversed.txt

Thursday, August 14, 2008

Recovering a Deleted Ext3 File

A co-worker accidentally deleted a .java file containing several hours worth of work. The file resided in his home directory, which conveniently had it's own dedicated partition. We managed to recover the file using the following (hackish) technique.

1) Take the machine to single user mode.

telinit 1

2) Unmount the partition containing the data.

umount /dev/sda2

3) Identify a semi-unique pattern located in the file.

His code contained a string that matched "Dictionary()".

4) Determine how much context around the string you need.

We chose 250 lines before and after the match.

5) Run grep on the partition.

grep --binary-files=text -250 "Dictionary()" < /dev/sda2 > /tmp/dump.bin

If you're lucky, the data will be somewhere in the file. We extracted the Java code using Vim, wrote a new file to /root, and went back to runlevel 2. One warning, remember not to write the recovered file to /tmp as it's often cleared when changing runlevels and rebooting.

Monday, July 21, 2008

Untar a Single File

From time to time, I've had the need to extract a single file from a tar archive. How to do so is pretty poorly documented in the man page, but it's actually easy to do. If you have a tarball named "my.tar" with a group of files in it, and you want to extract "hello.txt", you would do the following:

tar -x hello.txt -vf my.tar

Before I extract anything from a tarball, I always preview it's contents to avoid "tarbombs" (tarballs that extract directly into the CWD).

tar -tvf my.tar

Or if it's gzipped:

tar tzvf my.tar.gz

Lastly, newer versions of Vim can browse a tarball out of the box and open any file contained within.

vim my.tar (displays a file browser)

Monday, June 9, 2008

Source A File

If you make changes to your ~/.bashrc or ~/.bash_profile or ~/.zshrc, etc... you can load the changes without logging in and out again by issuing a source command:

source ~/.bashrc # load changes, execute commands, etc...

alternatively, you can use the shortcut dot syntax:

. ~/.bashrc

You can do the same in Vim to read a file of ex-commands such as your ~/.vimrc.

:source ~/.vimrc

Thursday, May 29, 2008

Two Copies, Same Buffer

Oftentimes I like to have two copies of the same window open in a horizontal split, so I can look at different pieces of code in the same file. Vim has a handy shortcut to quickly split the current file in a new window. Just hit ctrl-w s. A side bonus is that you don't have to save the file first unlike using :new filename.

Thanks to Nate for the tip!

Friday, May 9, 2008

Demystifying File Attributes

Unix based operating systems store three attributes regarding file access and modification. At least one of these attributes is a common source of confusion, and while it's generally a harmless mistake to misinterpret it's meaning, knowing what it actually means could save you some headache down the road.

atime (access time):

This is the last time a file was accessed or read. It's also updated when a file is executed.

view with: ls -lu

mtime (modification time):

This is updated whenever a file is modified.

view with: ls -l

ctime (change time):

This attribute is regularly confused for "creation time", which is totally wrong. It's actually not possible to determine a file's creation time on a Unix based OS. Rather, ctime actually means "change time". This flag is set whenever a files owner or permissions change or when the contents change.

view with: ls -lc

Thursday, May 8, 2008

Reverse Lines in a File

You can quickly reverse all lines in a file using the following ex command.

:g/^/m0

This is particularly useful for files that are in chronological order such as logs or email mbox files. Thanks to Skyler for submitting the tip.

Friday, April 11, 2008

Quickly Truncate a File

Quick shell tip:

If you want to truncate a file to zero bytes from the command-line, here's an easy way.

user@host [~]$ > filename

I used to use:

user@host [~]$ cp /dev/null filename

But the first approach is less typing.

Wednesday, February 13, 2008

Last File Location

Some Linux distributions set this up by default and some don't. I like Vim to open a file I'm working on to the last location I was in before exiting. This can be accomplished by adding the following to your .vimrc.

au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif

Friday, November 16, 2007

GOTO File Under Cursor (not harmful)

If you're editing a file and have your cursor positioned on a file you would like to open with Vim, press `gf' from normal mode, and it will open in the same window. Ctrl-w f will open it in a new window, and Ctrl-w gf will open it in a new tab.