vim: Use insert-mode-like completion for command completion - vim

Is it possible to use a vertical command line completion just the way it appears when using CTRL-N in insert mode?

I personally like
set wildmode=longest,list
It behaves sort of like bash completion. (It also shows the same information as <c-d> when you hit <tab>).

You can have such a menu when doing completion in the command-line window (q:) but you can't have it in the command-line itself.
But set wildmenu (see also :help wildmode) is good enough in my opinion.

Related

How to source vimrc in All buffers

To source vimrc in all buffers in the current window I do :bufdo so~/.vimrc ,to source vimrc in the ACTIVE buffers in all windows I do :windo so~/.vimrc. How to source vimrc in all buffers in all windows?
for some reason if I do:bufdo so~/.vimrc | :windo so~/.vimrc the active buffer in the non-active window changes.
example: if I set set number in my .vimrc, I want all buffers in all windows affect the change.
the solution might be something like this:
for window in windowlist
execute 'bufdo so~/.vimrc'
I use vim 8.2 .
PLEASE NOTE: I did try all of the commands you guys suggested but it seems that things aren't clear enough. all of the commands that includes :tabdo :windo :bufdo or combination of them doesn't work properly for what I want. please note that this is the same as changing vimrc and sourcing it with chain of these commands like :tabdo bufdo so~/.vimrc. for example :windo bufdo set number does set number to all buffers(active or hidden) in the active window but on non-active windows only does set number for the active buffer(buffer that is shown in the window) or :tabdo windo set number only does set number in active buffers in every window meaning that hidden buffers won't get line numbers.
I even made a reddit post about this problem on r/vim and made a video about it but I don't know why nobody gets what I say. honestly don't think this problem will be solved by someone else but me even though its a simple one.
The :bufdo command will consume | as part of the command to execute, so in effect you're running the :windo command for each buffer that you have active!
See :help :bar, which documents this behavior.
That help section also mentions a way to work around this behavior, by using the :execute command to run the first command from a string, which allows you to delimit the first command. Like so:
:execute 'bufdo so ~/.vimrc' | windo so ~/.vimrc
Please note that sourcing your vimrc file "in all buffers" or "in all windows" doesn't make much sense... The vimrc file typically has global commands that usually need to be sourced only once, and usually if you modify your vimrc, sourcing it again only once should be enough...
This might make sense with a separate *.vim script that affects local settings and is meant to act on a single buffer. The ftplugin, indent and syntax scripts come to mind. But also with those, they're normally run per buffer, not per window... It's not completely inconceivable that you'd have scripts that you want to run on every buffer and window, but it surely seems odd...
.vimrc shall only contain global definitions. That the way it's supposed to be used. Sourcing it in several buffers makes no sense.
I wonder if you're fighting with local settings for which the best tool to use is either ftplugin (when the setting are filetype driven), or a local_vimrc plugin for project driven settings.
To apply in all buffers and all windows:
:windo bufdo set number
If you have tabs, from :tabe, :tabf, :tab and friends. Just add tabdo like this:
:tabdo windo bufdo set number
I would advise not to source your vimrc like this. If you want a quick setting, just use the set command and friends.
:help tabdo
:help windo
:help bufdo
:help source
While I agree with most answers on sourcing your vimrc. I do believe there are uses of the source command. Particularly batch fixing. I've never tried using
:windo with this, I mostly use :argdo and :cdo. As I have more fine-grained control on what files I need to apply.
Batch fixing is particularly useful on a large codebase. You do your fixes with :g, :v, :s for example, and save them in a file called fix.vim. This is so useful, you could even pair macros with those commands (:g and :v) via :norm command.
:help norm
Then update your argslist via :args *.js or similar commands (like backtick expression) and finally do the :argdo source fix.vim

Show Vim command options

This was a feature I used to have when I was first learning vim, and sometime between me customizing my .vimrc file, I must have disabled it some how.
Lets say I have this command I can enter
:TmuxNavigate
Now this command could take Left, Right, Up, or Down as options. Early on in my learning of Vim, I had a small line above the command line that would show any available commands that start with :TmuxNavigate
I've tried enabling set showcmdor laststatus=2 but wasn't able to enable this feature.
Is this a feature I can enable or have I just completely lost my mind and imagined this? Any help is appreciated.
Just add this line to your ~/.vimrc:
set wildmenu
Every command set up to complete arguments will do what you expect.
See :help 'wildmenu' and :help 'wildmode'.
[space] [Ctrl+d] after command like:
:colorscheme [space] [Ctrl+d]

How to open a terminal in new tab from VIM?

When I'm working with lets say 4 files, all are open in tabs(VIM). I want to save the changes and compile it without having to close the tabs, i.e I want to open a terminal in new tab along with the existing 4?
How should I do this in VIM?
:tab ter
opens a terminal in a new tab instead of opening it in a new window as :ter does. You can also use the equivalent, longer :tab terminal form.
Credits to user wolloda in this Reddit post.
Extra information
Terminating the shell with Ctrl-d or exit closes the terminal buffer.
Ctrl-w N or Ctrl-\ Ctrl-n put the buffer in the terminal-normal mode:
Keystrokes are not forwarded to the shell, but are used by Vim as in a normal buffer
(although the shell job is still running). Then you can use gt to change tabs,
type Ex commands such as :ls, etc. To bring the terminal buffer back to life,
i, a, ...
If you are going to map this, I recommend using :tab ter++kill=hup, so that when you :qa the terminal job does not prevent Vim from quitting.
And this is the signal normal terminal emulators send to its jobs when closed anyway.
For terminal mode mappings, use tnoremap, for example
tnoremap <S-Tab> <C-W>:tabprevious<CR>
tnoremap <C-N> <C-W>N
More information on :help :terminal and :help :tab.
In 2019, vim now has a Terminal mode.
:help terminal
For example, you can use it like this.
# go to terminal-job mode
:terminal
# go to terminal-normal mode
ctrl-w N
# go back to terminal-job mode
i
A more vim like way of doing this would be to use :make
:make
:make will execute the 'makeprg'. It defaults to make which is great of C projects
After running :make the quickfix list will be contain any errors.
Set your compiler via the :compiler command.
Extra parameter can be passed like so :make foo-command
Current filename can be represented by %. e.g. :make %
quickfix list
Use :cnext and :cprev to move between your errors.
:copen to open up the quickfix list in a window (:cclose to close)
:cwindow to open quickfix list window only if there are errors
May want to use better mappings for :cnext and friends. I suggest Tim Pope's unimpaired plugin
Alternatives and Plugins
Just use <c-z> to suspend vim and run your build system. (Cons: loose out on the quickfix list)
Use :! to compile. (Same cons as suspending) e.g. :!make
Syntastic is a syntax checking system that checks files on save
Dispatch can be used to run things in the background. Great for test suites
As #brettanomyces mentioned you may want to consider terminal multiplexers like tmux or screen.
SingleComplile tries and takes some of the work out of using :make
Conclusion
If you are just starting out I would suggest you learn how to use :make and the quickfix list. There is a nice Vimcast episode that intros the quickfix list: Search multiple files with :vimgrep. Additionally Syntastic is a great way to get up and running with linters quickly.
Aside about tabs
Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. Therefore using features like the quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) use no tabs and use on average 1-2 splits without any issue. Bottom line: Learn to use buffers effectively.
For more help see the following:
:h :make
:h 'makeprg
:h quickfix
:h :cnext
:h :cope
Vim 8.1 now has a built in terminal that can be opened with the :term command. This provides much more complete integration with the rest of the Vim features.
Original Answer:
I would suggest looking at tmux or screen. I use tmux myself and along with vim-tmux-navigator moving between the terminal and vim is very easy.
For anyone using NeoVim:
The highest voted answer uses :tab ter. This doesn't work on NeoVim (at least for me). However, it's still fairly simple:
:tabe term://bash
tabe is open a new tab and edit file.
term:// is a NeoVim way of opening a terminal
bash is the kind of shell you want to use (e.g. I use zsh, so my command is actually :tabe term://zsh)
Some helpful commands that I created:
" open terminal
if has('nvim')
command Terminal vsplit term://zsh
command TerminalTab tabe term://zsh
else
command Terminal vert term
command TerminalTab tab ter
endif
Another way
Ctrl-w :
That gets me to the command line, then one can enter tablast tabnext or tabprevious
or the short versions tabl, tabn, tabp
Or this way:
Ctrl-w gt and Ctrl-w gT
(Next tab and Previous Tab)
Or Ctrl-w Number gt (for a specific tab)
That works too.

How do you search through Vim's command history?

I would like to have the following search in Vim too
(reverse-i-search)`':
Enter a word of your previous command, and you get the full command.
I know the chronological history tool in Vim
q:
However, it is not that useful as the fuzzy reverse search.
How can you have a similar reverse search in Vim as in the terminal?
Type q: in the normal mode to open commands window. You can search/edit here using regular vim commands. You start in Normal mode. Press Enter to execute a command.
This approach lets you search across whole command not just beginning of line.
Enter the first letters of your previous command and push <Up> arrow (or Ctrl+p).
:set li<up>
:set lines=75
Don't forget to check history option and set it to big enough value
:set history=1000
Press Ctrl+F in command mode to open the command history window. Then, you can use / , ? , and other search commands. Press Enter to execute a command from the history.
For more about the command history window, see :h cmdwin .
Here are the docs for Vim's commandline history, also see this part of the docs on Vim's commandline history that covers the key bindings while in the history. It looks like you can say :foo and then hit the up arrow to find the last command that started with foo.
With FZF fuzzy search command: :History:
Source: https://github.com/junegunn/fzf.vim
I was looking for this as well (finally after wondering why it wasn't built-in for some time) and decided I couldn't resist whipping up an implementation, so here you go: https://github.com/goldfeld/ctrlr.vim
It should work just like the shell's--well there are still a couple basic things missing (like pressing ^R again to skip to next match), but all that I use is in this first release, and I plan to add the rest in the coming weeks as I get time.

How do I run a terminal inside of Vim?

I am used to Emacs, but I am trying out Vim to see which one I like better.
One thing that I like about Emacs is the ability to run a terminal inside Emacs. Is this possible inside of Vim? I know that you can execute commands from Vim, but I would like to be able to run a terminal inside of a tab.
Outdated from August 2011
Check out Conque Shell (also on GitHub). Lets you run any interactive program inside vim, not just a shell.
I'm not sure exactly what you're trying to achieve (I've never used Emacs), but you can run commands in Vim by typing:
:! somecommand [ENTER]
And if you want to type in several commands, or play around in a shell for a while, you can always use:
:! bash (or your favourite shell) [ENTER]
Once the command or shell terminates, you'll be given the option to press Enter to return to your editor window
Vim is intentionally lightweight and lacking in the ability to do non-editorish type things, just as running a full-blown shell inside a Vim pane/tab, but as mentioned above there are third-party addons such as vim-shell that allow you to do that sort of thing.
Typically if I want to switch between Vim and my shell (Bash), I just hit CTRL+Z to pause the Vim process, play around in my shell, then type 'fg' when I want to go back to Vim - keeping my editor and my shell nice and separate.
Updated answer (11 years later...):
I would recommend using tmux instead of screen as suggested in the original answer below, if you choose to use that solution.
Vim 8.1 now has a built in terminal that can be opened with the :term command. This provides much more complete integration with the rest of the Vim features.
I would definitely recommend screen for something like this. Vim is a text editor, not a shell.
I would use Ctrl+AS to split the current window horizontally, or in Ubuntu's screen and other patched versions, you can use Ctrl+A|(pipe) to split vertically. Then use Ctrl+ATab (or equivalently on some systems, Ctrl+ACtrl+I which may be easier to type) to switch between the windows. There are other commands to change the size and arrangement of the windows.
Or a less advanced use of screen is just to open multiple full-screen windows and toggle between them. This is what I normally do, I only use the split screen feature occasionally.
The GNU Screen Survival Guide question has a number of good tips if you're unfamiliar with its use.
The way that I get around this is:
pause Vim with Ctrl + Z,
play in the terminal,
then return to exactly where you left with Vim by just typing the command fg.
If enabled in your version of Vim, a terminal can be started with the :term command.
Terminal window support was added to Vim 8. It is an optional feature that can be enabled when compiling Vim with the +terminal feature. If your version of Vim has terminal support, :echo has('terminal') will output "1".
Entering :term will place you in Terminal-Job mode, where you can use the terminal as expected.
Within Terminal-Job mode, pressing Ctrl-W N or Ctrl-\ Ctrl-N switches the mode to Terminal-Normal, which allows the cursor to be moved and commands to be ran similarly to Vim's Normal mode. To switch back to Terminal-Job mode, press i.
Other answers mention similar functionality in Neovim.
:sh then Ctrl+D to get back in (bash)
Update:
You could map Ctrl+D in vim to run :sh, which allows you to toggle between bash and vim quickly.
noremap <C-d> :sh<cr>
The main new feature of Vim 8.1 is support for running a terminal in a Vim window.
:term will open the terminal in another window inside Vim.
:term
Added in Vim 8.1.
Keep in mind that whenever a terminal window is active, most keystrokes will simply be passed to the terminal instead of having their usual functions. Ctrl-W and its subcommands are the main exception. To send a literal ^W input to the terminal, press Ctrl-W .. You can also open the Vim : command line by pressing Ctrl-W :. The other Ctrl-W commands work as normal, so managing windows works the same no matter what type of window is currently selected.
Eventually a native :terminal command was added to vim in 2017.
Here is an excerpt from the :terminal readme:
This feature is for running a terminal emulator in a Vim window. A
job can be started connected to the terminal emulator. For example, to
run a shell:
:term bash
Or to run build command:
:term make myprogram
The job runs asynchronously from Vim, the window will be updated to
show output from the job, also while editing in another window.
This question is rather old, but for those finding it, there's a new possible solution: Neovim contains a full-fledged, first-class terminal emulator, which does exactly what ConqueTerm tried to. Simply run :term <your command here>.
<C-\><C-n> will exit term mode back to normal-mode. If you're like me and prefer that escape still exit term mode, you can add this to your nvimrc:
tnoremap <ESC><ESC> <C-\><C-N>
And then hitting ESC twice will exit terminal mode back to normal-mode, so you can manipulate the buffer that the still-running command is writing to.
Though keep in mind, as nvim is under heavy development at the time I'm posting this answer, another way to exit terminal mode may be added. As Ctrl+\Ctrl+n switches to normal mode from almost any mode, I don't expect that this answer will become wrong, but be aware that if it doesn't work, this answer might be out of date.
https://github.com/neovim/neovim
I know that I'm not directly answering the question, but I think it's a
good approach. Nobody has mentioned tmux (or at least not as a
standalone answer). Tmux is a terminal multiplexor like screen. Most
stuff can be made in both multiplexors, but afaik tmux it's more easily
to configure. Also tmux right now is being more actively developed than
screen and there's quite a big ecosystem around it, like tools that help
the configuration, ecc.
Also for vim, there's another plugin: ViMUX, that helps a lot in
the interaction between both tools. You can call commands with:
:call VimuxRunCommand("ls")
That command creates a small horizontal split below the current pane vim
is in.
It can also let you run from a prompt in case you don't want to run the
whole command:
<Leader>vp :VimuxPromptCommand<CR>
As it weren't enought, there are at least 6 'platform specific plugins':
vim-vroom: runner for rspec, cucumber and test/unit; vimux support via g:vroom_use_vimux
vimux-ruby-test: a set of commands to easily run ruby tests
vimux-cucumber: run Cucumber Features through Vimux
vim-turbux: Turbo Ruby testing with tmux
vimux-pyutils: A set of functions for vimux that allow to run code blocks in ipython
vimux-nose-test: Run nose tests in vimux
Here is a nice "use case": Tests on demand using Vimux and Turbux with Spork and Guard
Someone already suggested https://github.com/Shougo/vimshell.vim, but they didn't mention why. Consequently, when I came away from this question I wasted a lot of other time trying the other (much higher ranked) options.
Shougo/vimshell is the answer. Here's why:
In addition to being a terminal emulator, VimShell allows you to navigate through terminal output in normal and visual mode. Thus, if a command you run results in output that you'd like to copy and paste using the keyboard only...VimShell covers this.
None of the other options mentioned, including the :terminal command in NeoVim do this. Neovim's :terminal comes close, but falls short in at least the following ways as of 2/18/2017:
Moves the cursor to the end of the buffer, instead of at the last keeping it in the same spot like VimShell does. Huge waste of time.
Doesn't support modifiable = 1 see a discussion on this at Github, so helpful plugins like vim-easymotion can't be used.
Doesn't support the display of line numbers like Vimshell does.
Don't waste time on the other options, including Neovim's :terminal. Go with VimShell.
It's possible to open a new tab with a terminal in vim since 2017 as #fjardon said:
Just type: :terminal. It will open a tab by default above your current tab.
If you want it to open in another place you can try the following options:
:below terminal : open the terminal below current tab.
:below vertical terminal : open the terminal always vertically to the right.
You can play with these until you find what you like. After this you can set a map in your .vimrc configuration file, for me, I use:
nmap <leader>tt :below vertical terminal<CR>
This way I can type <space>tt (space my leader key) to open it quickly.
As a side note:
You can switch between your tabs (terminal and other buffers) with Ctrl+W Ctrl+W.
You can enter an editable mode in your terminal if you want to copy your commands with Ctrl+W N and go to normal terminal mode with i or a.
Cheers!
You might want to take a look at the :sh command (see :help sh in Vim).
Various commands
No, you cannot:
http://vimdoc.sourceforge.net/htmldoc/tips.html#shell-window
By far, I have tried a lot of solutions mentioned here, what I really wanted is to keep the terminal open while coding a similar experience in VsCode. Then I came across this solution which is working perfectly for me.
Before Installing:
I am using Nvim 0.5 but I think it can work for any version and checked also on vim
I am using macOS Catalina Version 10.15.7
Setup your integrated terminal
Step -1-
Create a script with the name myQuickTerminal.vim or whatever name you want.
Put the following script
"==============================================================================
"
" ▒█▀▀█ █░░█ ░▀░ █▀▀ █░█   ▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ░▀░ █▀▀▄ █▀▀█ █░░
" ▒█░▒█ █░░█ ▀█▀ █░░ █▀▄   ░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▀█▀ █░░█ █▄▄█ █░░
" ░▀▀█▄ ░▀▀▀ ▀▀▀ ▀▀▀ ▀░▀   ░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▀▀▀ ▀░░▀ ▀░░▀ ▀▀▀
"
"==============================================================================
" " This is a script that will trigger a terminal quickly than the FloatTerminal
" open new split panes to right and below
"link: https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
"==============================================================================
set splitright
set splitbelow
" turn terminal to normal mode with escape
tnoremap <Esc> <C-\><C-n>
" start terminal in insert mode
au BufEnter * if &buftype == 'terminal' | :startinsert | endif
" open terminal on ctrl+n
function! OpenTerminal()
split term://zsh
resize 10
endfunction
nnoremap <leader> n :call OpenTerminal()<CR>
NOTE: if you want to run bash instead of zsh for a particular reason then replace zsh with bash.
Step -2-
Lets source it, put this in init.vim for neovim or `.vimrc' for vim
source $HOME/.config/nvim/modules/mySpecialScripts/myQuickTerminal.vim
This will be preloaded ahead as you save and resource it, you can use source $MYVIMRC for quick reloading the init.vim file.
Step -3-
I mapped as you can see in the script n to open a terminal in a new pane, my is the (Space bar) and once I click (space + n) a terminal will be triggered and I will enjoy writing my code while the terminal is opened.
To quit insert mode in the terminal, press Esc.
Now, to switch to the code editor pane, use CTRL+w w. This shortcut can get annoying once you have more than two panels open, so I added the following shortcuts too.
I mapped these too for quick jumping among opened panes, use these
" Better window navigation
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
Optional
If you want your terminal to exit the current buffer with key. you can use
tnoremap <Esc> <C-\><C-n>:q!<CR>
But since I want to switch in between several buffers I use without close the terminal I use instead:
tnoremap <Leader><Esc> <C-\><C-n>:q!<CR>
Final results
Reference:
https://betterprogramming.pub/setting-up-neovim-for-web-development-in-2020-d800de3efacd
Only way I know of is by using vim-shell, a third-party patch.
I use this now, you may can try. VimShell
Split the screen and run command term ++curwin to run the terminal inside the Vim buffer. Following command does both and worked for me:
:bo 10sp | term ++curwin
If you are interested in quick answer, here is it: :vert term. It will split your screen vertically and open up terminal.
Try vterm, which is a pretty much full feature shell inside vim. It is slightly buggy with its history and clear functions, and still in development, but it still is pretty good
Assuming your version of vim supports +term command first, set shell for vim to use in one command (e.g. set=/usr/bin/zsh), and then run the command +term (i.e. bo 15vs +term). you may have to do some additional maneuvering of your windows (i.e. deleting one and rotating), but you'll have your terminal.
With vim 8.1.3741, just type :terminal to start a terminal inside of vim.
Try map :nnoremap ]t :terminal<CR> to do that quicker!
I acknowledge that I am not strictly answering your question, but what has worked better for me when using Vim and Terminals in the same window is Tmux (which is kind of a "run in the background software" like, similar to screen, although this one works better with splits and tabs).
This post will help you to understand how they work together: 'Tmux and Vim — even better together'.
This way we can convert Vim into a powerful IDE

Resources