Hide

What's this?

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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

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.

Hide

Stay in the loop…

Follow the Tweets.

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using vim from sorted by
Terminal - Commands using vim - 77 results
X='pattern'; vim +/"$X" `egrep -lr "$X" *`
vim -r 2>&1 | grep '\.sw.' -A 5 | grep 'still running' -B 5
2010-04-17 19:43:35
User: rkulla
Functions: grep vim
3

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].

vim -p file1 file2 [...]
vim -o file1 file2...
2010-04-13 22:09:47
User: rkulla
Functions: vim
Tags: vim split
7

-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.

vim $(find . ! -path \*.svn\* -type f -iname \*foo\*)
2010-04-11 23:32:41
User: rkulla
Functions: find vim
Tags: vim find
2

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)

svn diff | vim -
vim suite.js -c '%s!/\*\_.\{-}\*/!!g'
alias busy='my_file=$(find /usr/include -type f | sort -R | head -n 1); my_len=$(wc -l $my_file | awk "{print $1}"); let "r = $RANDOM % $my_len" 2>/dev/null; vim +$r $my_file'
2010-03-09 21:48:41
User: busybee
Functions: alias awk find head sort vim wc
22

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.

Create aliases for common vim minibuffer/cmd typos
2009-12-28 20:58:29
User: tmsh
Functions: vim
Tags: vim
1

Because entering ':' requires that you press shift, sometimes common command-line / mini-buffer commands will be capitalized by accident.

vim -n -es -c 'g/# CommandParse/+2,/^\s\+esac/-1 d p | % d | put p | %<' -c 'g/^\([-+]\+[^)]\+\))/,/^\(\s\+[^- \t#]\|^$\)/-1 p' -c 'q!' $0
2009-12-19 08:32:00
User: syladmin
Functions: vim
0

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'

extract_cmdl_options()

{

# 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

}

example code:http://snipplr.com/view/25059/display-embeded-comments-for-every-opt-usefull-for-auto-documenting-your-script/

vim -
2009-11-10 22:25:36
User: tmsh
Functions: vim
-3

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 '|'!

Jk.

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

ras_a.h

ras_a.cpp

ras_b.h

ras_b.cpp

to:

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.

vim -e -s -c 'g/start_pattern/+1,/stop_pattern/-1 p' -cq file.txt
2009-08-26 10:22:27
User: syladmin
Functions: vim
Tags: vim block
0

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.

details :

-e ex mode

-s silent

-c 'ex command' : global + start and end pattern + offset print (p)

-cq : quit

$ vim ... :help 42
2009-08-17 11:37:02
User: alvinx
Functions: vim
11

inside vim try:

:help 42

to get the meaning of life, the universe and everything !

vim $(grep test *)
2009-07-15 10:15:04
User: goatboy
Functions: grep test vim
Tags: vim grep
4

I often use "vim -p" to open in tabs rather than buffers.

vim -c new myfile
2009-07-03 17:54:43
Functions: vim
1

: 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.

svn diff <file> | vim -R -
2009-06-13 22:00:49
User: caiosba
Functions: diff vim
Tags: svn vim diff color
15

Simple way to achieve a colored SVN diff

vim `which <scriptname>`
2009-05-08 17:21:47
User: bunedoggle
Functions: vim
Tags: vim which script
1

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).

vim -x <FILENAME>
2009-05-05 23:24:17
User: denzuko
Functions: vim
64

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

vim ~/.purple/pounces.xml
2009-04-28 19:11:39
Functions: vim
Tags: vim pidgin
0

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!

function v { if [ -z $1 ]; then vim; else vim *$1*; fi }
2009-04-11 23:06:43
User: kFiddle
Functions: vim
Tags: vim vi function
0

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.

vim $( ls -t | head -n1 )
vim -p file1 file2 ...
2009-02-27 16:43:59
User: haivu
Functions: vim
Tags: vim
8

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'

tail -1000 /some/file | vim -
2009-02-25 11:43:27
User: root
Functions: tail vim
17

The hyphen tells vim to open from STDOUT - saves having to create temporary files.

for i in `recode -l | cut -d" " -f 1`; do echo $i": ?" | recode utf-8..$i -s -p >> temp; done; vim temp
2009-02-20 00:14:33
User: saerdnaer
Functions: cut echo vim
0

If you have some textfile with an unknown encoding you can use this list to find out

vim scp://username@host//path/to/somefile