Copy and paste in vi + some other tips

vim-editor_logo-300x300

I use vi a lot at work.  It really became a lot more useful when I figured out how to copy and paste blocks of code.

All of these are done in command mode:

shift-v =  select a line
ctrl-v = select a rectangle
Y = yank selection to buffer
P = paste from buffer before cursor
p = paste from buffer after cursor position
dd = delete a line (this also moves the line to the buffer, so you can still paste it)

Here are some other handy ones:

u = convert a selection to lowercase
u = undo previous action (if nothing is selected)
ctrl-R = redo an action
gg = go to the top of the document
G = go to the end of the document
###gg = go to line number ###
:%s/blah/whee/gc = replace “blah” with “whee” globally with confirmation
:%r~/whee.txt = insert text into the current document from a file (~ = your home directory)

Some of my favorite Linux commands

linux-penguin

Find one of many strings in a set of files
grep -E "str1|str2|str" file*.*

Search through files with multiple file extensions
grep -r --include=\*.{html,php,htm} str /path/to/directory/to/grep

Search through files with multiple extensions with one find command
find -name "*.jsp" -o -name "*.java" | xargs grep -i "whee"

Search through files with multiple extensions with one find command, regex method
find -regex ".*\(\.jsp\|\.php\|\.html\|\.htm\).*" | xargs grep -i "whee"

Replace newlines with HTML breaks in a file
sed ':a;N;$!ba;s/\n/\/g' file.txt

Rename multiple files
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done

Directory sizes here and below
du -sm $(find $1 -type d -maxdepth 1 -xdev) | sort -g

Find files containing between 600 to 700 characters, inclusive.
find . -size +599c -and -size -701c

Update a file’s timestamp, but do not create files
touch -c file*.txt

Spriglet #15: Encoding video that will play in Microsoft PowerPoint

PowerPoint (PPT) often gets a bad rap when it comes to video.

Here’s a little tutorial on how to convert a video so it will play embedded in a PPT presentation.

mplay32

1. First and foremost, PPT will only play video that mplay32.exe (Windows Media Player 5) will play.  If you want to test a video to see if it will work in PPT, click Start -> Run -> mplay32.exe.  Open your video and click the play button.  If you get an error, then it won’t work in PPT.  Here’s a good site with more information about video in PPT.

mediacoder

2. So you have a video that won’t play in mplay32.exe.  You need to convert it.  Enter the Open Source MediaCoder. You can download it here.

mediacoder1

3. Install and fire up MediaCoder.  Click the Add button.  Click Add File, browse to the video file and click Open.

mediacoder2

4. Click Video tab on the left side of the bottom pane.

mediacoder3

5. In the Mode drop-down box, choose “Bitrate-based”.  Change the bitrate to 1500.

mediacoder4
6.
Set the Format and Container both to MPEG1

mediacoder5
7.
Click the Start button.  Wait until it finishes.

mplay32c
8.
Open it in mplay32.exe and click the Play button.  If it plays, it’ll work in PPT.

ppt1
9.
Start PPT and insert the video by clicking Insert -> Movies and Sounds -> Movie from File.

ppt2
10.
Click the Automatically button so it plays when you visit the slide.  Resize the video box as needed.  If the video is in 16×9 format, you may need to set the background to black.  Start your presentation.  Voilà.