How to remove quotes surrounding the first two columns in Vim?

38,109

Solution 1

Control-V is used for block select. That would let you select things in the same character column.

It seems like you want to remove the quotes around the numbers. For that use,

:%s/"\([0-9]*\)"/\1/g

Here is a list of what patterns you can do with vim.


There is one more (sort of ugly) form that will restrict to 4 replacements per line.

:%s/^\( *\)"\([ 0-9]*\)"\([ 0-9]*\)"\([ 0-9]*\)"/\1\2\3\4/g

And, if you have sed handy, you can try these from the shell too.

head -4 filename.txt | sed 's/pattern/replacement/g'

that will try your command on the first 4 lines of the file.

Solution 2

use visual block commands:

  • start mode with Ctrl-v
  • specify a motion, e.g. G (to the end of the file), or use up / down keys
  • for the selected block specify an action, e.g. 'd' for delete

For more see :h visual-mode

Solution 3

Say if you want to delete all columns but the first one, the simple and easy way is to input this in Vim:

:%!awk '{print $1}'

Or you want all columns but the first one, you can also do this:

:%!awk '{$1="";$0=$0;$1=$1;print}'

Indeed it requires external tool to accomplish the quest, but awk is installed in Linux and Mac by default, and I think folks with no UNIX-like system experience rarely use Vim in Windows, otherwise you probably known how to get a Windows version of awk.

Solution 4

Although this case was pretty simple to fix with a regex, if you want to do something even a bit more advanced I also recommend recording a macro like Bryan Ward. Also macros come easier to me than remembering which characters need to be escaped in vim's regexes. And macros are nice because you can see your changes take place immediately and work on your line transformation in smaller bits at a time.

So in your case you would have pressed qw to start recording a macro in register w (you can of course use any letter you want). I usually start my macros with a ^ to move to the start of the line so the macro doesn't rely on the location of the cursor. Then you could do a f" to jump to the first ", x to delete it, f" to jump to the next " and x to delete that too. Then q to finish recording.

Instead of making your macro end on the next line I actually as late as today figured out you can just V (visually line select) all lines you want to apply your macro to and execute :normal @w which applies your macro in register w to each visually selected line.

Solution 5

See column editing in vim. It describes column insert, but basically it should work in the same way for removing.

Share:
38,109
Smith.Patel
Author by

Smith.Patel

A golf fanatic trapped in a programming career (which I quite enjoy, mind you). Twitter: @jasondown Blog: ...I digress - My thoughts and experiences with functional programming LinkedIn: Jason Down GitHub: Jason Down I'm a moderator on ebooks.stackexchange.com. Come check it out! For my employer I get to develop with F# woo! Outside of work I'm also very interested in F# (and Elixir, but it's been a while). Really I'm very interested in functional programming in general. Haskell is on the bucket list. Check out the top 100 users in a given tag (helpful for learning interesting things about tags you are interested in). Interesting books I'd recommend: Domain Modeling Made Functional (Life manual for software developers... Highly recommended!) Dependency Injection in .Net (I'd say this is required reading for any .Net (or even Object Oriented) programmer). Second edition found here (slight name change). Functional Programming in C#. Second edition found here. Code: The Hidden Language of Computer Hardware and Software The Annotated Turing - A Guided Tour Through Alan Turing's Historic Paper on Computability and the Turing Machine Seven Languages in Seven Weeks

Updated on October 19, 2020

Comments

  • Smith.Patel
    Smith.Patel over 3 years

    Say I have the following style of lines in a text file:

    "12" "34" "some text     "
    "56" "78" "some more text"
    .
    .
    .
    etc.
    

    I want to be able to remove the quotes surrounding the first two columns. What is the best way to do this with Vim (I'm currently using gVim)?

    I figured out how to at least delete the beginning quote of each line by using visual mode and then enter the command '<,'>s!^"!!

    I'm wondering if there is a way to select an entire column of text (one character going straight down the file... or more than 1, but in this case I would only want one). If it is possible, then would you be able to apply the x command (delete the character) to the entire column.

    There could be better ways to do it. I'm looking for any suggestions.


    Update

    Just and FYI, I combined a couple of the suggestions. My _vimrc file now has the following line in it:

    let @q=':%s/"\([0-9]*\)"/\1/g^M'   
    

    (Note: THE ^M is CTRLQ + Enter to emulate pressing the Enter key after running the command)

    Now I can use a macro via @q to remove all of the quotes from both number columns in the file.