Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Using Vim like notepad

  1. #1
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Using Vim like notepad

    Hi,

    Before you fry me for using the words Vim and notepad in the same line, let me explain

    Is there an option in Vim to highlight a portion of text then use that portion of text in a find and replace across the whole file?

    Much like highlighting some characters/words in notepad, pressing CTRL + C (to copy) then CTRL + H (to open the find/replace window) then CTRL + V (to paste the highlighted text in the "Find what" field), then completing the "Replace with" field, before clicking "Replace all".

    From my Googling I've come across pressing ‘:’ while in visual mode to run a search on the highlighted section - not what I'm looking for.

    How about using the highlighted section in the search itself?

    thanks

    John

  2. #2
    Join Date
    Nov 2008
    Beans
    128
    Distro
    Ubuntu

    Re: Using Vim like notepad

    you can use a sed command inside the command mode:

    for example:

    Code:
    :%s/old\ statement/new\ statement/g
    this will replace "old statement" with "new statement", notice that you have to use the backslash character to escape the space " " characters. Also the 'g' at the end will search and replace all instances in the file.

    Remember you also have to be in command mode by hitting escape before you try this.

    Have fun, this is a very powerful tool once you learn how to use it correctly.

  3. #3
    Join Date
    Oct 2007
    Location
    $HOME
    Beans
    631

    Re: Using Vim like notepad

    add some regex skills with the above and you have the most powerfull search/replace ever made

  4. #4
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Using Vim like notepad

    Thanks for the replies so far.

    Forgot to mention that the file I was looking to run a find / replace on, has some hidden TABS & spaces (Windows file).

    Ryan your sed approach does look good but I would prefer to run the find / replace within Vim. This will give me a visual sense of what I'm running it against and I won't need to know how to format TAB character's in a sed command.

    I'm keen on using Vim over notepad for reasons of speed & accuracy.

    I'll do some more Googling.....

    thanks

    John

  5. #5
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Using Vim like notepad

    looks like I found what I was looking for:

    http://www.vim.org/scripts/script.php?script_id=848

    Is there no easy way to do this?

  6. #6
    Join Date
    Nov 2008
    Beans
    128
    Distro
    Ubuntu

    Re: Using Vim like notepad

    What your looking for doesn't exist, at least with vim. You need to escape certain characters such as spaces even with find. What you can do is run a find to see what it highlights, and then drop that search in a sed command.

    It is much easier to learn the best way to do things in VIM than try to alter its functionality to suit your past experiences with other text editors. The power of vim comes from is differences not its similarities. It has a very a steep learning curve to be sure, but benefits you will see are well worth it in my opinion.

    just my 2 cents

    Good luck

  7. #7
    Join Date
    Feb 2006
    Location
    Vancouver, BC, Canada
    Beans
    318

    Re: Using Vim like notepad

    1. Select your text, and yank it. (press y to yank )

    2. Enter command mode and start the substitution operator (type :s/ )

    3. Paste into the command mode prompt: (type Ctrl-r")

    4. Since the s/// operator won't work for multi-line searches, look at what you pasted and remove any trailing ^M characters that might have gotten pasted in.

    5. Finish your substitution operator (type the next //, with a replacement string between them if desired)

    6. Hit enter to submit the command

  8. #8
    Join Date
    Feb 2005
    Beans
    509
    Distro
    Lubuntu 11.10 Oneiric Ocelot

    Re: Using Vim like notepad

    Thanks for the reply stylishpants!

    I'll have a closer look this eve.

  9. #9
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: Using Vim like notepad

    Quote Originally Posted by john_spiral View Post
    Ryan your sed approach does look good but I would prefer to run the find / replace within Vim.
    That is from within vim.

    http://www.geocities.com/volontir/#substitute

    Why he called it a "sed" command is beyond me. He is also wrong about having to escape spaces.
    Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
    L.A.G. - Jobs Dissembles - 2010/4/29

  10. #10
    Join Date
    Oct 2009
    Beans
    9

    Re: Using Vim like notepad

    Quote Originally Posted by john_spiral View Post
    Thanks for the replies so far.

    Forgot to mention that the file I was looking to run a find / replace on, has some hidden TABS & spaces (Windows file).

    Ryan your sed approach does look good but I would prefer to run the find / replace within Vim. This will give me a visual sense of what I'm running it against and I won't need to know how to format TAB character's in a sed command.

    I'm keen on using Vim over notepad for reasons of speed & accuracy.

    I'll do some more Googling.....

    thanks

    John
    I have no idea how well it would work but notepad comes bundled with wine. I was going to see if it would let me open jpg's like it does in windows but no cigar. Of course someone with the skill could maybe tweak wine and make it work.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •