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

  • SEARCH
  • Home
  • 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 8933015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:26:14+00:00 2026-06-15T09:26:14+00:00

I have two text files, ones is the file I’m currently working, the other

  • 0

I have two text files, ones is the file I’m currently working, the other contains a list of line numbers. What I would like to do is to highlight lines in the first file where the line number matches the latter one.

E.g.:

File1:

I like eggs
I like meat
I don't like eggplant
My mom likes chocolate
I like chocolate too

File2:

2
4

In this example the those lines should be highlighted:

I like meat
My mom likes chocolate

Thanks!

  • 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-15T09:26:16+00:00Added an answer on June 15, 2026 at 9:26 am

    You can use readfile() to read in the line numbers, and then convert them into a regular expression that matches those line numbers (e.g. \%42l). The highlighting can be done via :match, or matchadd().

    Here’s all this condensed into a custom :MatchLinesFromFile command:

    ":MatchLinesFromFile {file}
    "           Read line numbers from {file} and highlight all those
    "           lines in the current window.
    ":MatchLinesFromFile    Remove the highlighting of line numbers.
    "
    function! s:MatchLinesFromFile( filespec )
        if exists('w:matchLinesId')
            silent! call matchdelete(w:matchLinesId)
            unlet w:matchLinesId
        endif
        if empty(a:filespec)
            return
        endif
    
        try
            let l:lnums =
            \   filter(
            \   map(
            \       readfile(a:filespec),
            \       'matchstr(v:val, "\\d\\+")'
            \   ),
            \   '! empty(v:val)'
            \)
    
            let l:pattern = join(
            \   map(l:lnums, '"\\%" . v:val . "l"'),
            \   '\|')
    
            let w:matchLinesId = matchadd('MatchLines',  l:pattern)
        catch /^Vim\%((\a\+)\)\=:E/
            " v:exception contains what is normally in v:errmsg, but with extra
            " exception source info prepended, which we cut away.
            let v:errmsg = substitute(v:exception, '^Vim\%((\a\+)\)\=:', '', '')
            echohl ErrorMsg
            echomsg v:errmsg
            echohl None
        endtry
    endfunction
    command! -bar -nargs=? -complete=file MatchLinesFromFile call <SID>MatchLinesFromFile(<q-args>)
    
    highlight def link MatchLines Search
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two text files bala.txt and bala1.txt bala.txt contains text line by line
I have two text files bala.txt and bala1.txt bala.txt contains text line by line
I have about 150 text files filled with character information. Each file contains two
I have two text files in two different languages and they are aligned line
I have two 3GB text files, each file has around 80 million lines. And
i have two text files file 1 number,name,account id,vv,sfee,dac acc,TDID 7000,john,2,0,0,1,6 7001,elen,2,0,0,1,7 7002,sami,2,0,0,1,6 7003,mike,1,0,0,2,1
I have a two huge text files whose format is as below. File 1:
I want to append two text files together. I have one file with a
I have a text files that contain two numbers which represent monetary amounts. When
I have two text files that have these structures: File 1 Column1:Column2 Column1:Column2 ...

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.