How to see the last few keystrokes in vim? - vim

Sometimes I hit the wrong keys on my laptop's small keyboard, and odd navigation or editing occurs (sometimes a feature that's new to me).
Although I can undo the editing (u) or navigation (control-o), I don't know what I did wrong, so it's difficult to avoid it in future. It's also frustrating to not know what just happened. So, I would like to be able to see my last few keystrokes.
A bit like :set showcmd, but to show the literal keystrokes, and (ideally) a short history of them.
I've only found commandline and navigation history in help/google.
To clarify: I'm not looking for commandline history, but keystroke history. e.g. did I press ) accidentally and go to the next sentence? Did I press dd and delete a line? I guess it's similar to a keystroke logger.
Commandline history (:history) only shows commands entered at the : prompt.

A bit old, but I've just found this on reddit:
You can start vim with the -w flag to write all keystrokes in a file. See :h -w
vim -w filename

To my knowledge, the closest you can get is the q command, which records your keystrokes into a register of your choice. Obviously, that has to be set up a priori as it's intended for complex repeats, although you could probably hack something to start recording on every file open. There's the matter of memory usage and that annoying "recording" prompt though.
Probably the most straightforward way would be to install keylogging software. I don't have any personal experience with these, but the security implications are probably mild if you get it from a trustworthy source or build it yourself, set it to only log to memory not a disk, only have a buffer the size of a sentence or so, or only log for vim windows.

More recently I came across a logging plugin for Vim and it reminded me of this question:
http://wolever.net/~wolever/wiki/vim-logging
It basically records everything you do for later analysis, it was intended for getting statistics about command usage but should work perfectly for finding what those magic commands you accidentally entered were.

:history will show your command history - that should help you uncover the new features that we all uncover in vim with misplaced keystrokes.

Gundo - Is probably worth a look, whilst perhaps not exactly what you are looking it will help slightly.
It lets you visualise the Vim undo tree, this means you will be able to see the last edits that happened. For example if you accidentally deleted a line or some such this will show up in the tree, however, it doesn't show you the actual keystrokes that were pushed and will not show things such as cursor movements.

Related

Is there a way to view the current input source in vim?

When using vim in a non-English language environment, mistakes in mis-entering command often occur.
To prevent this, is there a way to view the current input source on the statusline?
For example, EN or JP.
like MS word bottom bar
Vim doesn't have that information so it can't expose it, which is not exactly a problem since your desktop environment already does.
If you really want to duplicate that information in your Vim status line, then you will have to find what external command to call, and then find a way to consume the information that doesn't slow Vim to a crawl because the status line is refreshed several times per second.
Now, your question is really two questions:
How to get the information?
How to show it in the statusline?
The answer to the first question depends on your desktop environment, which you didn't disclose. You will have to look around on your own, on more specialised forums.
The answer to the second question is irrelevant without an answer to the first one but you can look up :help 'statusline' and :help system() if you are curious.
Anyway, you already have the information in your desktop environment's menu bar/task bar/whatever, so why bother?
Vim has modes. In Normal mode where it is supposed to stay most of the time all commands consist of latin letters by default. Therefore, using OS level keyboard layout switch cannot be recommended. Instead, Vim has a feature called "keymap". Vim keymap is only active in Insert or Command-line mode never affecting the Normal mode.
So, Vim never cares what is your active keyboard layout on OS level and you won't expect an easy way showing it. On the other side, Vim keymap name is accessible either as b:keymap_name variable or %k format specifier for the statusline option. So it is pretty easy to add it.

Can vim command history store commands that produce an error?

I'm new to vim (for the seventh time) so this may not be something that makes sense for vim style but:
The command history in the command window has up-arrow mapped to history and this has been very helpful in learning, except that it does not seem to store commands that fail to execute in the history. Obviously, this is a bad configuration choice for someone trying to learn from mistakes. Is there a way to force it to remember the failed commands, in the sense that they can be recalled with the up-arrow?
FYI, I am using MacVim with Janus extensions.
What kind of error are you talking about? :s/foo/bar is always recorded into history, even if there was no foo on the current line and you get E486.
As a side note, if you use Ex commands a lot you might be interested in the Command line window, invoked by q:, that you can navigate and edit just like any other window. It's really great.
Also, drop Janus as soon as possible! That pile of crap is the absolute worst thing to install when learning to use Vim. Seriously.

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.

How not to give up on VIM?

I have started to do some programming using VIM.
I have very mixed feelings so far. On one side I do love the idea, on the other - it is just hard to remember everything.
So I took the approach of learning while actually doing some stuff (for Ruby on rails development).
Unfortunately there is no chance in hell for me to be more productive as in other "conventional" text editor for now. And it seems it will take quite a lot of time to get used to VIM.
I noticed, that I often don't use VIM navigation/search&replace abilities, but instead just move around as I would do in other editors.
I am trying hard pushing myself not to open anything in other editors except VIM so I can learn it.
But, honestly, yesterday I gave up and did my last 20 minutes of coding in GEdit.
UPDATE: I want to say why I gave - just because of I would finish what I need faster (it was veeeery late and it was not the best time for learning VIM). And indeed I did enjoy using VIM. But I always had the "there must a better way of doing this" feeling and spent a lot of time finding that way.
So my question wold be: how can I learn and start using VIM more productively from day to day provided that I want to do some real coding when learning?
Thanks,
Dmitriy.
Keep the following in mind. While there are physical limits to the speed of your fingers, there seem to be few limits on the processing that your brain can perform. Therefore, the time you invest in learning vi(m)'s keyboard editing commands and shortcuts will be paid back handsomely over time as the speed with which you edit improves breaking the physical speed limits you would encounter when using a traditional editor. For instance, to delete the next five words in vi(m) you type 5dw and to insert 50 * characters you type 50i* ESC.
You can begin using vi(m) after learning very few commands: basic movement, inserting, changing, deleting, opening a new line, and saving a document. Coupling these commands together produces powerful combinations. As you master these, you'll be looking for more.
Print a vi reference sheet (like this, or this or this more extensive list), and keep it near you at all times.
I use Gvim (the GUI mode). If you forgot the key sequence for some action you can invoke it through the GUI. For most actions you can also see the necessary keys, so that Gvim can also serve as a quick reference for Vim.
Off course a different text editor will not magically make you more productive. But if you like to use keyboard shortcuts on the mainstream editors, you'll like Vim because you can trigger fairly powerful actions with a few keystrokes.
I personally don't like Vim, I prefer mainstream editors. But Vim has REST syntax coloring, and I found it perfectly usable after about a week.
Why? Use whatever editor suits you best and makes you the most productive. I use vi for editing configuration files, because it's usually the quickest way to edit a few lines and then exit. For serious programming, it's either TextMate (ruby), Emacs (python or ruby on platforms without TextMate) or Xcode (objective-c).
Start using it when it makes sense : quick edits on config files, commit messages, README updates, etc...
The startup speed can difficult be beat.
When you get the hang of the basics, explore the help file if you think "there must be something in there for the task I need to do now". ...
Build the knowledge gradually.... step by step...
Until you find one day you do a lot of your editing in VIM or find that your toolchain is well integrated with Vim.
I personally use whichever works best, IntelliJ for Java, Emacs for clojure, Vim for perl and ruby scripts, ...
I'm more static typed languages guy and here is my story:
For me VIM was all about hjkl movement in normal mode + intert mode. I've found it so efficient that I wanted to have it all the time, everywhere.
Then I started to read cheatsheets from time to time and picking up the best gestures to remember (somehow sorted from most commonly useful to less useful ones): b, w, x, gj, gk, gg, G, numberg, *, #, %, f/t/F/T, /, >>, <<, =, v then mark with j/k, <</=/>>.
Then I started to write Makefiles to everything and configured Vim to impretet it. So I do :mak and I'm right at the line that error was found.
Then autocompletion happened (binded to TAB).
Then natural language checking z=.
Then I've written a wrappers for switching buffers. Opening them with :e filename.c and then doing Ctrl+l, Ctrl+h.
I have my config publicly available in git archive here:
http://github.com/dpc/vim-config
I am trying hard pushing myself not to open anything in other editors except VIM so I can learn it.
But, honestly, yesterday I gave up and did my last 20 minutes of coding in GEdit.
Developer should be comfortable with the environment he works with. That's why there are lots of editors developed by the developers for the developers.
As long as the editor does what you want, the way you want, it is all fine: editor is just a mean to do the work.
So my question wold be: how can I learn and start using VIM more productively from day to day provided that I want to do some real coding when learning?
For the VIM, unfortunately, my recommendation would be to spend several days with it without doing any real work, but simply learning. It took me about two days to get to know the basic functions required for the efficient editing. I knew that editor would play important role in my daily work that's why I have invested close to the week of my spare time to learn both VIM and Emacs.
My ex-colleague also kept a VIM cheat sheet as his desktop wallpaper. Helped in the beginning.
This might help: Why, oh WHY, do those #?#! nutheads use vi?
Anyone else looking to learn Vim should check out the Open Vim website. It is a fantastic resource for any newcomer to Vim. It has an interactive tutorial and various sandbox modes for playing with the editor. Have fun!
I've heard very good things about SwaroopCH's Byte Of Vim book. Haven't gotten around to reading it myself yet, but his Byte Of Python book is definitely excellent.
Learn at your rhythm. I think you should start mastering the basics:
modes: command, normal and visual
the commands: paste, yank, delete.
Then you can improve these knowledges:
learn some useful commands in command mode (list buffers, substitution)
learn to move faster (beginning/end of the word/line/file)
search/substitute a pattern
Look at people's vim config and customize yours
While doing this, always keep a vim cheat sheet near you. The basics commands are easy to remember (d for delete, p for paste, y for yank, i for insert, a for append, ...).
Learn progressively and stay simple.
How about books?
These ones are excellent:
Learning the vi and Vim Editors
VI Editor Pocket Reference
And after all, so what - so you don't work with vim. What is the big added value for vim, which worth the difficulty of learning it?
Here's some novel advice from Yehuda Katz, a core member of the Ruby on Rails team who recently switched to Vim: Try using it exactly the same as you would any other editor at first so you can keep being productive. Maybe this means continuing to use the arrow keys or *gasp* mouse at first. Don't try to learn all the Vim ways at once. Rather, let them come slowly and naturally.
The full article is a great read:
Everyone Who Tried to Convince Me to use Vim was Wrong
Additionally, try using vi key bindings in other applications. If your shell supports vi movement, use that. For web browsing, try the phenomenal Vimperator Firefox plugin.
Perhaps some Easter Eggs would help you get started on the right foot. Try the following and enjoy the wonderful world of open source tradition and legacy of contained silliness :)
:help 42
:help holy-grail
:help!
:help map-modes (see comment below the table about :nunmap)
:help UserGettingBored
:help spoon
:help showmatch (read the note)
:Ni!
(know more: visit vim.org)
vim takes a lot of time to get used to and actually be productive with
this is how i look at it:
suppose your productivity index is anywhere between 1 and 10
when you start using an other editor, your productivity index is like 6, and can go up to 8.
when you start using vim, your productivity is like 2, but you can go up to as high as 10. it just takes time.
Here is how I learnt when I switched from Windows to Linux:
1) I printed out the vim quick reference card(pdf) and kept it next to my keyboard at all times.
2) I started off with gvim and used the easy mode (gvim -y or evim). This makes vim behave like a regular editor - it is always in insert mode, and the keys are mapped to work like a regular CUA editor (e.g. CTRL-X/C/V for cut/copy/paste). You can still access all the vim functionality with CTRL-o to enter a single Vim command, then it will go back to insert mode.
After a while I got fed up with using CTRL-o or the menus all the time, and switched to proper Vim mode. I have not looked back since then and now use Vim for everything, even on Windows. I even use Vimperator on FireFox.
It is also worth taking a look at Cream - this is like vim easy mode on steroids.
If you find that you comfortable using evim or cream then there is no reason that you have to go all the way switch to normal vim mode, whatever vim purists may say. You should aim to become a master of your tools, not a slave to them.

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