Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ?

share|improve this question
    
I don't know how you do it in vim, but if you don't mind creating a temporary file, you could run cut -c36- logfile > logfile_with_first_35_chars_missing, and then review that. –  Warwick Jul 28 at 23:29

4 Answers 4

up vote 3 down vote accepted

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 it:

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.)

share|improve this answer
    
Works fine, I think vim needs to be compiled with +conceal (mine is) option for this to work. Also good to do set concealcursor=nc to prevent the cursor line from expanding. –  user881300 Jul 30 at 19:34
    
Yes, the +conceal feature option is standard like some years. Sad that it's not more well-known by now. It's really great and flexible. Gives lots of new options... –  Volker Siegel Jul 30 at 20:14

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.

share|improve this answer
    
And then use %!cat % to get it back ? This works great and I can use a map to toggle between these two –  user881300 Jul 29 at 0:11
    
Yes, that will work, as long as you have a real file backing your buffer. –  Michael Homer Jul 29 at 0:14
3  
@Michael Homer: This will now work with unicode characters. –  Gnouc Jul 29 at 2:03

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.

share|improve this answer
    
This is very useful when reading logs, thank you! –  Fabio F. Jul 29 at 9:43

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.