开发者

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:

  1. qaq: clear a register (starts recording a macros that will be stored in a register and then immediately stops recording before typing anything else);
  2. qa: start recording a macros in register a;
  3. 0: go to the very beginning of the line;
  4. 1f<Tab>: jump to next tab. 1 may be omitted here. If you want to delete first column, then omit the whole motion;
  5. d2t<Tab>: delete to second next tab;
  6. j: move to next line;
  7. @a: run macros stored in register a. It is empty at this point (because it was cleared in 1st item), so nothing is actually done;
  8. q: stop recording a macros;
  9. @a: run macros stored in register a. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜