Delete first word of each line

前端 未结 7 2059
耶瑟儿~
耶瑟儿~ 2021-02-01 16:45

How do I delete first word of each line in Vim?

How about a pattern on each line?

相关标签:
7条回答
  • 2021-02-01 17:18
    :%s,^[^ ]*,,
    

    From the beginning of the line match anything, but not a space and replace with none.

    0 讨论(0)
  • 2021-02-01 17:23

    I would use something like the following:

    :%s/^\w+\s+//
    

    The regular expression will match one or more "word" characters starting at the beginning of the line followed by at least one whitespace character. It will remove the word and any following whitespace. If a line can contain only a single word -- and you still want it removed -- you could use alternation to match either whitespace or the end of line.

    :%s/^\w+(\s+|$)//
    
    0 讨论(0)
  • 2021-02-01 17:28

    First word (where word is defined as no whitespace)

    :%s/^\s*[^ ]* //g
    

    Delete pattern:

    :%s/< insert pattern here >//g
    
    0 讨论(0)
  • 2021-02-01 17:29

    :normal to the rescue:

    :%norm dw
    

    It basically replays the arguments as if you were typing them in normal ('non-edit') mode.

    From :help :

    :norm[al][!] {commands}

    Execute Normal mode commands {commands}.

    This makes it possible to execute Normal mode commands typed on the command-line. {commands} is executed like it is typed.

    0 讨论(0)
  • 2021-02-01 17:34

    What about this?
    :%!cut -s -d' ' -f2-

    0 讨论(0)
  • 2021-02-01 17:34

    Although this is an old question, if someone else is looking to do this you could do use visual block.

    • press ctrl+v
    • select all the lines you would want to edit
    • now use arrow keys to select the entire word
    • press 'x'

    That would delete the first word in all the lines. This method is especially very handy to edit log files

    0 讨论(0)
提交回复
热议问题