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
cmdfu(){ local t=~/cmdfu;echo -e "\n# $1 {{{1">>$t;curl -s "commandlinefu.com/commands/matching/$1/`echo -n $1|base64`/plaintext"|sed '1,2d;s/^#.*/& {{{2/g'>$t;vim -u /dev/null -c "set ft=sh fdm=marker fdl=1 noswf" -M $t;rm $t; }
2012-02-21 05:43:16
User: AskApache
Functions: echo rm sed vim
6

Here is the full function (got trunctated), which is much better and works for multiple queries.

function cmdfu () {

local t=~/cmdfu;

until [[ -z $1 ]]; do

echo -e "\n# $1 {{{1" >> $t;

curl -s "commandlinefu.com/commands/matching/$1/`echo -n $1|base64`/plaintext" | sed '1,2d;s/^#.*/& {{{2/g' | tee -a $t > $t.c;

sed -i "s/^# $1 {/# $1 - `grep -c '^#' $t.c` {/" $t;

shift;

done;

vim -u /dev/null -c "set ft=sh fdm=marker fdl=1 noswf" -M $t;

rm $t $t.c

}

Searches commandlinefu for single/multiple queries and displays syntax-highlighted, folded, and numbered results in vim.

cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$(echo "$@" | sed 's/ /-/g')/$(echo -n $@ | base64)/plaintext" --silent | vim -R - }
2012-02-10 16:26:47
Functions: vim
4

Search for one/many words on commandlinefu, results in vim for easy copy, manipulation. The -R flag is for readonly mode...you can still write to a file, but vim won't prompt for save on quit.

What I'd really like is a way to do this from within vim in a new tab. Something like

:Tex path/to/file

but

:cmdfu search terms
ls | vim +'set bt=nowrite' -
cmdfu(){ local TCF="/var/tmp/cmdfu"; echo " Searching..."; curl "http://www.commandlinefu.com/commands/matching/$(echo "$@" | sed 's/ /-/g')/$(echo -n $@ | base64)/plaintext" --silent > "$TCF"; vim -c "set filetype=sh" -RM "$TCF"; rm "$TCF"; }
vim -c'highlight Comment ctermfg=white' my.conf
2011-10-21 00:53:03
User: kev
Functions: vim
Tags: vim
-1

you should choose proper color to make comments invisible.

alias busy='rnd_file=$(find /usr/include -type f -size +5k | sort -R | head -n 1) && vim +$((RANDOM%$(wc -l $rnd_file | cut -f1 -d" "))) $rnd_file'
2011-10-16 00:05:59
User: frntn
Functions: alias cut find head sort vim wc
0

Enhancement for the 'busy' command originally posted by busybee : less chars, no escape issue, and most important it exclude small files ( opening a 5 lines file isn't that persuasive I think ;) )

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.

ack-open () { local x="$(ack -l $* | xargs)"; if [[ -n $x ]]; then eval vim -c "/$*[-1] $x"; else echo "No files found"; fi }
2011-10-04 08:56:18
User: iynaix
Functions: echo eval vim
1

Takes the same arguments that ack does. E.g. ack-open -i "searchterm" opens all files below the current directory containing the search term. The search term is also highlighted within vim if you have hlsearch set. Works on zsh, unsure if it works on bash.

Note: ubuntu users have to change ack to ack-grep unless you already have it aliased (as I do)

sedit() { cp "$*"{,.bk}; which $EDITOR > /dev/null && $EDITOR "$*" || vim "$*"; }
2011-08-16 18:28:22
User: kaedenn
Functions: cp vim which
-6

Some people put spaces in filenames. Others have an $EDITOR environment variable set. This defaults to vim, but you can use whatever you wish: emacs, nano, ed, butterflies, etc.

vim -p `grep -r PATTERN TARGET_DIR | cut -f1 -d: | sort | uniq | xargs echo -n`
vim -p `ls *.java *.xml *.txt *.bnd 2>/dev/null`
cvs up -r1.23 -p main.cpp | vim -
vim `find . -iname '*.php'`
2011-05-11 01:19:28
User: wsams
Functions: vim
0

In this case, we'll be editing every PHP file from the current location down the tree.

You can show all the files in the vim buffer with :buffers which outputs something like,

:buffers

1 %a "./config/config.php" line 1

2 "./lib/ws-php-library.php" line 0

3 "./lib/css.php" line 0

4 "./lib/mysqldb.class.php" line 0

5 "./lib/config.class.php" line 0

6 "./lib/actions.php" line 0

Press ENTER or type command to continue

If you'd like to edit ./lib/mysqldb.class.php for example, enter :b4 anytime you're editing a file. You can switch back and forth.

find $DIR -name *.php -exec vim -u NONE -c 'set ft=php' -c 'set shiftwidth=4' -c 'set tabstop=4' -c 'set noexpandtab!' -c 'set noet' -c 'retab!' -c 'bufdo! "execute normal gg=G"' -c wq {} \;
2011-04-08 11:42:45
User: ruslan
Functions: find vim
-2

The sample command searches for PHP files replacing tabs with spaces.

-u NONE # don't use vimrc

Instead of

retab!

one may pass

retab! 4

for instance.

Look at this http://susepaste.org/69028693 also

vim -u NONE yourfile
2011-03-29 01:31:10
User: fossilet
Functions: vim
Tags: vim
2

This will skip all initializations. Especially useful when your ~/.vimrc has something wrong.

shebang() { if i=$(which $1); then printf '#!%s\n\n' $i > $2 && vim + $2 && chmod 755 $2; else echo "'which' could not find $1, is it in your \$PATH?"; fi; }
2011-03-09 14:47:32
User: bartonski
Functions: chmod echo printf vim which
3

The first argument is the interpreter for your script, the second argument is the name of the script to create.

vimcmd() { $1 > $2 && vim $2; }
2010-12-16 21:51:35
User: bartonski
Functions: vim
1

This is one of those 'nothing' shell functions ...which I use all the time.

If the command contains spaces, it must be quoted, e.g.

vimcmd 'svn diff' /tmp/svndiff.out

If I want to keep the output of the command that I'm running, I use vimcmd. If I don't need to keep the output, I use this:

vim <( ... my command ... )
( apache2ctl -t && service apache2 restart || (l=$(apache2ctl -t 2>&1|head -n1|sed 's/.*line\s\([0-9]*\).*/\1/'); vim +$l $(locate apache2.conf | head -n1)))
2010-11-26 18:12:08
User: cicatriz
Functions: head locate sed vim
3

Checks the apache configuration syntax, if is OK then restart the service otherwise opens the configuration file with VIM on the line where the configuration fails.

vim ... :nmap <F5> :w^M:!python %<CR>
2010-09-03 18:44:21
User: duxklr
Functions: vim
Tags: vim python
1

This will save and execute your python script every time your press the F5 function key.

It can also be added to your .vimrc:

autocmd BufRead *.py nmap :w^M:!python %

NOTE: the ^M is not just caret-M, it can be created by type: ctrl-v ctrl-m

vim txt.gz
2010-09-03 01:54:05
User: Aparicio
Functions: vim
-6

Works at lest with .gz .bz2 .tar.gz .tar.bz2

vi2() {for i in $@; do [ -f "$i" ] && [ ! -w "$i" ] && sudo vim $@ && return; done; vim $@}
2010-08-15 10:00:14
User: pipeliner
Functions: sudo vim
Tags: vim sudo
-3

Like the http://www.commandlinefu.com/commands/view/6327/open-file-with-sudo-when-there-is-no-write-permission, but works (in zsh; my commandlinefu is not strong enough to understand why bash don't like it) with vim options, like -O, and many input files.

There could be other mistakes.

if test -w $1; then vim $1; else sudo vim $1; fi
2010-08-14 13:28:32
User: srepmub
Functions: sudo test vim
Tags: vim sudo tee
-2

this avoids several VIM warnings, which I seem too stupid to disable: warning, readonly! and: file and buffer have changed, reload?!

vim !$
2010-07-06 01:39:22
User: emacs
Functions: vim
0

the second command 'vim !$' will open test.txt to edit

vim --version | grep -P '^(\+|\-)' | sed 's/\s/\n/g' | grep -Pv '^ ?$'
2010-07-02 02:57:19
User: evaryont
Functions: grep sed vim
Tags: vim sed grep
2

The above output is for a custom compiled version of Vim on Arch Linux.

Just a quick shell one liner, and presents a list of all the enabled and disabled (those prefixed with a '-') features.

find ./ -type f -mtime -1 -name .*.sw[po] -print | sed -r 's/^(.+)\/\.(\S+)\.sw[op]$/\1\/\2/' | xargs vim -r
2010-06-16 13:15:10
User: nodnarb
Functions: find sed vim xargs
-1

this is great if you loose you ssh connection (with out a screen session) or are working on a laptop with a bad battery, or just a power outage.

Modifications: you may not need the -print; the mtime is last modified time in days

vix(){ vim +'w | set ar | silent exe "!chmod +x %" | redraw!' $@; }
2010-05-27 21:12:48
User: dooblem
Functions: ar set vim
Tags: vim vi chmod
2
vix /tmp/script.sh

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