sponsor Vim development Vim logo Vim Book Ad

basic Tip #234: Vi(M) Command Line tips & tricks

 tip karma   Rating 47/28, Viewed by 9193 

created:   April 11, 2002 3:18      complexity:   basic
author:   David Rayner (zzapper)      as of Vim:   6.0

Hi VIMMERs

These tips save me wearing out my delicate little fingers with unnecessary keystrokes.
They assume Unix, but I also use them on a Windows Unix Shell (MKS) as well

# When I know the file i want to edit is the most recent file in a directory

alias -x vew='vi `l\s -t * | head -1 `'

#When I know the file I want to edit contains a unique keyword
#this is actually in a little shell script call ed vg where the keyword is passed as parameter $1
#/bin/sh
#name vg
vi.exe  $(grep -isl $1 *) &

# some variations
alias -x vp='vi `l\s -t *.@(pl|cgi)| head -1 `'

#execute the most recent script (I call this from within VIM with a mapped button)
alias -x xew='`l\s -t *.pl | head -1 `'

Cheers zzapper

 rate this tip  Life Changing Helpful Unfulfilling 

<<Some tips for using Vim to write Lisp code | Toggle highlight word under cursor, to find cursor. >>

Additional Notes

[email protected], April 13, 2002 2:54
Hi
I forgot my best script v.ksh this allows me to edit a file(s) containing any particular substring
such say I know that a file in a directory is the only one that contains the letter z I just need to type

> v z


# v.ksh
# description : vi all files containing $1 in name
#               but excluding binaries
#set -x
filenum=0
for x in *$1*
do
   if [[ "$x" != *.@(xls|exe|mdb) ]] && [ -f "$x" ]
   then
      filelst[$filenum]=$x
      let filenum=filenum+1
   fi
done

vim ${filelst[*]} &
# end script

If your shell (ef csh) allows you alias parameters this can also be done with
something like (sorry I forget)

alias v=''vi.exe *\($1\)*'
[email protected], April 18, 2002 11:56
that 'vew' thing is very cool.  i _always_ want to do that, very useful...  i had to have it like this under cygwin...

alias vnew='vim `ls -t | head -1 `'
alias vold='vim `ls -t | tail -1 `'

vold is cool when you want to edit each file in a directory but don't want to deal with buffers inside vim.
[email protected], April 20, 2002 15:12
Hi I've just remembered he syntax

A Csh alias (for shells that allow parameters)

alias v 'vi *\!*\*'
You can now edit a file in a directory that is say the only one with a filename containing say "bank"
just by typing

> v bank

alsp don't forget the wonderfull fact that vim will edit a pipe

> ls -1 fred*.c | vim -

will result in VIm opening an unnamed file containing a list of files fred* r
[email protected], May 29, 2002 11:16

Open a file with vi(m) and jump to a particular place defined by "searchstr"

vi -c "/searchstr" main.c

turn this into a script
#vis.ksh
#vi with search
vi -c "/$1" $2






[email protected], May 29, 2002 11:22
how many times you decided to use one file as a basis for a new file, started editting the original, and then absently minded saved it, rather than saving as!!

this shell script simplifies and avoids this problem

# vcp.ksh
# description : copy file $1 to $2 then edit $2
#set -x
cp $1 $2
vi $2
[email protected], July 22, 2002 8:53
View Gvim Command Line help by typing
gvim -h

Notables
gvim -u local_vimrc
gvim --noplugin
gvim -v (Vi compatible)
gvim + fred.php (jump to End of file)
gvim +10 fred.php (jump to 10th line)
gvim -w hist.txt fred.php (append all typed commands to hist.txt)
gvim -c "/searchstr" main.c (jump to string when main.c opened)(note quotes)
gvim -R important.txt (open read only)
[email protected], July 29, 2002 9:42
To summarise

# the following is an alias to edit the most recent file in a directory
> vew

# the following script v.ksh , edits any file in current directory whose name contains say "main"
>v main

# the following script vg.ksh (see below) , edits any file containing the specified keyword and jumps to 1st occurrence
>vg fn_main

# vg.ksh
gvim.exe -c "/$1"  $(grep -isl "$1" *)

# gvim can edit a pipe
ls -1 *.php | gvim -

These should run with adaptions on Unix, and on Windows with CYGWin,MKS etc
[email protected], August 10, 2002 2:01
Ftping via VIM

gvim ftp://ftp.yoursite.co.uk/public_html/index.html
[email protected], September 2, 2002 6:32
Open file1 & file2 into a split window
gvim -o file1 file2
[email protected], September 8, 2002 2:04
# compare differences in 2 files (vimdiff)
# see :h vimdiff
gvim -d file1 file1
[email protected], September 18, 2002 2:47
# performing edits on multiple files (pipe separates commands)
vim -c "argdo %s/ABC/DEF/g | w" *.txt
[email protected], July 29, 2003 12:45
vim -c "argdo %s/FOO/BAR/g | update" `grep -l FOO *`
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo