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 tagged vi

Commands tagged vi from sorted by
Terminal - Commands tagged vi - 29 results
:!bash
2012-08-13 17:13:53
User: CLIxBOBxBOM
Tags: bash vim vi
2

Helps when I'm editing a script and want to double check some commands without having to exit out of vi multiple times or having to use another terminal session.

sed -i <start>,<end>d <filename>
perl -i -ne 'print if $. == 3..5' <filename>
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.

:w !diff -u % -
LANG=fr_FR@euro
2011-05-23 08:27:22
User: igorfu
Tags: vi
-1

French uses accents (???...) which may be badly displayed on computers with the wrong default character set. This command may help (sometimes)

:33,61 !sort
2011-05-06 06:10:05
User: greggster
Tags: sort vi ex
6

Sort lines within vi editor. In this example sort lines 33-61 and lines 4-9 asciibetically.

:%s/\s\+$//
set list / set nolist
alias vim="vim -p"
2010-12-30 22:53:33
User: tommed
Functions: alias
Tags: vim vi tab
8

I always add this to my .profile rc so I can do things like: "vim *.c" and the files are opened in tabs.

au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif
2010-11-15 01:26:17
User: lifenotfound
Tags: vim vi chmod +x
19

Add this to .vimrc to automatically give scripts with a shebang (e.g., #!/usr/bin/perl) executable permissions when saving.

Found @ http://stackoverflow.com/questions/817060/creating-executable-files-in-linux/817522#817522

:%d
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
:w !curl -F "sprunge=<-" http://sprunge.us | xclip
2010-04-25 00:43:37
User: shawnjgoff
Tags: vim vi share
15

Sprunge.us is a code/text sharing site like pastebin, but it is easy to post stuff from the command line.

How it works:

:w !command

In vim, w writes the current tab to a file when a filename is given afterwards, but if !command is given, the output is piped to the stdin of command.

curl -F "sprunge=<-" http://sprunge.us

curl is an HTTP client. The -F option does an HTTP post to the given address. The data in the quotes is passed in the post. The "sprunge=" part sets up a fieldname - the part that follows is what is associated with the name. The "<" tells curl to send data from the file descriptor that follows it. The "-" in bash is a file descriptor that points to stdin instead of an actual file; in this case, stdin is being piped in from vim. After we send the HTTP post to sprunge.us, it will give back a url that points to the data you just sent.

| xclip

xclip is a utility that lets you put stuff in your clipboard or selection buffer. This part uses a bash pipe ( | ) to redirect the stdout of the previous command to the stdin of the next command. So, we're capturing the URL that curl gave us and putting it into the selection buffer, ready to paste into IRC or a forum.

Notes:

Of course, for this to work, you must have curl (which comes by default on most distroes), and xclip installed.

When you share the url, you can append "?lang" to highlight and have line numbers. Check out http://sprunge.us/BZXV?log for line numbers and http://sprunge.us/BZXV?ruby for highlighting.

If you prefer to use ctrl-v (paste from clipboard) instead of middle-click (paste from selection buffer), look up options on xclip - you can do that.

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.

sudo sed -iorig '/\(up\|down\)/s/^/#/' /etc/zsh/zshrc
2010-02-02 23:17:08
User: cbrinker
Functions: sed sudo
Tags: Ubuntu vi zsh
1

Use sed to comment out any up/down bindings in zsh

alias :q='tput setaf 1; echo >&2 "this is NOT vi(m) :/"; tput sgr0'
2009-12-08 12:59:44
User: sputnick
Functions: alias echo tput
Tags: vim alias vi tput
-1

For vi(m) users :

Add it in your ~/.bashrc

Add an "exit" @ the end if you are masochist ;)

:!>test.txt
:%s/^V^M//g
2009-08-19 11:59:22
User: slim
-1

Whereas ^V is CTRL-V.

converts a dos file to unix by removing 0x13 characters

ggdG
%
2009-05-21 15:02:39
User: haivu
Tags: vim vi
6

This is a simple command for jumping to the matching brace, square bracket, or parentheses. For example, it can take you from the beginning of a function to the end with one key stroke. To delete everything between the pairs of {}, [], or (), issue the command:

d%

To replace text between pairs of braces, brackets, or parentheses, issue the command:

c%

You can also use this command to find out if an opening brace has been properly closed.

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
1

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.