vim command line editting - vim

is there a way to use vim/vi in the vim command line? Sometimes I write a long
command in vim such as:
:!./script /home/user/pet --flag=1
and I want to change for instance "user" by "other". What I usually do is to
navigate the command line with right arrow which is time consuming and even more
when I want to go to the beginning of the line. I would like to have something
like "0" to go there or w/b to move by words. Or use j/k to go to the next/previous
command.
Thanks.

:h cedit
in command line, type ctrl-F(default) to enter command window.
or in normal mode type q:
(for search, type q/)

Vim has a feature called the "commandline window". You can enter it with Control-F by default when you're already on the commandline, or q: from normal mode, edit the commandline using vim commands, and press enter to execute. It also contains your command history so that you can yank previous commands if you like. See :help cmdline-window for more information.

I'm not aware how you can use Vim commands to edit a command directly on the command line, but if you enter the command window q: you get can use regular Vim editing to edit commands.
From there you can execute commands by hitting <CR> or use Ctrl-C to copy the command to the regular command line.

If you run set -o vi you will have vim capabilities in your command line. Just put 'set -o vi' in your .bashrc file or equivalent to have it by default.

Related

Vim Running Terminal Command Automatically

I am trying to write a command (in vim rc) that automatically creates a new split and starts a terminal (i.e by executing :Term) and then automatically executing some bash commands on that terminal (specifically 'conda activate')
This is how far I got:
command CustomTerminal execute "vsplit ." <bar> execute "Term"
How to I extended this command such that, it pipes and executes some bash commands on this terminal?
I see the following issues with your command definition:
you don't need :execute here; also <Bar> is only needed in mappings
the vsplit . creates a directory listing; is that intended?
have you defined a custom :Term command? The build-in command to open a terminal is :terminal
:term performs a (horizontal) split on its own; its :help :terminal mentions
If you want to split the window vertically, use:
:vertical terminal
The :terminal command accepts optional shell commands already. If that's what you need, you can easily extend your custom command to take and pass this argument:
:command -nargs=? CustomTerminal vertical terminal <args>
See :help :command-nargs and :help <args>.
Additionally, you can add :help :command-completion via -complete=shellcmd.
Keeping the terminal and feeding commands to it
If you want to run more than one command and then close the terminal, so to reuse a single terminal session, you have to follow :help terminal-to-job to send commands from Vim to the terminal (received by the interactive shell or whatever application currently is running). It looks like this:
call term_sendkeys(buf, "ls *.java\<CR>")

Vim: executing a visual command on startup

I am aware I can select an entire line in Vim in visual mode by pressing Shift+V. Now, can I achieve the same (i. e. placing the cursor to the specific line within a file and selecting this very line) from the shell, using only Vim command line options?
Currently, the best I have is
vim +[line] -c 'set cursorline' [file]
but this is not exactly what I want.
Use the normal command normal to run commands in normal mode.
vim +[line] -c 'normal! V' [file]
Take a look at :h normal

Re-execute command by :history option in VIM

How can you execute a command again listed in the
:history
option in vim. There are numbers shown. Is the only way to copy the command by hand, then re-enter it? Or is there something like in shell script.
:history is only there for you to look at it.
To re-execute a previous command, you have two options:
Use <Up> and <Down> at the command prompt:
:m10
(do stuff)
:<Up>
Use the "command-line window":
You can call it with q: and navigate with search and use the beautiful normal mode commands we all love.
Position the cursor on a line and hit <CR> to re-execute the command.
Edit a command and hit <CR> to execute the new command.
You can quit the command-line window with :q.
See :help cmdline-window.
I use q: shortcut in normal mode to open interactive command history window. You just move to the right command and execute it by pressing Enter. You can find more information and other ways of accessing history here.
What I like to do is type the first few characters in the command and press <UP>. For example if you want to repeat an edit command of the file file.txt you could:
:e fil<UP><ENTER>
If the first <UP> does not give you the command you want, keep pressing<UP> until you find the command you were looking for.
If Vim is compiled with +eval you can use histget( {history} [, {index}])
:exe histget('c', 15)
That isn't exactly convenient to type, so you can also create a user-defined command:
:com -nargs=1 HI exe histget('c', <args>)
Thereafter you can use HI {index} to execute the history entry:
:HI 15

How do I get a list of the history of all the vim commands I ran?

I'm aware that much like a bash prompt in Vim you can press : and get a list of all the history commands by pressing up and down.
But is there a way to export this command history into a buffer?
:history will show you a history of your commands.
Edit:
In normal mode, q: will open your history in a new buffer.
you can check all the commands by looking into .viminfo in your home folder or :history inside vim

ESC doesn't work in cygwin vim

I installed cygwin on Windows 7. When I start vim in cygwin terminal it starts in interactive mode. I can't change mode to command one by pressing ESC. What could be the reason?
UPDATE:
Also vim prints these varnings at start:
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
If by interactive mode you mean insert mode (where keypresses are inserted as text, just as in other editors), then your Vim is in easy mode.
In it, you can temporarily execute normal mode commands via Ctrl + O. But I guess you don't want this strange beginner's mode. To turn it off, check whether Vim has been invoked with the -y argument or as evim (is there a shell alias?). Or, if you find a :set insertmode command in a .vimrc, remove it. (By default, at least in my Cygwin installations, Vim is not configured for easy mode, so it must be something in your configuration.)
Try to
Press “ESC” and "shift" and ":" together;
You should find the place that you can type command line in vim;

Resources