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 vi from sorted by
Terminal - Commands using vi - 19 results
iptables-save > iptables.current; vi iptables.current; iptables-restore iptables.current; service iptables save
2013-12-04 18:41:48
User: bigc00p
0

These series of commands allows you all at once to make a backup of your current config, edit that config, then saves it as the running config and makes it persistent. I would advise knowing what your doing to the config before running this because if you mess up say the port 22 portion, you may get knocked off the system. ;) Don't say I didn't warn ya!

vi scp://username@host:12345//path/to/somefile
2012-11-23 04:06:23
Functions: vi
2

If you are running sshd on different port use above this command to edit/view remote file with vi/vim.

In above example I am using port 12345.

vi scp://username@host//path/to/somefile
2012-11-23 03:10:50
User: huazhihao
Functions: vi
15

notice the double slash

vi +'<start>,<end>d' +wq <filename>
vi +START,ENDd +wq sample.txt
2012-01-26 20:47:35
User: titan2x
Functions: vi
Tags: bash vi
1

Deletes lines from START to END, inclusive. For example +4,10d will delete line 4, 5, ..., 10. Just like the vi command :4,10d does it.

vi +{<end>..<start>}d +wq <filename>
2012-01-26 20:36:04
User: javidjamae
Functions: vi
Tags: bash vi
1

Deletes lines to of a file. You must put the end line first in the range for the curly brace expansion, otherwise it will not work properly.

vi +<lineNumber>d +wq <filename>
vi +4 /etc/mtab
2011-09-15 19:18:00
User: totti
Functions: vi
Tags: edit text line goto
1

This is not printing, real editing using the text editor.

alias viaco='task="$(basename "$(pwd)")"; if [ -f "$task.c" ]; then vi -c "set mouse=n" -c "set autoread" -c "vsplit $task.out" -c "split $task.in" -c "wincmd l" -c "wincmd H" $task.c; fi'
2011-05-13 13:35:02
User: yolila
Functions: alias vi
-1

1. Get name of task by task=$(basename "$(pwd)")

2. Check whether "$task.c" exists as a file

3. open "$task.c", "$task.in", "task.out" in vim with such layout.

-------------------------------

|            | $task.in    |

|            |                |

|$task.c  |-----------------|

|            | $task.out  |

|            |                |

-------------------------------

eh () { history -a ; vi ~/.bash_history ; history -r ; }
2011-03-23 18:00:20
Functions: vi
5

eh stands for Edit History

.

Frequently, I'll mistype a command, and then step back through my history and correct the command. As a result, both the correct and incorrect commands are in my history file. I wanted a simple way to remove the incorrect command so I don't run it by mistake.

.

When running this function, first the ~/bash_history file is updated, then you edit the file in vi, and then the saved history file is loaded back into memory for current usage.

.

while in vi, remember that `Shift-G` sends you to the bottom of the file, and `dd` removes a line.

.

this command is different than bash built-in `fc` because it does not run the command after editing.

vi +/pattern [file]
2010-04-24 22:15:12
User: punkwalrus
Functions: vi
Tags: vim edit vi
25

Open up vi or vim at the first instance of a pattern in [file]. Useful if you know where you want to be, like "PermitRootLogin" in sshd_config. Also, vi +10 [file] will open up a file at line 10. VERY useful when you get "error at line 10" type of output.

vi ~/.crontab && crontab ~/.crontab
2009-08-16 08:09:15
User: micha
Functions: crontab vi
-6

this lets you edit a crontab file (which may be used as a backup as well) and insert this into your crontab. this way is especially handy, when multiple users are working on one account (e.g. root)

vi `!!`
2009-07-15 15:20:58
User: libdave
Functions: vi
0

say you've just found all the config files with this command

find . -name '*.config'

and you need to edit them all

vi `!!`

will re-execute the command and present them to vi in the argument list

don't use if the list is really long as it may overflow the command buffer

vi `!!`
2009-04-06 17:58:13
User: vimes656
Functions: vi
4

That will open vi with the four README files in different viewports.

Specially handy when you find there is only one file matching your pattern and you don't want to specify the full path.

sudo vi /etc/fstab; Go//smb-share/gino /mnt/place smbfs defaults,username=gino,password=pass 0 0<esc>:wq; mount //smb-share/gino
2009-04-02 16:04:35
User: GinoMan2440
Functions: mount sudo vi
4

the middle command between the ; and ; is the vi commands that insert that line into the last line of the file, the esc with the carets is literally hitting the escape key, you have to have the smbfs package installed to do it, I use it to access my iTunes music on my mac from my linux PC's with amarok so I can play the music anywhere in the house. among other things, it allows you to access the files on that share from your computer anytime you're on that network.

vi ~/journal/$(date +%F)
2009-03-18 23:31:13
User: ar
Functions: date vi
1

prerequisite:

mkdir ~/journal
vifind() { vi `find . -name "$1"` }
vi -R filename
2009-03-04 06:56:40
User: haivu
Functions: vi
Tags: vim
-3

The -R flag prevents you from making changes to a file, useful when you only want to view the file. This command is nicer than the 'more' or 'less' commands because for source codes, vi shows syntax highlight.

find . -type f -exec grep -l pattern {} \; | xargs vi +/pattern