Vim Tips Wiki
No edit summary
(Change <tt> to <code>, perhaps also minor tweak.)
 
(8 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{TipImported
 
{{TipImported
 
|id=234
 
|id=234
|previous=233
+
|previous=232
 
|next=235
 
|next=235
|created=April 11, 2002
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=David Rayner (zzapper)
 
|author=David Rayner (zzapper)
 
|version=6.0
 
|version=6.0
 
|rating=72/39
 
|rating=72/39
  +
|category1=
  +
|category2=
 
}}
 
}}
 
These tips save me wearing out my fingers with unnecessary keystrokes. They assume Unix, but I also use them on a Windows Unix Shell (MKS).
 
These tips save me wearing out my fingers with unnecessary keystrokes. They assume Unix, but I also use them on a Windows Unix Shell (MKS).
 
 
<pre>
 
<pre>
 
#When I know the file I want to edit is the most recent file in a directory
 
#When I know the file I want to edit is the most recent file in a directory
Line 22: Line 23:
 
#/bin/sh
 
#/bin/sh
 
#name vg
 
#name vg
vi.exe $(grep -isl $1 *) &amp;
+
vi.exe $(grep -isl $1 *) &
   
 
#some variations
 
#some variations
Line 32: Line 33:
   
 
==Comments==
 
==Comments==
I forgot my best script v.ksh this allows me to edit a file(s) containing any particular substring
+
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
  +
<pre>
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
 
  +
</pre>
&gt; v z
 
   
 
<pre>
 
<pre>
Line 45: Line 46:
 
for x in *$1*
 
for x in *$1*
 
do
 
do
if [[ "$x" != *.@(xls|exe|mdb) ]] &amp;&amp; [ -f "$x" ]
+
if [[ "$x" != *.@(xls|exe|mdb) ]] && [ -f "$x" ]
 
then
 
then
 
filelst[$filenum]=$x
 
filelst[$filenum]=$x
Line 51: Line 52:
 
fi
 
fi
 
done
 
done
 
vim ${filelst[*]} &
 
vim ${filelst[*]} &amp;
 
 
</pre>
 
</pre>
   
 
If your shell (e.g. csh) allows you alias parameters this can also be done with something like:
 
If your shell (e.g. csh) allows you alias parameters this can also be done with something like:
  +
<pre>
 
alias v 'vi *\!*\*'
+
alias v 'vi *\!*\*'
  +
</pre>
   
 
And as a bash function:
 
And as a bash function:
  +
<pre>
 
function v () {
+
function v () {
vim *${1}*
+
vim *${1}*
}
+
}
  +
</pre>
   
 
You can now edit a file in a directory that is say the only one with a filename containing say "bank" just by typing
 
You can now edit a file in a directory that is say the only one with a filename containing say "bank" just by typing
  +
<pre>
 
> v bank
  +
</pre>
   
 
Also don't forget the wonderful fact that Vim will edit a pipe
&gt; v bank
 
  +
<pre>
 
> ls -1 fred*.c | vim -
  +
</pre>
   
 
will result in Vim opening an unnamed file containing a list of files fred* r
Also don't forget the wonderfull fact that vim will edit a pipe
 
 
&gt; ls -1 fred*.c | vim -
 
 
will result in VIm opening an unnamed file containing a list of files fred* r
 
   
 
----
 
----
 
That 'vew' thing is very cool. I _always_ want to do that, very useful. I had to have it like this under cygwin.
 
That 'vew' thing is very cool. I _always_ want to do that, very useful. I had to have it like this under cygwin.
  +
<pre>
 
alias vnew='vim `ls -t | head -1 `'
+
alias vnew='vim `ls -t | head -1 `'
alias vold='vim `ls -t | tail -1 `'
+
alias vold='vim `ls -t | tail -1 `'
  +
</pre>
   
 
vold is cool when you want to edit each file in a directory but don't want to deal with buffers inside vim.
 
vold is cool when you want to edit each file in a directory but don't want to deal with buffers inside vim.
Line 85: Line 90:
 
----
 
----
 
Open a file with vi(m) and jump to a particular place defined by "searchstr"
 
Open a file with vi(m) and jump to a particular place defined by "searchstr"
  +
<pre>
 
vi -c "/searchstr" main.c
+
vi -c "/searchstr" main.c
  +
</pre>
   
 
Turn this into a script
 
Turn this into a script
  +
<pre>
#vis.ksh
+
#vis.ksh
#vi with search
 
vi -c "/$1" $2
+
#vi with search
  +
vi -c "/$1" $2
  +
</pre>
   
 
----
 
----
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!
+
How many times you decided to use one file as a basis for a new file, started editing the original, and then absently minded saved it, rather than saving as!
   
 
This shell script simplifies and avoids this problem.
 
This shell script simplifies and avoids this problem.
 
 
<pre>
 
<pre>
 
#vcp.ksh
 
#vcp.ksh
Line 107: Line 114:
   
 
----
 
----
View Gvim Command Line help by typing
+
View gvim command line help by typing <code>gvim -h</code>
gvim -h
 
   
 
Notables
 
Notables
Line 124: Line 130:
 
----
 
----
 
To summarise
 
To summarise
 
 
<pre>
 
<pre>
 
#the following is an alias to edit the most recent file in a directory
 
#the following is an alias to edit the most recent file in a directory
&gt; vew
+
> vew
   
#the following script v.ksh , edits any file in current directory whose name contains say "main"
+
#the following script v.ksh, edits any file in current directory whose
  +
#name contains say "main"
&gt;v main
+
>v main
   
#the following script vg.ksh (see below) , edits any file containing the specified keyword and jumps to 1st occurrence
+
#the following script vg.ksh (see below), edits any file containing the
  +
#specified keyword and jumps to 1st occurrence
&gt;vg fn_main
+
>vg fn_main
   
 
#vg.ksh
 
#vg.ksh
Line 141: Line 148:
 
ls -1 *.php | gvim -
 
ls -1 *.php | gvim -
 
</pre>
 
</pre>
 
These should run with adaptions on Unix, and on Windows with CYGWin,MKS etc
 
   
 
----
 
----
Line 149: Line 154:
 
gvim ftp://ftp.yoursite.co.uk/public_html/index.html
 
gvim ftp://ftp.yoursite.co.uk/public_html/index.html
   
#Open file1 &amp; file2 into a split window
+
#Open file1 & file2 into a split window
 
gvim -o file1 file2
 
gvim -o file1 file2
   
Line 163: Line 168:
   
 
----
 
----
  +
Proper editing of the contents of a pipe on Unix is possible even with console Vim by using a temporary file and redirection of IO to the tty, an example of which is {{script|id=2769}}.
  +
<pre>
  +
find | pipe.vim | nl
  +
find | xargs pipe.vim
  +
  +
# for end of pipe/less with syntax highlighting, vi - is better:
  +
cat /etc/samba/smb.conf | vi -
  +
</pre>

Latest revision as of 05:20, 13 July 2012

Tip 234 Printable Monobook Previous Next

created 2002 · complexity basic · author David Rayner (zzapper) · version 6.0


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

#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 `'

Comments[]

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[*]} &

If your shell (e.g. csh) allows you alias parameters this can also be done with something like:

alias v 'vi *\!*\*'

And as a bash function:

function v () {
   vim *${1}*
}

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

Also don't forget the wonderful 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


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.


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

How many times you decided to use one file as a basis for a new file, started editing 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

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)

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 -

#Ftping via Vim
gvim ftp://ftp.yoursite.co.uk/public_html/index.html

#Open file1 & file2 into a split window
gvim -o file1 file2

#compare differences in 2 files (vimdiff)
#see :h vimdiff
gvim -d file1 file1

#performing edits on multiple files (pipe separates commands)
vim -c "argdo %s/ABC/DEF/g | w" *.txt

vim -c "argdo %s/FOO/BAR/g | update" `grep -l FOO *`

Proper editing of the contents of a pipe on Unix is possible even with console Vim by using a temporary file and redirection of IO to the tty, an example of which is script#2769.

find | pipe.vim | nl
find | xargs pipe.vim

# for end of pipe/less with syntax highlighting, vi - is better:
cat /etc/samba/smb.conf | vi -