Any execution of command in vim causes it gets suspended - shell

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.

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).

Open a small window while in Vim for executing commands

I need to write lot of code and compile very often. I hate switching back and forth various windows just to compile the code. Is it possible to open a small window at bottom and run invoke shell and close that window when needed?
With GVim or MacVim, you can run external commands in the command-line: Gvim/MacVim comes with a (very) limited shell that will happily show you whatever the compiler outputs. The general usage pattern is:
:!command
:!command %
With CLI Vim, the same method will pause Vim and return to the shell to execute your command.
In both cases, you'll get a message asking you to press ENTER to come back to your normal editing.
Using :make | cw would be a slightly more sophisticated alternative, with the added bonus of showing the errors in the quickfix window.
An even more sophisticated approach would be to use Tim Pope's Dispatch plugin in combination with tmux or screen.
Sounds like a problem for Screen
http://www.gnu.org/software/screen/
Quick reference of commands
http://aperiodic.net/screen/quick_reference
I use tmux to achieve something like that. I have the following in my ~/.tmux.conf file:
bind s splitw -v -p 25 -c '#{pane_current_path}' '/bin/bash'
bind q kill-pane
On pressing Ctrl-b + s (prefix + s), a new pane containing a bash shell opens up at the bottom. I can run shell commands from there: find, grep, make, etc. When I'm done, I press Ctrl-b + q to close the shell.
To enable tmux on every bash session, add the following to your ~/.bashrc:
[[ -z "$TMUX" ]] && exec tmux
Maybe map a key to shell out to the compiler and run the program if compilation is successful:
:map F8 :!cc % && ./a.out
Or maybe just
:sh
make run
Ctrl-D
Another option is to suspend vi, using Ctrl-Z and do your stuff in the shell, then type fg to bring vim back to the foreground. Note that this is actually a feature of your shell, rather than vim but it produces the effect you seek.
Note this idea originates from the book "Efficient Linux at the Command Line" by Daniel Barrett. I forget the page number.

vim shell key mapping to execute shell commands

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.

Vimperator - Bash - can't use alias

I am using the Firefox plugin Vimperator that simulates vim like behaviour. However, i want to use some Bash commands i specified in my ~/.bashrc.
For example, i have todo.txt.sh bound to an alias 't' so that i can simply type 't add remberToDoThis' to add a task.
It would we wonderful to have the ability to add tasks from within Vimperator by typing
:!t add task
However, this is the message i get:
:!t add task
/bin/bash: t command not found
shell returned 127
I am using Ubuntu 10.10 Maverick Meerkat
Any suggestions?
Thank you for your try, but actually i found out a way:
just do the following in vimperator:
:set shell=/bin/bash
:set shellcmdflag=-ic
by default the shellcmdflag option is only "-c" by adding i we are telling the bash to be interactively, allowing to execute my aliases from the .bashrc
After doing this, it finally works. My aliases are recognized and are working, besides the fist line always telling me no job control available but i can live with that
Vimperator has no knowledge of bash aliases, so you have to call your script directly.
You can however define an abbreviation like:
:cabbr tt !todo.txt.sh
so you can type: :ttspace and have it expanded to !todo.txt.sh.

Resources