How to know commands I've been typing? - vim

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

Related

How to stop these Vim errors?

It seems like almost every time I open a file in Vim anymore, I'll get an error like this:
Error detected while processing /Users/me/.vim/view/=+something=+something=+something...
E518: Unknown option: nomacmeta
The option is usually something different depending on the file. I've done :help view but the results weren't very helpful. Is there some way I can disable this view feature so I can stop getting these errors? I don't have time to run down every one of them.
:help 'macmeta' is an option specific to MacVim. You are having this error because you are sourcing a view script that contains the command set nomacmeta in a Vim that is not MacVim and thus doesn't support that option.
Either…
you are on a Mac but you are using the built-in Vim at /usr/bin/vim, which is not MacVim,
or you are on a Mac but you are using a manually built Vim, which is very likely to not be MacVim,
or you are on some other system where Vim is pretty much guaranteed to not be MacVim.
To prevent those errors, remove any non-cross-platform option from your view scripts and consider reading :help :mkview carefully, specially the part about :help 'viewoptions'.
Also, &macmeta is disabled by default so disabling it doesn't make much sense to begin with.

how to clear the screen after exit vim

Sometimes vim will leave something, i.e. press any key to continue, on the terminal and is there any way to return to a clear terminal after exiting vim?
I am new to vim and please tell me exactly what I should do.
Sorry I did not express my idea clear enough the first time. What I actually want to ask is that is there a way to return to a clear terminal after typing :q in vim without further input of commands.
I am using VIM 7.4 in Ubuntu, terminal type is xterm.
Add to your ~/.vimrc:
au VimLeave * :!clear
I use an alias to clear on any exit method including :q, :wqa. (this is for my osx brewed binary, find your own vim binary with which vim)
# .zshrc
alias vim="/usr/local/Cellar/vim/8.0.0094/bin/vim && clear"
There is a way to do more stuffs by editing your .vimrc file
Add this to your .vimrc
command Qc :call ClearAndExit()
function ClearAndExit()
:!clear
:q!
endfunction
use :Qc to quit.... it will clear the screen as well
If Vim is compiled with support for switching xterm-screens, it can do this by default, if you set the t_ti and t_te (Vim usually figures out, to what values this needs to be set by itsself). The gory details are explained at :h xterm-screens (pasted below)
(From comp.editors, by Juergen Weigert, in reply to a question)
:> Another question is that after exiting vim, the screen is left as
it :> was, i.e. the contents of the file I was viewing (editing) was
left on :> the screen. The output from my previous like "ls" were
lost, :> ie. no longer in the scrolling buffer. I know that there is a
way to :> restore the screen after exiting vim or other vi like
editors, :> I just don't know how. Helps are appreciated. Thanks. : :I
imagine someone else can answer this. I assume though that vim and vi
do :the same thing as each other for a given xterm setup.
They not necessarily do the same thing, as this may be a termcap vs.
terminfo problem. You should be aware that there are two databases
for describing attributes of a particular type of terminal: termcap
and terminfo. This can cause differences when the entries differ AND
when of the programs in question one uses terminfo and the other uses
termcap (also see +terminfo).
In your particular problem, you are looking for the control sequences
^[[?47h and ^[[?47l. These switch between xterms alternate and main
screen buffer. As a quick workaround a command sequence like
echo -n "^[[?47h"; vim ... ; echo -n "^[[?47l" may do what you want. (My notation ^[ means the ESC character, further down you'll
see that the databases use \E instead).
On startup, vim echoes the value of the termcap variable ti (terminfo:
smcup) to the terminal. When exiting, it echoes te (terminfo: rmcup).
Thus these two variables are the correct place where the above
mentioned control sequences should go.
Compare your xterm termcap entry (found in /etc/termcap) with your
xterm terminfo entry (retrieved with "infocmp -C xterm"). Both should
contain entries similar to:
:te=\E[2J\E[?47l\E8:ti=\E7\E[?47h:
PS: If you find any difference, someone (your sysadmin?) should better
check
the complete termcap and terminfo database for consistency.
NOTE 1: If you recompile Vim with FEAT_XTERM_SAVE defined in
feature.h, the builtin xterm will include the mentioned "te" and "ti"
entries.
NOTE 2: If you want to disable the screen switching, and you don't
want to change your termcap, you can add these lines to your .vimrc:
:set t_ti= t_te=
Yes. You most certainly can.
Use the UNIX command to clear the screen.
clear
I didn't want to have to use a different command to exit Vim (e.g. :Qc as suggested) by manoj, and the EntangledLoops' .vimrc method didn't work for me.
Inspired by Plato's answer, I found a similar solution by putting the following function in .bashrc:
# Vim exits to clear terminal screen
function vim {
/usr/bin/env vim "$#" && clear
}
Using only vim inside the function was problematic due to the function calling itself recursively and creating an infinite loop, but adding /usr/bin/env eliminates this issue, ignoring the function and executing the first vim in the PATH.

What is the difference between gVim and gVim easy?

The question is self explanatory, but I haven't found a single resource that explains what the difference is after an hour of searching. After poking around a little bit in both, it appears that gVim and gVim easy are identical.
Gvim easy is started and locked in insert-mode (every character you type is printed like a simple notepad).
Standard gvim starts in normal mode and you have to toggle between normal/insert like all other vim.
From help:
Easy mode. Implied for |evim| and |eview|. Starts with
'insertmode' set and behaves like a click-and-type editor.
This sources the script $VIMRUNTIME/evim.vim. Mappings are
set up to work like most click-and-type editors, see
|evim-keys|. The GUI is started when available.
{not in Vi}
gVim-Easy, which is installed with gVim, has all the functionality of
normal gVim but lacks modes. This is especially useful for begginers
and people who do not want to, or do not have the time to, learn how
to use gVim. Users can benefit from gVim's superior syntax
highlighting and auto-indentation while not having to have to learn
the, often deemed complex, command set of gVim in order to edit a
simple document. It is recommended that readers of this tutorial at
least try to learn how to use gVim in normal mode, the learning curve
is steep, but, the benefits in speed and usability this confers is
worth the investment.
Read here
gVim Easy: gvim.exe -y (pass a parameter y)
gVim: gvim.exe
It seems that Insert and Replace are available modes, and command mode is missing. And at first glance it appears to have lost the ability to save a document using keystrokes - the File menu shows only ":w" for Save and without command mode, the command simply isn't there. What isn't shown however is Alt-F followed by S - this works, although also missing is the status line confirmation of the file being written.

Vim "show my last command" command?

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.

Readline's vi-mode in vim ex mode

Let's see if I can explain myself.
I use vi-mode in bash, which is really great since I'm used to Vi.
When I'm inside vim and type : (to go to ex mode), since I'm used to the vi-mode from bash, I feel the slowliness of having to use this mode like the "regular" way of using bash.
Question is: is there a way of using vim's ex-mode like bash's (or readline) vi-mode?
Not sure if I understand what you're trying to do, but it might be something like hitting q: in normal mode?
For users that use Vim or vi bindings almost everywhere, including on their shell command line, it really hurts when you leave that environment. If you're used to the vi bindings hyperdrive, going back to chords for skipping words and other manoeuvres is painful and slow. Operating systems also differ on their default bindings so Mac, for instance, supports option-arrow instead of control-arrow, adding to the pain.
But there is one place where this also happens where it's really upsetting: in Vim itself. When working in Vim and entering command mode using : the default readline editing returns. Chords all over again. How to fix this?
Simple: When in "normal" mode, that is, when navigating around, type q:
Vim will drop you at the bottom of a full Vim full screen editing experience, go for your life
Additionally the command history is available on previous lines in the buffer
You can yank and paste lines and edit the commands as much as you wish
To execute a command in "command" or "ex" mode just hit ENTER on the line you want to execute
Hitting enter on an empty line closes the buffer and does nothing
But this is just another buffer so you can quit it as usual with :q as well
Although ESC leaves the "ex" command line, ESC in the buffer will not leave the buffer, because it's an actual buffer
The q prefix is used to introduce macro recording, so the q: variant is perfectly mnemonic for entering recording of an "ex" command line.
Note that q: to enter the buffer editing mode is very similar to :q ! You may have hit that by accident sometimes ;-) Now you know how to get out of it!
Zigdon had this answer a long time ago, of course, but it's pretty darn sparse, but then again, so is the question. If Zigdon adds this extra detail to his answer I'll be happy to delete this answer so that there can be one good answer.

Resources