Vim Tips Wiki
(Use `all` instead of `ball`)
 
(6 intermediate revisions by 4 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
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
I use the <tt>:split</tt> command a lot -- both to open a second window containing the currently edited file and to edit a new file altogether (with the <tt>:split <filename></tt> option). however, I also like to be able to edit more than one file and calling <tt>:sp</tt> 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 31: Line 31:
 
</pre>
 
</pre>
   
This retains the behaviour of <tt>:sp</tt> in that I can still type <tt>:sp</tt> (the abbreviation takes care of that). <tt>:Sp</tt> 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.
 
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.
Line 66: Line 66:
 
<pre>
 
<pre>
 
function! Sp(dir, ...)
 
function! Sp(dir, ...)
let split = 'sp'
+
let split = 'sp'
if a:dir == '1'
+
if a:dir == '1'
let split = 'vsp'
+
let split = 'vsp'
endif
+
endif
if(a:0 == 0)
+
if(a:0 == 0)
execute split
+
execute split
else
+
else
let i = a:0
+
let i = a:0
while(i > 0)
+
while(i > 0)
execute 'let files = glob (a:' . i . ')'
+
execute 'let files = glob (a:' . i . ')'
for f in split (files, "\n")
+
for f in split (files, "\n")
execute split . ' ' . f
+
execute split . ' ' . f
endfor
+
endfor
let i = i - 1
+
let i = i - 1
endwhile
+
endwhile
windo if expand('%') == '' | q | endif
+
windo if expand('%') == '' | q | endif
 
endif
 
endif
 
endfunction
 
endfunction
Line 88: Line 88:
 
</pre>
 
</pre>
   
  +
----
== Starting in splits ==
 
  +
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
 
If you want to start vim with several files in a splitted window, just type
 
<pre>
 
<pre>
Line 97: Line 104:
 
vim -O a b c
 
vim -O a b c
 
</pre>
 
</pre>
for the vertical split. For further information, you can consult
+
for the vertical split.
  +
 
To change between the windows opened
 
<pre>
 
<pre>
man vim
 
</pre>
 
To change between the windows opened
 
</pre>
 
 
crtl+ww
 
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: