commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:
Open a file directly with execution permission.
Put the function in your .bashrc
You can also put this in your vimrc:
command XX w | set ar | silent exe "!chmod +x %" | redraw!
and open a new file like this:
vi +XX /tmp/script.sh
Catches .swp, .swo, .swn, etc.
If you have access to lsof, it'll give you more compressed output and show you the associated terminals (e.g., pts/5, which you could then use 'w' to figure out where it's originating from): lsof | grep '\.sw.$'
If you have swp files turned off, you can do something like: ps x | grep '[g,v]im', but it won't tell you about files open in buffers, via :e [file].
Open files in tabs
-o acts like :spit. Use -O (capital o) for side-by-side like :vsplit. Use vim -d or vimdiff if you need a diff(1) comparison.
To split gnu Screen instead of vim, use ^A S for horizontal, ^A | for vertical.
This command searches the current directory, and all of its subdirs, for files that have the string "foo" in their filename (foo.c, two-foo.txt, index-FOO-bar.php, etc), and opens them in Vim. It ignores any hidden .svn directories. Change -iname to -name if you want to do case-sensitive matches.
Files open in buffers by default, so to verify that the correct files were opened, type ":list". You can load all the files in tabs by doing ":tab ball" or use 'vim -p' on the command-line to load files straight to tabs.
If you get permission denied errors, do: vim $(find . ! -path \*.svn\* -type -f iname \*foo\* 2>/dev/null)
To narrow it down to a single file extension, such as .php files, use \*foo\*.php (or '*foo*.php'. Which ever you prefer)
Will colorize your svn diff.
This makes an alias for a command named 'busy'. The 'busy' command opens a random file in /usr/include to a random line with vim. Drop this in your .bash_aliases and make sure that file is initialized in your .bashrc.
Because entering ':' requires that you press shift, sometimes common command-line / mini-buffer commands will be capitalized by accident.
A really fun vim oneliner for auto documenting your option's parsing in your script.
# print the text embeded in the case that parse options from command line.
# the block is matched with the marker 'CommandParse' in comment, until 'esac'
# use vim for parsing:
# 1st grep the case block and copy in register @p + unindent in the buffer of the file itself
# 2nd filter lines which start with --opt or +opt and keep comment on hte following lines until an empty line
# 3rd discard changes in the buffer and quit
vim -n -es -c 'g/# CommandParse/+2,/^\s\+esac/-1 d p | % d | put p | %
-c 'g/^\([-+]\+[^)]\+\))/,/^\(\s\+[^- \t#]\|^$\)/-1 p' \
-c 'q!' $0
I don't know if you've used sqsh before. But it has a handy feature that allows you to switch into vim to complete editing of whatever complicated SQL statement you are trying to run.
But I got to thinking -- why doesn't bash have that? Well, it does. It's called '|'!
Seriously, I'm pretty sure this flow of commands will revolutionize how I administer files. And b/c everything is a file on *nx based distros, well, it's handy.
First, if your ls is aliased to ls --color=auto, then create another alias in your .bashrc:
alias lsp='ls --color=none'
Now, let's say you want to rename all files that begin with the prefix 'ras' to files that begin with a 'raster' prefix.
You could do it with some bash substitution. But who remembers that? I remember vim macros because I can remember to press 'qa' and how to move around in vim. Plus, it's more incremental. You can check things along the way. That is the secret to development and probably the universe. So type something like:
lsp | grep ras
Are those all the files you need to move? If not, modify and re-grep. If so, pipe it to vim.
lsp | grep ras | vim -
Now run your vim macros to modify the first line. Assuming you use 'w' and 'b' to move around, etc., it should work for all lines. Hold down '@@', etc., until your list of files has been modified from
mv ras_a.h raster_a.h
mv ras_a.cpp raster_a.cpp
mv ras_b.h raster_b.h
mv ras_b.h raster_b.cpp
then run :%!bash
then run :q!
then be like, whaaaaa? as you realize your workflow got a little more continuous. maybe. YMMV.
By using vim, you can also filter content on stdout, using vim's extra power, like search pattern offset!
No more awk of course, sorry.
-e ex mode
-c 'ex command' : global + start and end pattern + offset print (p)
-cq : quit
inside vim try:
to get the meaning of life, the universe and everything !
I often use "vim -p" to open in tabs rather than buffers.
: new command allow to split a Vim screen in two separate windows. Each window can handle its own buffer.
Passing the -c new options when Vim start cause to split screen automatically.
Simple way to achieve a colored SVN diff
Often I need to edit a bash or perl script I've written. I know it's in my path but I don't feel like typing the whole path (or I don't remember the path).
While I love gpg and truecrypt there's some times when you just want to edit a file and not worry about keys or having to deal needing extra software on hand. Thus, you can use vim's encrypted file format.
For more info on vim's encrypted files visit: http://www.vim.org/htmldoc/editing.html#encryption
So you keep getting buzzes sounding from pidgin but you can't remember which buddy pounce is causing the beep. Well, cat/edit the ~/.purple/pounces and find out!
Reduce the number of keystrokes it takes to open a file in vim. First of all, you just need to type "v", which is less than half the number of characters (!), and second-of-all, you only need to enter a substring of the file you want to open. For example, if you want to open the file, homework.txt, then type "v hom" to open it. Good tip is to use the lowest unique substring, otherwise you'll open multiple files in different buffers (which is sometimes desirable). Use Ctrl-^ to switch between buffers.
Edit the files, each in a separate tab. use gT and gt to move to the left- and right-tab, respectively. to add another tab while editing, type ':tabe filename'
The hyphen tells vim to open from STDOUT - saves having to create temporary files.
If you have some textfile with an unknown encoding you can use this list to find out