Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I open a collection of files in vim tabs using a glob pattern, I'd like to be warned if the number of files matching the pattern exceeds the value of tabpagemax. For example, if the following,

$ vi -p `find . -type f -name "*.tex"`

matches more files than tabpagemax, only tabpagemax tabbed documents are opened. I'd like a warning or error in this case. Can it be done?

share|improve this question

1 Answer 1

I'm not finding any method where vim can emit a warning when the tabpagemax is exceeded, so the next best thing is to check, prior to running vim.

Potential solution

Here we're running the find first, storing the results in a variable, $tfiles. We then only invoke vim -p if the number of files we've found is less than or equal to the $guard value, 20.

$ guard=20
$ tfiles=$(find . -type f -name "*.tex")
$ [ $(echo $tfiles | wc -w) -le $guard ] && vim -p $tfiles || echo "too many"

This can be converted to a oneliner like so:

$ guard=20; \
tfiles=$(find . -type f -name "*.tex"); \
[ $(echo $tfiles | wc -w) -le $guard ] && vim -p $tfiles || echo "too many"

Example

When we have more than say 3 files returned we'll get this type of experience:

$ guard=3; \
  tfiles=$(find . -type f -name "*.txt"); \
  [ $(echo $tfiles | wc -w) -le $guard ] && vim -p $tfiles || echo "too many"
too many

If we have less then 3 files, they'll be opened up in vim in separate tabs.

share|improve this answer
    
Thanks - yes, I had thought of that, but it's handy to see it worked through. I guess this could then also be wrapped up in a vimrc script; use tabpagemax etc. –  user7543 Sep 24 '13 at 6:51
1  
@user7543 - correct you could get fancy and get the tabpagemax in .vimrc, and have the $guard parse the file to get that setting. guard=$(grep tabpagemax ~/.vimrc | cut -d"=" -f2). –  slm Sep 24 '13 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.