How to cut a text file using Vim

vim

How can I cut a text file using vim? For example I would like to cut the file up to position 56. So I would like to keep everything from position 1-56, and delete everything from position 57 (including 57) to the end of the line for EVERY line.

Text file:

Web Application - http://sharepoint

Url                                                     CompatibilityLevel
---                                                     ------------------
http://sharepoint                                       15
http://sharepoint/sites/appcatalog                      15
http://sharepoint/sites/devtest                         15
http://sharepoint/sites/publishing                      15

I can get more or less the results I want using awk:

$awk '{print $1}' file

Results:

Web

Url
---
http://sharepoint
http://sharepoint/sites/appcatalog
http://sharepoint/sites/devtest
http://sharepoint/sites/publishing

But say I want to split hairs and really want to keep the title. How can I technically cut this file up to position 56 using vim. Preferably within vim. Meaning I have the file open in vim.

Best Answer

There are many ways to do it, here are a few of them:

  1. :%s/\%>56c.//g
  2. :%s/\%57c.*//
  3. :%s/\v^.{56}\zs.*//
  4. :% normal 57|D (this one assumes virtualedit=all or virtualedit=onemore)
  5. Shift-g57|Ctrl-v1Shift-gEndx (this one assumes virtualedit=all)

Edit: Adding the relevant regurgitations from the manual, as requested:

  • For 1. and 2. (:h /\%c):

    \%23c Matches in a specific column.
    \%<23c Matches before a specific column.
    \%>23c Matches after a specific column.
    These three can be used to match specific columns in a buffer or string. The "23" can be any column number. The first column is 1. Actually, the column is the byte number (thus it's not exactly right for multi-byte characters).

  • For 3. (:h \zs):

    \zs Matches at any position, and sets the start of the match there: The next char is the first char of the whole match.

  • For 4. (:h |, :h D, :h :normal, :h :%, :h 'virtualedit'):

    | To screen column [count] in the current line. exclusive motion. Ceci n'est pas une pipe

    ["x]D Delete the characters under the cursor until the end of the line and [count]-1 more lines [into register x]; synonym for "d$".

    :norm[al][!] {commands} Execute Normal mode commands {commands}. This makes it possible to execute Normal mode commands typed on the command-line. {commands} are executed like they are typed.

    % equal to 1,$ (the entire file)

    'virtualedit' A comma separated list of these words:
    block Allow virtual editing in Visual block mode.
    insert Allow virtual editing in Insert mode.
    all Allow virtual editing in all modes.
    onemore Allow the cursor to move just past the end of the line

    Virtual editing means that the cursor can be positioned where there is no actual character. This can be halfway into a tab or beyond the end of the line. Useful for selecting a rectangle in Visual mode and editing a table.

  • For 5. (:h G, :h CTRL-V, :h <End>, :h x):

    G Goto line [count], default last line, on the first non-blank character linewise.

    [count]CTRL-V Start Visual mode blockwise.

    $ or <End> To the end of the line.

    ["x]x Delete [count] characters under and after the cursor [into register x] (not linewise). Does the same as "dl".

Related Question