Best way to view multiple logs at once? - linux

I am currently running zookeeper processes and multiple internal processes and they all print out to their own log files as text. I am pretty green to linux but I was wondering if viewing multiple log file in a single screen without switching between emacs windows or vim windows is an issue for other. What is the best way to view say 3, four or more log files at once? Would it involve the CAT or | commands?

If you are viewing live logs you can use tail with multiple files, or just tail an entire directory using the wildcard operator.
If you are digging though logs you can use Terminator, it is in the Debian repos, to open multiple terminal sessions in one window.
If you are feeling more adventurous your can use tmux to split your terminal window, the great thing about tmux is that is works in textmode, so you can do it over ssh. Here is a pretty decent tmux split pane tutorial http://lukaszwrobel.pl/blog/tmux-tutorial-split-terminal-windows-easily

i like multitail as an optional but nice way to monitor multiple files whithout a lot of hacking around. After installing (e.g. apt-get install multitail) run multitail file1.log file2.log file3.log. the 'f1' key gives you inline help which keys to press.
But, if you want to stay in vim you can use this answer:
One can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)
:set autoread | au CursorHold * checktime | call feedkeys("lh")
Explanation:
- autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
- CursorHold * checktime: when the cursor isn't moved by the user for the time specified in 'updatetime' (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
- call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)

It hit me out of nowhere... I should just import the log folder directory into an empty project in eclipse, then I can swiftly explore and inspect logs and split the screens as needed across multiple monitors. All of Leon's answer is good stuff too, but since I am already using Eclipse heavily I might as well take advantage of that locality.

Related

Return focus to vim when using screens plugin after ScreenShellFocus?

The screens plugin turns vim into a kind of dreampie, only its a whole lot better because you have more control over what gets send even when you have multiple buffers and specific text within those buffers.
To follow my environment setup, merely install screens with vim, and then at the command line, do screen, reach the shell, then type vim in. Edit a shell script of some kind with it, then do :ScreenShell *shell type here, eg python/bash/irb* which spawns a shell below. You can use :ScreenShellSend to send the visually selected text to the shell or all of the file if none is selected.
My problem is, say I want to restart the shell that I'm running using this plugin and sending it some text. I need to do :ScreenShellFocus, call exit, and then start it up again. But then I need to be able to return to the same vim session that is directly above the shell that I'm affecting. I realize that I can use screen to just reach the vim session, but the only way I can do that now is to select it with ^a 1, which really only replaces the lower portion, the one that supposed to be the shell, with the vim buffer being edited. This is stupid because now you have vim buffer on top of vim buffer.
So my question is, how do I return the cursor to the vim session above after doing :ScreenShellFocus?
(yes I know my example of killing and restarting a shell may be circumvented by other logic, but I'm not that acquainted with the GNU screen util, and I think there's a way to do it like this, which I think preserves the workflow of the programmer)
<C-a><Tab>
is the mapping used in screen to cycle through "regions". Since you have only two, it moves the focus to the other region.
Since you are effectively using screen, you should learn how to use it to make this plugin really useful.
$ man screen

Open vim tab in new (GUI) window?

I'd like to move the current tab into a new (visual/real) window in MacVim.
It's probably difficult, as:
there is nothing in the vim help and only very few - not helpful - hits on google
MacVim does not support it (link, 2009)
So I am wondering if someone has found a way to achieve this?
The closest I think you can come is using mksession
This will have several drawbacks (like, initially the secondary session will open a few more buffers than ultimately desired).
However, it will preserve your mappings, settings, history, window layout (if you had multiple windows inside the current tab, they will all get cloned).
If this idea tickles your fancy, you could look at creating a script that will filter parts out of the session file (which is Yust Another Vim Text Script)
:mksession! $HOME/detach.vim
:tabclose
:silent! !gvim remote --servername Detach -nR +'silent! source H:\detach.vim' +tabonly
save all current windows, mappings, settings (:he mksession)
close the tab we are about to detach
clone the session (detach.vim) into a remote vim
:silent! (try not to talk too much)
!gvim remote --servername Detach; launch a new remote Vim server if it doesn't yet exist, or talk to the existing server named Detach
-nR TODO Fix This is here to avoid the use of swapfiles (because I found no way to suppress the dreaded ATTENTION messages[1]). However, this may be unsafe advice depending on your situation, which is why I also include -R for read-only mode
+'silent! source H:\detach.vim' +tabonly -- In the remote vim, source the session to clone, and keep only the active tab (that was already closed in step 1.)
A little rough around the edges, for sure, but quite close to what you intended, I feel.
If you are comfortable, you can drop the -nR flags and just click through a few annoying swapfile attention messages (keyboard: Q).
[1] :he ATTENTION
Post-scripts:
on windows you might want to use :silent! !start /b gvim .... in case you have terminal windows sticking around
also on windows, you might get annoying behaviour if the resulting gvim window is too small. The :simalt ~x sequence is one hacky way to maximize the window (assuming English UI, because x is the accelerator for Maximize)
as icing on the cake, vim 7.3 has 'persistent undo'. See e.g. :se undofile
I don't think this is possible because, when you open a new instance of (g)vim you don't have access to the undo-history of the previous vim instance. All you can do is (in command mode):
:!gvim %
It will open the current file in a new instance of gvim. At least this is all I could think of. I may be wrong

Working with vim on multiple monitors

I have three monitors.
I normally run one maximized xterm on each monitor, attached to the same GNU screen session.
Can a similar model be used for vim? Is it possible to have three vims running, all sharing the same "vim session":
Each vim window showing a different vim tab
vim settings shared between all windows
Not getting the "Swap file ... already exists!" error message if I open the same file on two tabs.
I have a working solution where two vim instances communicate.
http://github.com/codeape2/vim-multiple-monitors
It uses the SwapExists autocmd to instruct the other instance to open a file if an existing swap file is detected.
You can use vim under screen.
$ screen
$ vim
# on another terminal
$ screen -x
# the same vim screen
If your terminal emulator supports tabbing (e.g. gnome-terminal), you can use it as a tabbing (IMHO, gnome-terminals' tabbing support is better than vim's own, except perhaps you can't copy and paste among different vim sessions, however you can instead use the system copy-paste buffer: "+y and "+p).
This does not work if you're using gvim though.
No, a Vim instance is limited to a single application window on your desktop. The different Vim instances have independent sessions.
In addition, be aware that if you open the same file in two different Vim instances, not only do you get the 'Swap file exists' message, but the two instances do not share a buffer, so changes made in one are independent of the other.
If a changed file is saved in one instance you will get a message when you return to the file in another Vim instance that 'the file has changed since editing started' and asking you if you want to reload the file (which would load changes as saved by the other instance, disregarding any changes you had made in the current instance).
Neovim is a project that aim to give vim attach/detach feature like tmux.
This feature isn't yet implemented (june 19th 2015), but this may come soon.
There is many other core features for this project that you can see at neovim.io.
:set noswapfile
:set autoread
Buffers aren't shared this way but vim reads file changes automatically if you save it. And you don't get that 'Swap file exists' message.

What's the best (Linux-compatible) buffer/session manager for Vim?

Is there a good project / session manager for vim? A session (or project) is a named lists of files, e.g. "bitonic_sort" could identify files "~/A/bitonic_sort.sk", "~/B/bitonic_sort.smt2", etc.
(rationale) I have a project where I need to edit files from many different locations, and it is too cumbersome to open them manually each time I resume work. (so, it looks like things like nerdtree brought up at this sister question, Favorite (G)Vim plugins/scripts?, won't work). I also need separate sessions (i.e. lists of files) for different projects, not just a recent document list.
After all documents have been loaded as buffers, any enhancements to switching between them is a plus (e.g. start typing a name, and matching documents are displayed). Thanks in advance.
Vim has built-in session manager. To save your current session use:
:mks session1.vim
This basically create a Vim script named session1.vim, which will restore your opened file if you source it or start Vim like this:
vim -S session1.vim
To overwrite your saved sessions, use :mks! your_saved_session.vim. Combine with a custom key map and this will be the solution. For more about Vim session read :help :mks. Vim also has views manager which is quite similar. Read more from: :help :mkview
For switching between buffers, you can use FuzzyFinder; but I prefer this key map:
nmap <C-tab> :bn<CR>
imap <C-tab> <ESC>:bn<CR>i
Add it to .vimrc and I can use Ctrl + Tab to switch between buffers just like Firefox tabs. Hope this help.
I'm not sure exactly what you're asking for... but if you want to turn on tab-completion when opening files in vim, add this to your ~/.vimrc:
" Auto-complete file names after <TAB> like bash does.
set wildmode=longest,list
set wildignore=.svn,CVS,*.swp
Also, take a look at screen. From the man page:
When screen is called, it creates a single window with a shell in it (or the specified command) and
then gets out of your way so that you can use the program as you normally would. Then, at any time,
you can create new (full-screen) windows with other programs in them (including more shells), kill
existing windows, view a list of windows, turn output logging on and off, copy-and-paste text between
windows, view the scrollback history, switch between windows in whatever manner you wish, etc. All
windows run their programs completely independent of each other. Programs continue to run when their
window is currently not visible and even when the whole screen session is detached from the user's
terminal. When a program terminates, screen (per default) kills the window that contained it. If
this window was in the foreground, the display switches to the previous window; if none are left,
screen exits.
It's pretty much like having several xterms open, except unlike graphical xterms you can access your screen session if you access your machine remotely (e.g. by sshing to it). You could leave up several different instances of vim in separate screens with all the files you want open, and just never exit them.
The very basic setup I use is one vim window, one compile window, and one testing/debugging window.
And since we're talking about vim, check out this post: Post your Vim config. Lots of cool tweaks and spiffy stuff in there.
You can try the vim-workspace plugin, its session management features are automated and relatively simple (compared to vim-session): https://github.com/thaerkh/vim-workspace

Using VIM as a logfile-viewer

I'd like to use VIM as a logfile-viewer. Is it possible to reload the current file in a regular time interval (~1s)?
use :set autoread
See this VIM tip. It offers tailing (like tail -f) together with log line numbering
I like it short and without a lot of hacking or external scripts.
You can run this oneliner from ex (whithin vim) when needed (or put each command in vimrc, for when log-files are opened.)
:set autoread | au CursorHold * checktime | call feedkeys("lh")
(if you would want to jump (nearly) to the end of the file, just use "G" instead of "lh" with feedkeys)
Explanation:
autoread: reads the file when changed from the outside (but it doesnt work on its own, there is no internal timer or something like that. It will only read the file when vim does an action, like a command in ex :!
CursorHold * checktime: when the cursor isn't moved by the user for the time specified in updatetime (which is 4000 miliseconds by default) checktime is executed, which checks for changes from outside the file
call feedkeys("lh"): the cursor is moved once, right and back left. and then nothing happens (... which means, that CursorHold is triggered, which means we have a loop)
To stop the scrolling when using call feedkeys("G"), execute :set noautoread - now vim will tell, that the file was change ans ask if one wants to read the changes or not)
I like the idea to watch logfiles in vim (instead of tail -f), e.g. when you are working in an ssh session without screen/tmux. Additionally you can copy directly from the logfile, if needed, or save the output directly or ... whatever you can do with vim :)
*from this answer (refering to an answer by PhanHaiQuang and a comment by flukus)
With the timers in Vim 8 this can now much simpler and less of a kludge. For example:
:set autoread
:function! Tailf(id)
: checkt
: $
:endfunc
:let timer_id = timer_start(4000, 'Tailf', {"repeat":-1})
This has vim rereading the whole log every 4 seconds. For many purposes that's fine, but would not be for large, say several GB, log files. There's other capabilities, like jobs and channels, that could be used for more sophisticated log reading.
asyncrun.vim lets you run programs and see live output in the quickfix (essentially JohnLittle's answer, but with setqflist() instead of Tailf()). You can use it with tail (I'm on Windows and my tail comes from my git install):
AsyncRun tail -f C:\logs\plugin-info.log
It has the additional benefit of using 'errorformat' to parse filenames out of the logs.

Resources