Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete from cursor to end of line under visual selection in vim

Tags:

vim

I have a code segment like below.

type Account struct {                                                                                                                                                                                       
    Id                         int                                                                                                                                                                          
    UserId                     int                                                                                                                                                                          
    Name                       string                                                                                                                                                                       
    Address                    string                                                                                                                                                                       
    City                       string                                                                                                                                                                       
    State                      string                                                                                                                                                                       
    CountryId                  string 
}

I want to delete all the data types. Is there a key combination to this?

I tried <C-V> and select the first letter of all data types in a vertical line, hoping d + $ would work post that, however vim only takes the first input d and deletes the first letter.

like image 915
tsudot Avatar asked Apr 17 '15 07:04

tsudot


People also ask

Which command would delete from the cursor to the end of the line in Vim?

The command dw will delete from the current cursor position to the beginning of the next word character. The command d$ (note, that's a dollar sign, not an 'S') will delete from the current cursor position to the end of the current line. D (uppercase D) is a synonym for d$ (lowercase D + dollar sign). Save this answer.


3 Answers

Use <C-v> to enter visual block mode, select the lines you want to change and then D to delete until the end of those.

From :h v_D :

{Visual}["x]X   or                  *v_X* *v_D* *v_b_D*
{Visual}["x]D       Delete the highlighted lines [into register x] (for
                    {Visual} see |Visual-mode|).  In Visual block mode,
                    "D" deletes the highlighted text plus all text until
                    the end of the line.  {not in Vi}

Note that, as mentioned in the help, X and D are not equivalent in visual block mode (X only deletes the current selection, not until the end of the line).

like image 124
Marth Avatar answered Oct 14 '22 17:10

Marth


You can move to the left brace, press the % key and issue:

s/ \+[^ ]* *$/

to get:

type Account struct
    Id
    UserId
    Name
    Address
    City
    State
    CountryId
}

The substitution removes all non-white-space characters at the end of the line.

like image 45
perreal Avatar answered Oct 14 '22 17:10

perreal


You can use C-V and select the first column of all data type, then do $ to select until the end of the line, followed by x or d to delete.

like image 42
Math Avatar answered Oct 14 '22 19:10

Math