Keep vim always in command line mode with a ":" - vim

Is there a way to make vim stuck in command mode with a : already typed in?
In that way, for instance:
I would type /fooEnter and the cursor would go to the beginning of the next line containing foo.
Next, I would be still on command line mode with a : already typed in for the next command.

Yes, start it in Ex mode, by invoking it either as ex or as vi -e.
You can also enter Ex mode from the normal visual mode by typing Q (must be upper case).
You can return from Ex mode to normal visual mode by using the vi command.
EDIT : This doesn't actually do what the OP is looking for. He wants to keep the visual display while keeping the cursor on the bottom command line. That may not be possible.

No, but you can map ; to : to put yourself "closer" to command mode.
I'll link to the Vim wiki instead of reposting identical information here.
http://vim.wikia.com/wiki/Map_semicolon_to_colon

You can build your own REPL, like this:
:while 1 | execute input(':') | redraw | endwhile
This is just a conceptual demo; you probably want to add a condition to quit this special mode. Also, commands like :append would need special handling to work properly.

As a last try, I could just initialize vim with -servername=FOO and then code a little script that would read from stdin and send remote-send to FOO whenever it detects(by parsing) a whole command was typed on stdin.
Then I would just use Vim and this other script side by side on different xterms/gnu screens.
EDIT
OK, I will use this one. This way I can even make :a command to enter vim's Insert mode and switch back to command mode when entering a line with a single .. This way I would also have syntax highlight on the fly when inserting text (you know, vim has a very pretty visual display of the text, I'm just too used with ed's interface). When I have so time I'll write this script and link it here.

Related

Vim :'<,'> When entering command mode

Sometimes in vim I appear to be entering a keymap unintentionally when attempting to enter command mode. For instance, when attempting to write :w I, sometimes, end up with this:
:'<,'>w
Which throws the error E481: No Range Allowed
It's mostly just a minor annoyance, and I'm more wanting to know what am I doing to initiate the command line in this way with the brackets.
:'<,'>w appears when you start a command line while being in visual mode. It allows to apply this command on a portion of your document, e.g. to sort some lines. In your case, you have accidentally hit v before entering your command.
Adding to Vincent's correct answer, if you happen to come upon a command that doesn't support a range and gives you the E481 error (though the given :write does support ranges), you can just remove the '<,'> prefilled content by pressing Ctrl + U, and then start typing the command. This is quicker than Esc and re-triggering command-line mode via :.

Why some commands in vim require a colon while some don't?

Some of the commands in vim are given by first typing a colon (:) eg . :wq for saving a file and quitting . While some of the commands don't require a colon for example the Replace command (R). I want to know what is the difference between the two approaches ? Is there any specific rule as to which ones require a colon and which ones don't ?
You have to look into the history of vi, the predecessor of Vim, for an explanation. A long time ago, when text editing had to be done with a keyboard and attached printer (called a terminal), there was no mouse, no display other than the paper, and therefore, little interactivity. Editing consisted of short, mnemonic commands via an editor called ex. You issued a command addressing one or several lines (e.g. :substitute/foo/bar), and the editor obeyed. In case you were not sure about the command's effects, you could :print some lines.
Time passed, video terminals appeared, and the vi editor incorporated the ex commands (because they were useful and the programmers were used to them), but introduced more interactive commands like delete (x), insert (i), and so on. The ex commands are still available in command-line mode, which is started with :, and concluded with Enter.
Vi and Vim are special in this regard, because they have these different modes where the same keys mean different things depending on which mode one is in. To become proficient in Vim, you have to learn about the different modes, and how to best use them to achieve your editing goals.
:help vim-modes gives you a starting point into the excellent and comprehensive help facilities.
The commands that "don't require" a colon are called "normal (mode) commands".
The commands that "require" a colon are called "Ex commands".
Vim, being a modal editor, has many commands that are contextual to the mode you are in. The most obvious effect is that hitting the same key in different contexts may produce different results.
In insert mode, most keys on your keyboard are used to actually input characters into your document.
You have to switch to normal mode to yank, put, delete, move your cursor around… normal mode is where you do the laser-focused editing Vim is famous for and use commands like dcggsi/.* and so on.
You enter command-line mode by hitting : in normal/*visual* mode. It is typically used for two things:
perform administrative tasks (writing to disk, switching buffers, opening files…)
use cool editing commands like :m10 or :t1 or :g/foo/d…
The many commands that you can use in this mode are (very powerful) remnants of Vim's past and are called Ex commands.
In short, neither normal mode commands nor Ex commands start with a colon. The colon is simply used to change modes.
You are in different modes of vim. There are 6 basic modes in vim. They are
Normal mode
Visual mode
Select mode
Insert mode
cmdLine mode
Ex mode
In Normal mode you don't require to type :, this mode can be reached by pressing Esc.
The most important difference (IMHO) is that the colon commands are treated as words, not individual letters, and they aren't interpreted until you hit enter.
Commands in normal mode are treated as individual letters, each letter has a defined meaning, and they are executed as soon as vim knows what to do with them.
For instance, typing 'de' in normal mode, will delete everything up until the end of the word. But 'de' isn't really a command; it's two commands. "e" moves you to the end of a word, and "d" means "delete something; wait for the next key press to tell you how much to delete." When you type 'd', it waits for you to complete the instruction. As soon as you press 'e', the command executes (without waiting to see if you want to type anything more).
That means in normal mode, you can't have an instruction like "define" or "deliver" or "describe". You'd get as far as typing 'de', and vim would execute the 'de' instruction. By the time you got to the 'f' in define, you'd be starting a new instruction.
But in command line (colon) mode, the commands are treated as words. There could be a command called 'define' and another command called 'describe'. (There isn't, but there could be).
'w' could have been created as a normal mode command, if all it did was save the current file under the current filename. But ':w' does more than that. You can also do ":w new_filename", and you get a 'save as' function. That wouldn't work in normal mode. In normal mode "w new_filename" would save the file ('w'), do nothing (space), try to find the next search term ('n'), move to the next end-of-word position ('e')... and so on.

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.

vim command line completion when opening files

How to make VIM to always auto-complete filenames in command mode? It works fine when I type for example ":cd /ww[Tab]", but if I want to open a file and type ":o /ww[Tab]", it inserts "^I" character instead of completing.
Try:
:e /ww[Tab]
Use ":e" or ":split" or other edit commands instead of ":o".
Bonus fact: vim doesn't really support the ":o" command, at least not according to the docs. ":help :o" says this:
This command is in Vi, but Vim only simulates it:
*:o* *:op* *:open* :[range]o[pen]
Works like |:visual|: end Ex mode.
{Vi: start editing in open mode}
:[range]o[pen] /pattern/ As above, additionally move the cursor to the
column where "pattern" matches in the cursor
line.
Vim does not support open mode, since it's not really useful.
For those situations where ":open" would start open mode Vim will
leave Ex mode, which allows executing the same commands, but updates
the whole screen instead of only one line.

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