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:
:%s/\%>56c.//g
:%s/\%57c.*//
:%s/\v^.{56}\zs.*//
:% normal 57|D
(this one assumesvirtualedit=all
orvirtualedit=onemore
)57
|Ctrl-v1Shift-gEndx (this one assumesvirtualedit=all
)Edit: Adding the relevant regurgitations from the manual, as requested:
:h /\%c
)::h \zs
):For 4. (
:h |
,:h D
,:h :normal
,:h :%
,:h 'virtualedit'
):For 5. (
:h G
,:h CTRL-V
,:h <End>
,:h x
):