Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:08:54+00:00 2026-06-09T05:08:54+00:00

I have this file foo foo bar foo bar baz bar baz foo baz

  • 0

I have this file

foo
foo bar
foo bar baz
bar baz
foo baz
baz bar
bar
baz
foo 42
foo bar 42 baz
baz 42

I want to

  1. Select lines which contain foo and do NOT contain bar
  2. Delete lines which contain foo and do NOT contain bar

I read somewhere (can’t find the link) that I have to use :exec with | for this.

I tried the following, but it doesn’t work

:exec "g/foo" # works
:exec "g/foo" | exec "g/bar" -- first returns lines with foo, then with bar
:exec "g/foo" | :g/bar -- same as above

And ofcourse if I cannot select a line, I cannot execute normal dd on it.

Any ideas?

Edit

Note for the bounty:

I’m looking for a solution that uses proper :g and :v commands, and does not use regex hacks, as the conditions may not be the same (I can have 2 includes, 3 excludes).

Also note that the last 2 examples of things that don’t work, they do work for just deleting the lines, but they return incorrect information when I run them without deleting (ie, viewing the selected lines) and they behave as mentioned above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:08:57+00:00Added an answer on June 9, 2026 at 5:08 am

    This might still be hackish to you, but you can write some vimscript to make a function and specialized command for this. For example:

    command! -nargs=* -range=% G <line1>,<line2>call MultiG(<f-args>)
    fun! MultiG(...) range
       let pattern = ""
       let command = ""
       for i in a:000
          if i[0] == "-"
             let pattern .= "\\(.*\\<".strpart(i,1)."\\>\\)\\@!"
          elseif i[0] == "+"
             let pattern .= "\\(.*\\<".strpart(i,1)."\\>\\)\\@="
          else
             let command = i
          endif
       endfor
       exe a:firstline.",".a:lastline."g/".pattern."/".command
    endfun
    

    This creates a command that allows you to automate the “regex hack”. This way you could do

    :G +foo -bar
    

    to get all lines with foo and not bar. If an argument doesn’t start with + or - then it is considered the command to add on to the end of the :g command. So you could also do

    :G d +foo -bar
    

    to delete the lines, or even

    :G norm\ foXp +two\ foos -bar
    

    if you escape your spaces. It also takes a range like :1,3G +etc, and you can use regex in the search terms but you must escape your spaces. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file like this: foo, bar, foo1 bar, foo foo I want
I have this file file.txt which I want to split into many smaller ones.
I have this file which I need to read the first bytes to check
Say I have 2 files: foo bar baz and 123 456 f[want autocomplete here]
I have a string vaguely like this: foo,bar,c;qual="baz,blurb",d;junk="quux,syzygy" that I want to split by
Let's say I have a file located at C:\foo\bar.js and I want to include
I have a text file that is like this: FOO BAR PIPPO PLUTO 31337
What does this mean exactly? I'm doing something like this: File.Copy(@\\foo\bar\baz.txt, @c:\test\baz.txt); MSDN doesn't
I have this bash file: #/bin/bash PROP=-Dprop=foo bar java $PROP -jar Foo.jar So, what
I have a file like this. rm a.txt mkdir foo cp a.doc docs I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.