the '!' command in vi spawns a shell, then pipes all of the specified lines in the buffer through the command specified after '!', replacing all input lines with the result of the command.
You can record, then replay a series of keystrokes in vim. In command mode 'q', then a letter [a-zA-Z] starts macro recording mode. Enter a series of vim commands. When done, enter command mode again, and press 'q' to stop recording. To replay, enter command mode, then press @{letter}
While editing a source file in vim, or using vimdiff to compare two or more files, the ':TOhtml' command can be used to export each buffer as an html file, including syntax highlighting and vimdiff colorization. If you are in insert mode in vim, you will have to type :TOhtml This will open a new buffer filled with html, which you can then save.
This function is used to sort selected lines of a text file to the end of that file. Especially useful in cases where human intervention is necessary to sort out parts of a file. Let's say that you have a text file which contains the words rough slimy red fluff dough For whatever reason, you want to sort all words rhyming with 'tough' to the bottom of the file, and all words denoting colors to the top, while keeping the order of the rest of the file intact. '$EDITOR' will open, showing all of the lines in the given file, numbered with '0' padding. Adding a '~' to the beginning of the line will cause the line to sort to the end of the file, adding '!' will cause it to sort to the beginning. Show Sample Output
First argument: string to put a box around. Second argument: character to use for box (default is '=') Same as command #4948, but shorter, and without the utility function. Show Sample Output
The function 'box' takes either one or two arguments. The first argument is a line of text to be boxed, the second argument (optional) is a character to use to draw the box. By default, the drawing character will be '='. The function 'n()' is a helper function used to draw the upper and lower lines of the box, its arguments are a length, and an character to print. (I used 'n' because 'line', 'ln' and 'l' are all commonly used) Show Sample Output
underline() will print $1, followed by a series of '=' characters the width of $1. An optional second argument can be used to replace '=' with a given character.
This function is useful for breaking lots of data emitted in a for loop into sections which are easier to parse visually. Let's say that 'xxxx' is a very common pattern occurring in a group of CSV files.
You could run
grep xxxx *.csv
This would print the name of each csv file before each matching line, but the output would be hard to parse visually.
for i in *.csv; do printf "\n"; underline $i; grep "xxxx" $i; done
Will break the output into sections separated by the name of the file, underlined.
Show Sample Output
For those who hate navigating info pages, a shell function which will dump the contents to stdout, then page it through less, thus acting like 'man'.
Like command #4845, prints score, number of entries, and average score.
given lines of the form 123|XXXX|1000 ... 123|XXXX|1011 each 'XXXX' will be replaced with a serial number between 0001 and 0004. Show Sample Output
This replaces the current bash session with a new bash session, run as an interactive non-login shell... useful if you have changed /etc/bash.bashrc, or ~/.bashrc
If you have changed a startup script for login shells, use
exec bash -l
Suitable for re-running /etc/profile, ~/.bash_login and ~/.profile.
edit: chinmaya points out that
env - HOME=$HOME TERM=$TERM bash -s "exec bash -l"
will clear any shell variables which have been set... since this verges on unwieldy, might want to use
alias bash_restart='env - HOME=$HOME TERM=$TERM bash -s "exec bash -l"'
The function will take a comma separated list of items to be 'selected' by xsel -i:
smenu "First item to paste,Paste me #2,Third menu item"
You will then be prompted to choose one of the menu items. After you choose, you will be able to paste the string by clicking the middle mouse button.
The menu will keep prompting you to choose menu items until you break out with Control-C.
Show Sample Output
Set up X forwarding in PuTTY, with X display location set to :0.0
Launch PuTTY ssh session.
Launch Xming. Make sure that display is set to :0.0 (this is default).
echo "I'm going to paste this into WINDERS XP" | xsel -i
will insert the string into the windows cut and paste buffer.
Thanks to Dennis Williamson at stackoverflow.com for sharing...
Each shell function has its own summary line, as a comment. If there are multiple shell functions with the same name, the function with the highest number of votes is put into the file. Note: added 'grep -v' to the end of the pipeline, to eliminate extraneous lines containing only '--'. Thanks to matthewbauer for pointing this out.
the 'set -x' mode can be exited by typing
set +x
Show Sample Output
This is useful when you're diffing two files of the same name in radically different directory trees. For example:
Set
path1='/some/long/convoluted/path/to/all/of/your/source/from/a/long/dead/machine'
then
path2='/local/version/of/same/file'
then run the command. Much easier on the eyes when you're looking back across your command history, especially if you're doing the same diff over and over again.
Creates a directory named with the current date, in the format YYYYMMDD. If you give it a directory name as an argument, it will create the new directory inside the specified directory. This is an alternative to command #1993. Show Sample Output
for example:
echo "..1234567." | cut -c $(range 3 7)
yields
1234567
This version of tweet() doesn't require you to put quotes around the body of your tweet... it also prompts you for password. It will still barf on a '!' character.
commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.
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
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: