Is it possible to log all commands I type in VIM for later analyzing? I mean each simple command like motions or changing the text, i.e. jjjjjjkkkcw<newword>
If it is not possible in VIM, maybe there is a keylogger on linux, which can be attached to specific window/process?
I'd prefer in-vim logging, because it could have options to have different logs for different vim modes. Also I don't want to log "colon" commands.
Yes, there is! When launching vim use vim -W ~/vimcommands.log to >> to a file, or -w to overwrite the file.
-w {scriptout}
All the characters that you type are recorded in the file {scriptout}, until you exit Vim.
This is useful if you want to create a script file to be used with "vim -s" or ":source!".
If the {scriptout} file exists, characters are appended.
-W {scriptout}
Like -w, but an existing file is overwritten.
You may want to add a bash alias to store vim logs based on file name. I am interested to see how you intend to analyse your logs, I would like to do the same.
Why not just start recording a macro (qa for example will start recording a macro in a), and it will record them all for you?
Ctrl-R a
in insert mode will let you view its contents.
Related
I use aliases with vim to edit large collections of files. This looks something like:
whole_lotta_vim *.c
However, this alias fills up vim's oldfiles list with a bunch of useless crud. Is there a way to prevent vim from doing this?
Starting Vim with -i NONE disables the use of the :help viminfo-file. This means that Vim won't store marks for any of the file you open, which is what you want, but it also means that it won't be able to access anything stored in the viminfo file, like command history and so on, which may be a compromise you are not willing to make.
I have a text that's around 1000 lines long, and I have a bunch of vim commands I want to apply to it. Lots of regex find-and-replace stuff. For example:
:%s/^[^\$\$]/def
:%s/\$\$/
%s/='/ = ['
I could copy and paste those commands one by one, but that's work I'll have to do again every time I receive a new version of this file. I'm wondering if there's a way to save a long list of commands that I can then apply to the file? Thanks!
registers
You can put them all into a register, either by typing those commands in a scratch buffer and then yanking into a register, or via explicit assignment:
:let #a = "%s///\n%s///"
Then, execute via :#a
Depending on your Vim configuration, the register contents may persist across sessions (cp. :help viminfo), but you must be careful to not override them.
scripts
A longer-lasting and more scalable solution is putting the commands into a separate script file, ideally with a .vim extension. You can put them anywhere (except ~/.vim/plugin, unless you want them executed automatically), and then run them via
:source /path/to/script.vim
custom commands
If you need a set of commands very often, you can give them a name and turn them into a custom command:
:command! MyCommands %s/// | %s///
Put this into your ~/.vimrc, or (if it's filetype-specific), make it a :help ftplugin.
Normally, you can run vim command from the shell using
vim -c YourCommandHere
I tried vim/less.sh as pager with syntax highlighting, but there is a little issue:
when displaying small file, vim uses 'full screen' and waits for a command from user.
Can I let vim to act like a less --quit-at-eof?
In other words, is there a way to automatically quit vim if displayed file is several lines length?
I found one solution with a shell script: count file lines by wc -l, then get terminal height, if size is small - use custom vim config file, where custom config file ending with :quit string.
However, this solution looks terrible and leave extra lines with ~ after end of small file, so I'm looking for better way to do this.
Vim switches to the alternate terminal page, so when you exit it, its contents are gone. Even if you turn that off:
$ vim --cmd 'set t_ti= t_te='
UI stuff like the ~, ruler and statusline would remain, too. Therefore, the solution you've found looks like a reasonable workaround. Either use that or (better) get used to quitting the Vim pager.
I'm using gVim and I would like to know if there is a way to see the commands I've been typing.
For example, when I pressed the visual mode (v) I've got message -- Visual --, but I don't know which letters I've been pressing so far.
Is there a way to permanent see which characters/commands I've typing?
You can use this setting:
:set showcmd
Type :help 'showcmd' to read more.
You could set this up:
alias vim="vim -W ~/.last_vim_session_key_pressed"
But this file is written only when you exit vim. You can source it with vim -s but beware, with vim gui versions you can have problems.
Check your home directory for a .viminfo file.
This will have, among other things, a history from newest to oldest of recent commands you've typed.
There is a tricky way to show all vim keystrokes which were pressed by using -w parameter which record all the characters that you type in the file. The problem is, that vim writes keystrokes only when you exit Vim as Benoit already said.
To workaround this, Kana Natsuno came up with this single-line patch, which disables buffering of the -w option, so you have access to realtime stream of keystrokes. Then it's a matter of reading them (e.g. tail -f), parsing or you can try to display them in the statusbar (:set statusline).
Check out a custom build of Vim using Drew's live-stream-keystrokes branch of MacVim, to get the realtime stream of keystrokes.
Source: Vimprint - a Vim keystroke parser at Drew Neil blog
This is useful if you'd like to reveal the Vim pressed keystrokes in live video tutorials (or GIFs).
Is there a command which shows what was the last command in normal mode?
Suppose I accidently hit random key and got some unexpected result.
Sure I can undo it, but could I reveal what key was pressed and how it was interpreted?
Hit the colon (:) and then use the up arrow to start going back through previous commands. You can use the up/down arrows too to move around the list.
q: will show you command history in Vim.
q/ will show you history of searches.
And must importantly, :q will quit the mode.
The text from the last command is stored in the . register. You can see all registers by :display. Unfortunately it doesn't say what the started the normal command.
To see commands from : (command mode) you can use :hist or q: which is limited to the last 20 (by default).
Another ability is to save the undo buffer :wundo undo.bin -- but the undo buffer is binary.
But none of these actually answer your question. I'm curious if it can be done.
Entering colon : then ctrl+p shows your previous command, i.e., moving backward through your vim command history. ctrl+n moves forward.
This is very convenient if you're used to using the command line and prefer not to change your keyboard hand positioning to use arrow keys.
It is difficult to know it. You can play with the variables:
v:operator
v:count (and v:prevcount)
v:register
But you cannot fully get the last normal mode command issued.
However if you want to systematically record everything you type while in Vim, you can launch vim -W ~/.vim-last-scriptout (a Windows version: vim -W "%HOMEPATH%\Vim\.last-scriptout) You can alias it in your shell on a UNIX machine. Every single key, or control-key, will be recorded into that file. Note that if you happen to use gvim or vim -g (the GUI) you might encounter this bug.
If you want to replay this file you can use :source! (with the exclamation mark) or the -s option from the command line.
On Windows I have set gvimportable.exe -W gvim_directory\last_scriptout as my default editor in my Commander program (FreeCommander). This way I can always remember what I have typed to do something and repeat a sequence of commands on another file. Of course I have another shortcut for opening Vim and playing the scriptout.
Note that the file might be written only when Vim exits, so you have to lose your session to know what you've done.