Vim Tips Wiki
(Uploaded by JohnBot from a locally edited file)
(Use `all` instead of `ball`)
 
(12 intermediate revisions by 6 users not shown)
Line 4: Line 4:
 
|previous=144
 
|previous=144
 
|next=147
 
|next=147
|created=October 23, 2001
+
|created=2001
 
|complexity=basic
 
|complexity=basic
|Author=salmanhalim
+
|author=salmanhalim
 
|version=5.7
 
|version=5.7
 
|rating=175/48
 
|rating=175/48
  +
|category1=
  +
|category2=
 
}}
 
}}
I use the :split command a lot -- both to open a second window containing the currently edited file and to edit a new file altogether (with the :split <filename> option). however, i also like to be able to edit more than one file and calling :sp multiple times is inconvenient. so, i created the following command, function and abbreviation:
+
I use the <code>:split</code> command a lot -- both to open a second window containing the currently edited file and to edit a new file altogether (with the <code>:split <filename></code> option). however, I also like to be able to edit more than one file and calling <code>:sp</code> multiple times is inconvenient. so, I created the following command, function and abbreviation:
   
 
<pre>
 
<pre>
Line 18: Line 20:
 
else
 
else
 
let i = a:0
 
let i = a:0
while(i &gt; 0)
+
while(i > 0)
 
execute 'let file = a:' . i
 
execute 'let file = a:' . i
 
execute 'sp ' . file
 
execute 'sp ' . file
Line 25: Line 27:
 
endif
 
endif
 
endfunction
 
endfunction
com! -nargs=* -complete=file Sp call Sp(&lt;f-args&gt;)
+
com! -nargs=* -complete=file Sp call Sp(<f-args>)
 
cab sp Sp
 
cab sp Sp
 
</pre>
 
</pre>
   
This retains the behaviour of :sp in that i can still type :sp (the abbreviation takes care of that). :Sp takes any number of files and opens them all up, one after the other.
+
This retains the behaviour of <code>:sp</code> in that I can still type <code>:sp</code> (the abbreviation takes care of that). <code>:Sp</code> takes any number of files and opens them all up, one after the other.
   
The things i have noticed are that this causes 'sp' to be expanded to 'Sp' everywhere, even in search patterns. also, prepending 'vert' doesn't work. if there is interest, i'll do that.
+
The things I have noticed are that this causes 'sp' to be expanded to 'Sp' everywhere, even in search patterns. Also, prepending 'vert' doesn't work.
   
 
==Comments==
 
==Comments==
This is great! It saves the effort of typing multiple :sp under vim. I have 2 suggestions
+
This is great! It saves the effort of typing multiple :sp under vim. I have two suggestions.
   
 
1. If I use
 
1. If I use
  +
<pre>
 
vim
 
:Sp a b c
  +
</pre>
   
 
It results in 4 windows in vim. One is empty. Is this could be improved?
vim
 
:Sp a b c
 
   
 
2. Is there a way to make a new_cmd that splits a window to 3, each one for a file specified in the command line? For example,
It results in 4 windows in vim. One is empty. Is this could be improved?
 
  +
<pre>
 
 
vim a b c
2. Is there a way to make a new_cmd that splits a window to 3, each one for a file specified in the command line?
 
 
:new_cmd
For example,
 
  +
</pre>
 
vim a b c
 
:new_cmd
 
   
 
then I could see 3 windows in vim. One for a, one for b, and one for c.
 
then I could see 3 windows in vim. One for a, one for b, and one for c.
Line 53: Line 56:
 
----
 
----
 
Just before the endif, add this:
 
Just before the endif, add this:
  +
<pre>
 
windo if expand('%') == '' | q | endif
+
windo if expand('%') == '' | q | endif
  +
</pre>
   
 
Empty windows will be closed (if unmodified).
 
Empty windows will be closed (if unmodified).
   
 
----
 
----
  +
I added an option to split also vertically and made filenames expand through glob:
  +
<pre>
  +
function! Sp(dir, ...)
  +
let split = 'sp'
  +
if a:dir == '1'
  +
let split = 'vsp'
  +
endif
  +
if(a:0 == 0)
  +
execute split
  +
else
  +
let i = a:0
  +
while(i > 0)
  +
execute 'let files = glob (a:' . i . ')'
  +
for f in split (files, "\n")
  +
execute split . ' ' . f
  +
endfor
  +
let i = i - 1
  +
endwhile
  +
windo if expand('%') == '' | q | endif
  +
endif
  +
endfunction
  +
com! -nargs=* -complete=file Sp call Sp(0, <f-args>)
  +
com! -nargs=* -complete=file Vsp call Sp(1, <f-args>)
  +
</pre>
  +
  +
----
  +
This tip is superseded by <code>:argadd</code>:
  +
<pre>
  +
:argadd *.html
  +
:all
  +
</pre>
  +
  +
==Starting in splits==
  +
If you want to start vim with several files in a splitted window, just type
  +
<pre>
  +
vim -o a b c
  +
</pre>
  +
for the horizontal split, and
  +
<pre>
  +
vim -O a b c
  +
</pre>
  +
for the vertical split.
  +
  +
To change between the windows opened
  +
<pre>
  +
crtl+ww
  +
</pre>
  +
  +
For further information, you can consult:
  +
*man vim
  +
*{{help|-o}}
  +
*{{help|CTRL-W_w}}
  +
*{{help|windows}}

Latest revision as of 23:15, 20 March 2014

Tip 146 Printable Monobook Previous Next

created 2001 · complexity basic · author salmanhalim · version 5.7


I use the :split command a lot -- both to open a second window containing the currently edited file and to edit a new file altogether (with the :split <filename> option). however, I also like to be able to edit more than one file and calling :sp multiple times is inconvenient. so, I created the following command, function and abbreviation:

function! Sp(...)
  if(a:0 == 0)
    sp
  else
    let i = a:0
    while(i > 0)
      execute 'let file = a:' . i
      execute 'sp ' . file
      let i = i - 1
    endwhile
  endif
endfunction
com! -nargs=* -complete=file Sp call Sp(<f-args>)
cab sp Sp

This retains the behaviour of :sp in that I can still type :sp (the abbreviation takes care of that). :Sp takes any number of files and opens them all up, one after the other.

The things I have noticed are that this causes 'sp' to be expanded to 'Sp' everywhere, even in search patterns. Also, prepending 'vert' doesn't work.

Comments[]

This is great! It saves the effort of typing multiple :sp under vim. I have two suggestions.

1. If I use

vim
:Sp a b c

It results in 4 windows in vim. One is empty. Is this could be improved?

2. Is there a way to make a new_cmd that splits a window to 3, each one for a file specified in the command line? For example,

vim a b c
:new_cmd

then I could see 3 windows in vim. One for a, one for b, and one for c.


Just before the endif, add this:

windo if expand('%') == '' | q | endif

Empty windows will be closed (if unmodified).


I added an option to split also vertically and made filenames expand through glob:

function! Sp(dir, ...)
  let split = 'sp'
  if a:dir == '1'
    let split = 'vsp'
  endif
  if(a:0 == 0)
    execute split
  else
    let i = a:0
    while(i > 0)
      execute 'let files = glob (a:' . i . ')'
      for f in split (files, "\n")
        execute split . ' ' . f
      endfor
      let i = i - 1
    endwhile
    windo if expand('%') == '' | q | endif
endif
endfunction
com! -nargs=* -complete=file Sp call Sp(0, <f-args>)
com! -nargs=* -complete=file Vsp call Sp(1, <f-args>)

This tip is superseded by :argadd:

  :argadd *.html
  :all

Starting in splits[]

If you want to start vim with several files in a splitted window, just type

vim -o a b c

for the horizontal split, and

vim -O a b c

for the vertical split.

To change between the windows opened

crtl+ww

For further information, you can consult: