vim - delete everything between tab characters
I have a file that has tab separated data. I need to delete 2 columns. So, what command(s) can I use to delete everything from the curren开发者_JAVA技巧t tab to the next tab?
To delete columns 4 and 5:
:%s/^\(.\{-}\t\)\{3}\zs.\{-}\t.\{-}\t//
Explanation:
^ => start of line
.\{−} => as few characters as possible
\( .\{-}\t \)\{3} => three times as few characters as possible followed with a tab
\zs => start of match
It could be clearer with the \v
switch:
:%s/\v^(.{-}\t){3}\zs.{-}\t.{-}\t//
I think shift+ctrl+v is what you are looking for.
http://jvi.sourceforge.net/javahelpset/jvi-vis_block.html
Does this have to be done in vim? If you have a unix command line I'd go instead for the good old cut
command. For example the following will keep the first column, and everything including & after the 4th (discards 2 & 3)
cat filename | cut -d"\t" -f1,4- > outputfile
While you can use regular expressions, it is better to use simple f
or t
normal commands: when you are at the first character of the first column you want to delete d2f<Tab>
will delete this and next columns unless one column can include newline in it. If it can, then d2/<Tab>/e<CR>
(or d2/\t/e<CR>
) will do what you want. To expand it on other lines, use macros: qaqqa01f<Tab>d2t<Tab>j@aq@a
:
qaq
: cleara
register (starts recording a macros that will be stored ina
register and then immediately stops recording before typing anything else);qa
: start recording a macros in registera
;0
: go to the very beginning of the line;1f<Tab>
: jump to next tab.1
may be omitted here. If you want to delete first column, then omit the whole motion;d2t<Tab>
: delete to second next tab;j
: move to next line;@a
: run macros stored in registera
. It is empty at this point (because it was cleared in 1st item), so nothing is actually done;q
: stop recording a macros;@a
: run macros stored in registera
. It contains items 3.-7. and in 7th item it will run itself.
:%s:\t[^\t]\+\t:\t\t:
This deletes everything between two tabs.
HTH
精彩评论