vim: hide first n letters of all lines in a file

I am analyzing log file using vim and the format looks like this

YYYY-MM-DD HH:MM:SS.USEC PID Name LogText

Since Most of the times I don’t care about date and time. I want to hide them and just focus on the Name and LogText columns (To save some screen estate). Since the first three columns always occupy the first 35 letters in a line. Is there a way to make vim not display first 35 letters of each line ?

Asked By: user881300

||

Use vim’s filter functionality. Run:

:%!cut -b36-

to run the contents of your buffer through the cut command, retaining only bytes 36 and onwards. % means to run the entire buffer through and replace its contents with the output, then ! is the filter command, with the rest of the line as the program to run. This doesn’t modify the underlying file unless you then save the buffer over the top.

To get the original untrimmed buffer back you can use :e, provided that it’s backed by a real file.

Answered By: Michael Homer

In command mode, try:

:%s/^.{35}//
  • %s/pat/sub/: replace each occurence of pat with sub
  • ^.{35}: match first 35 characters of line

This command remove first 35 characters of each line. You can read :h regular-expression for more details about regular expression in vim.

Answered By: cuonglm

I think more in line with what you’re looking for is horizontal scrolling.

Z is the horizontal scrolling command key, followed by a direction to move with the left or right arrow key.

First :set nowrap to disable line wrapping. Then press z,35, to scroll 35 spaces.

Answered By: bahamat

You asked about how to hide the first letters, not to remove them, or scroll them out of sight – so here is how to actually hide them:

Hide text in vim using conceal

You can use matching, combined with syntax highlighting and the conceal feature to actually not show matched characters inside lines.

To hide the first 25 chars of each line:

:syn match Concealed '^.{25}' conceal
:set conceallevel=2

To hide only the lines with the punctuation of a date instead:

:syn match Concealed '^....-..-.. ..:..:....... ' conceal

To unhide:

:syn clear Concealed
:set conceallevel=0

What looks like this normally:

YYYY-MM-DD HH:MM:SS.USEC PID Name LogText
YYYY-MM-DD HH:MM:SS.USEC PID Name LogText
YYYY-MM-DD HH:MM:SS.USEC PID Name LogText
YYYY-MM-DD HH:MM:SS.USEC PID Name LogText
YYYY-MM-DD HH:MM:SS.USEC PID Name LogText
YYYY-MM-DD HH:MM:SS.USEC PID Name LogText
YYYY-MM-DD HH:MM:SS.USEC PID Name LogText

will look like this after executing the first two commands:

PID Name LogText
PID Name LogText
PID Name LogText
PID Name LogText
PID Name LogText
PID Name LogText
PID Name LogText

See also – inside vim:
help :syn-match
help :syn-conceal
help 'conceallevel'
help 'concealcursor'


(Let me know if it does not behave like that – there may be some more setting I’m not aware of or so – I’ll get it to work.)

Answered By: Volker Siegel

There is an easy to remember solution, without modifying buffers or files. Place your cursor on the first character (L on your example) of the file portion you want to see, then type

:set nowrap
zs

The zs command will setup vi so that that the character with the cursor appears at first column of the screen. Be sure that you didn’t type :set nosol in your configuration files.

The g$ command will move your cursor to the last visible column.

The g0 command will move back your cursor to the first visible column, the one containing L.

If any key moves the cursor to left or right out of the visible columns, then the visible windows will shift.

Answered By: user2987828
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.