vim shell key mapping to execute shell commands - shell

i am using vimshell to execute commands inside vim
nnoremap <leader>vs :VimShellPop<CR>
with this key mapping i can open vim shell and execute commands like 'bundle install' and then
type exit to exit VimShellPop window but i want set a key mapping
nnoremap <leader>bi :
to open up vimshellpop execute the bundle install command and exit once i get completed..is it possible in vimshell?

The vimshell plugin provides an interactive shell inside a Vim buffer. Apparently, you don't need the interactivity (because you intend to immediately exit after issuing the shell command). For that, you don't need the plugin itself; the built-in :! command already allows you to launch external commands:
:nnoremap <leader>bi :!bundle install<CR>
If you want to keep the output visible, you can read it into a scratch buffer:
:nnoremap <leader>bi :new<Bar>0r!bundle install<CR>

Having an interactive shell in Vim is one of Vim's stated non-goals (cp. :help design-not), so the plugin has to jump through several hoops to make this possible. Those hacks are causing these problems (of defining a proper mapping, as evidenced by the attempts in the question's comments); lack of automation (like through mappings) is a limitation of this approach.
You may contact vimshell's author (via email or GitHub issue); he's usually very open and responsive! He's in the best position to make such mapping work.

Related

Paste bash command, but make sure it doesn't run

Sometimes when you ctrl-v with bash it will run the command even though you didn't intend to run it yet - is there a way to paste a command into the bash shell / terminal making sure you don't actually run any of the command(s)?
if you could set what was on the terminal prompt programmatically, you could do this with bash on MacOS:
export BASH_PROMPT="$(pbpaste)"
which ties into my other question that I just asked:
How to change the value that's in the prompt
There is a Readline variable:
enable-bracketed-paste
When set to On, Readline will configure the terminal in a way that will enable it to insert each paste into the editing buffer as a single string of characters, instead of treating each character as if it had been read from the keyboard. This can prevent pasted characters from being interpreted as editing commands. The default is off.
To turn this on, put something like
set enable-bracketed-paste on
into your ~/.inputrc.
This was introduced in Bash 4.4 / Readline 7.0.
Use ^X^E aka Ctrl+X Ctrl+E in bash to open your $EDITOR for command entry.
Paste and/or edit as much as you want, across as many lines as you want. When you're done, save and exit, and bash will run it.
(In vi mode, the shortcut is v)

How to prevent Emacs from duplicating shell commands?

I always use Emacs's shell. I create 2 buffers (C-x 2) and use one of them as a terminal (M-x shell).
Sometimes I use a command a lot of times (e. g. python3 test.py). I use shortcut <C-up> to repeat the last command. And it's very inconvenient that Emacs saves duplicates of commands. I have to press <C-up> many times so that I can run any other command.
The problems is only in Emacs's shell. I have setting HISTCONTROL=ignoreboth in .bashrc and in usual terminal it works very well.
Is it possible to configure Emacs so that it ignores duplicates?
It's customizable
(setq comint-input-ignoredups t)
Setting 'comint-input-ignoredups' as suggested by Jürgen Hötzel will of course solve the immediate, but here are some alternatives.
You could choose to use the command 'ansi-term' instead of 'shell', this will run a bash (or whatever you choose) so you get the same control as in (say) an xterm. As an added bonus, you also get a rather capable terminal so applications that depend on this will work.
Another possibility, if you have something you want to repeatedly run, is to use the 'compile' command. This can run any shell command, it does not have to be a compiler. The advantage here is that you keep the running of the test out of your shell command history (no matter how you run your shell) and you get the output in a separate buffer. If the output is suitably organised and/or the compilation buffer is suitably configured, you could also use the 'next-error' command to jump back to the appropriate part of the source.
Finally, I would like to mention that the 'shell' command supports searching the command history. It is by default bound to M-r (comint-history-isearch-backward-regexp).

How do I open a shell but still have access to vim

I am running gvim on Windows 7.
I use this mapping to execute the current file with powershell:
nnoremap <C-q> :! & '%:p'<cr>
It works great except I can't access vim until I close the powershell window. Sometimes I want the shell to remain open so I can run additional commands or I want to access vim with the shell still open so I can check the lines where errors were generated.
Ideally (don't know if this is possible) I want to have an already open shell execute the command. So I always have vim and a shell open (on separate monitors) and I can execute the script in that same shell.
How can I achieve this?
GVIM on Windows has a special :!start command to execute the external command asynchronously; i.e. Vim doesn't wait for its return. Just replace the :! with it. See :help :!start for more information.
On Unix, such special isn't necessary; you can just append & (a shell feature) to execute the command asynchronously.

Any execution of command in vim causes it gets suspended

This occurs when I set the vim's shell to be interactive:
set shellcmdflag=-ic
or
set shell=/bin/bash\ -i
I like these because they give syntax highlighting to the output (eg.: !ls)
But the cost is that I have to type fg # every time.
Is this a default behavior?
How can I get interactive shell in vim without having to make it run foreground?
You can't. This behavior is perfectly normal and expected and in line with Vim's author's philosophy. It's very unlikely to change in the future.
If you want a shell inside Vim, you'll have to install a plugin like Conque or Vimshell.
I usually just use tmux instead. You can split the terminal and have a normal interactive shell and an instance of vim running side by side - very handy.

How do I open a file in Vim from inside a Conque shell

Often I find my self navigating the filesystem from a Conque shell in Vim and want to open a specific file inside my existing MacVim session. Is this possible ? - I was hoping for something like:
shell> open some/file.txt
and then have file.txt pop up inside my existing Vim window (preferably in a new tab).
Note: I am using #wycats vim dot files (not sure this matters).
Type from ConqueShell
mvim --remote-tab-silent filename
This will open the file in a new tab in MacVim
You could also write a Bash alias to shorten the command (assuming you are using bash).
Put in your ~/.profile
alias vim='mvim --remote-tab-silent'
this would enable you to type
vim filename
from ConqueShell or bash, and have it open in a new MacVim tab, rather than terminal vim. It of course does disable your ability to run standard vim (although you could still use the vi command), so maybe you would want to name the alias differently.
Just to add, this will work only if you placed the mvim executable on your path E.G. /usr/bin/mvim. It comes with the MacVim.app
Often I find my self navigating the filesystem from a Conque shell
The beauty of running a shell from inside vim is you have all of vim and the shell at your disposal.
gf is your friend. Once you get the file you want displayed on the screen in some way, you can enter normal mode, move the cursor to the file you want to edit, then use the gf command to navigate to the file. There are many ways to use this. Any program or command that outputs file names is great for this (ll, git status, etc). You could also type the filename into the shell, just to make it visible on the screen without actually running any terminal commands (tab completion is handy here).
It is possible, you can start vim as server and then add as many files as you want, but I'm not very familiar with this, so I can't give you just a direction.

Resources