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>")
Related
In my init.vim for Neovim, I have the same line as in my .vimrc in Vim, which, when pressing F12, runs the file currently in the buffer using the python3 interpreter:
autocmd FileType python nnoremap <silent> <F12> :!clear;python3 %<CR>
Now I'm trying to run this tiny "test.py" script by pressing F12 in normal mode:
import IPython
IPython.embed()
Works fine in Vim:
But doesn't work in neovim despite exactly the same line in my ~/config/nvim/init.vim:
So it does run IPython, but then immediately (red arrow) inexplicably asks if I want to exit. It's also got a bunch of weird escape sequences inserted (yellow arrow) which I suspect are the reason why it wants to exit, and which don't appear with normal vim.
I don't really like the internal neovim terminal, so how can I get neovim to behave exactly like vim in this case?
This is a known limitation of NeoVim, :! is non-interactive and it will not allocate a pseudo-terminal which is typically required for full-screen applications such as IPython to run properly.
See issue #1496 for details.
An alternative is to use NeoVim's (or Vim 8's) support for terminal, with the :terminal command, or with a function such aa termopen() (in NeoVim) or term_start() (in Vim 8) to run full-screen applications such as IPython.
In your case, something as simple as :term python3 %, running the command in a terminal in a split, might be an adequate replacement.
You might also find the vim-bang-terminal plug-in interesting. It replaces a :! command with a similar command invocation that runs inside a Vim/NeoVim terminal instead.
How can I hide the output of any command in the form :!command in Vim. I don't want to get any messages from Vim after running this command (like Press ENTER or type command to continue) also. I want shell commands to run in background completely.
"I want shell commands to run in background completely." --- This is not about Vim. It is about how to run commands in background.
To not print any output, you can run command like this: yourCmd >/dev/null 2>&1, if it doesn't work, use nohup (https://www.computerhope.com/unix/unohup.htm).
To not print Press ENTER or type command to continue, you can execute commands in Vim like this:
:silent !<command>
silent blank-issue:
https://vi.stackexchange.com/questions/2809/silent-makes-my-vim-go-blank
Vim grep causes characters to temporarily disappear
As explained here, you can also do:
:call system('command')
I invoke ghci in gvim by using :!ghci % to load my haskell file, However, the newline is displayed as ^J as the image below shows:
If I invoke ghci in vim instead of gvim, then everything is OK, so How could I get newline in gvim ?
For the graphical version of Vim, GVIM, a crude built-in terminal emulation is used for shell commands (cp. :help gui-shell). As the help page mentions, this has some limitations, and it is not meant to be used for interactive commands.
Switching to Vim in a terminal will run the shell command inside the terminal (with full capabilities); I guess that would be the best alternative if you cannot live with the discrepancies, and don't want to launch a separate terminal from gvim (i.e. :! xterm -e ghci %)
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
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.