How do I start vim in insert mode? - vim

Whenever I enter vim, there are 99% chance that I will go in insert mode and edit the file. Can I make vim always start in insert mode?

You can start vim like this:
vim -c 'startinsert' FILENAME
If you want, you can edit the .bashrc file (if you are using bash) and add this line:
alias vim="vim -c 'startinsert'"

You can use vim +star, which is even shorter. NB: star is short for :help :start.
If you want this behavior by default, the best option is to add the line
autocmd BufRead,BufNewFile * start
into your ~/.vimrc. Also take a look at :h 'insertmode', which outlines a special option made for this kind of functionality. However, it can make it difficult to get out of insert mode which is crucial for growing in your vim ninja skills.

You can, and it's very simple.
The :startinsert command enters insert mode. (It is the exact command-line-mode counterpart to typing i in normal-mode.) Just drop it into your vimrc so it runs at startup. Unlike some of the other suggestions, this doesn't interfere with dropping back to normal-mode by ESC as normal.

Additionally, there's something called "Easy mode", started from vim -y or evim. It's a more radical departure than just starting in insert mode: it has some key bindings matching other editors', and normal-mode commands are done by hitting Ctrl+O instead of Esc. As a consequence of that, being in insert mode is the rule rather than the exception.

Related

Hard refresh in vim (not :so %)

Sometimes in vim I'll need to exit the vimrc file and enter back into it to update changes (for example if I'm editing other files related to it). To do this I'll normally do:
:wq
$ vim
ctrl-o ctrl-o " in vim
Would there be a way to do this all within vim? Something like:
:wq | !vim %
Additionally, why does it require me to do ctrl-o two times to go to the previous buffer (it almost seems like the first ctrl-o does nothing)?
Update: Please note that I am aware of doing :so % or :so $MYVIMRC, etc. My question here is how do I basically reset 100% of the things to whatever are in my current files? That is, unset ALL mappings, variables, etc. that may have been updated, removed, etc; update ALL files that may have changed (functions, plugins, colorschemes, etc.). I don't think "Running :so % on 20 files" is a good solution here, which is why my current solution is to close the file and re-open it.
As others have mentioned, you can source your .vimrc, but that doesn't completely reset Vim. If you want to just restart Vim, then you can do so by re-execing it.
Vim doesn't provide a built-in way to exec processes from within it, since typically one doesn't want to replace one's editor with another process, but it is possible to do so with Perl or Ruby, as you see fit:
:perl exec "vim"
or
:ruby exec "vim"
This may or may not work on Windows, and it of course requires that your Vim version have been compiled with support for the appropriate interpreters. Debian provides both in the vim-nox, vim-gtk3, and vim-athena packages, but not in vim or vim-tiny; Ubuntu, last I checked, did not provide Ruby support but did include Perl.
If you want to re-exec with the same buffer, you can use one of these:
:perl exec "vim", $curbuf->Name();
or
:ruby exec "vim", Vim::Buffer.current.name
Note that re-execing may cause your screen to be slightly messed up when exiting, so you may need to use reset to set it back to normal.
I don't know if you had tried this but you can source your vimrc file from vim itself by typing
:so $MYVIMRC
In order to apply the changes, you don't have to exit vim and open it again, no need for a "hard refresh" :)
If you want to apply in on the .vimrc file itself, you can type
:so %
to apply the changes in another file, you can type:
:so ~/.vimrc #path to your .vimrc file
in normal mode, Ctrl-O takes you to where your cursor has been backward and Ctrl-I forward. You can check your jump list by typing :jumps, to clear your jumps :clearjumps
in insert mode, Ctrl-O escapes to normal mode and lets the user to do one normal mode command.

Is there a way to change vim's default mode

Does anyone know how to change vim's default mode? Its default mode is command mode, but could I change it to insert mode?
Just add the following line to your vimrc:
start
Vim's default mode will be changed to Insert mode. Just press Esc to enter Command mode.
You can try the 'insertmode' option (add set insertmode to your .vimrc file), although I recommend that you learn the standard vi operation, because not all vi-like editors support this mode of work.
cheers,
mitch
If you really want an editor that starts out in insert mode, perhaps vim is not the editor for you.
Most editors that aren't based on vi behave the way you want. Emacs is very powerful, but it may be too complicated for your needs. Nano (man nano for info nano for more information) might be a good choice. There are other possibilities.
vim really isn't designed to be used that way. As others have said, there are ways to force it to start up in insert mode. (I've been using vi-style editors for decades, and I didn't even know about :set insertmode until now.) But it's awkward to use.
My advice: either (1) use an editor that behaves the way you want, or (2) spend some time learning to use vim in its default configuration, and see if you can get used to it.
One big advantage of vim's separation of insert and command modes is that the command mode can use letters as commands; modeless editors generally have to use control keys or function keys to execute commands.
Update (a decade later): vim has an option -y that starts it with insertmode enabled. evim is equivalent to vim -y, and eview to view -y. From the man page (emphasis added):
eVim starts Vim and sets options to make it behave like a modeless editor. This is still Vim but used as a point-and-click editor. This feels a lot like using Notepad on MS-Windows. eVim will always run in the GUI, to enable the use of menus and toolbar.
Only to be used for people who really can't work with Vim in the normal way. Editing will be much less efficient.
The 'insertmode' option is set to be able to type text directly.
Mappings are setup to make Copy and Paste work with the MS-Windows keys. CTRL-X cuts text, CTRL-C copies text and CTRL-V pastes text. Use CTRL-Q to obtain the original meaning of CTRL-V.
You can use
vi -cstartinsert
or
vi -cstart
That launch vi and put it insert mode. You can do an alias to that if it's really usefull (I still understand why you want that anyway). You can also look at this tip.
"Cream" is a project that aims to make Vim easier to use. By default everything you do in Cream is in insert mode, I believe:
http://en.wikipedia.org/wiki/Cream_%28software%29

VIM: Know which command executed when a key pressed

How do I know which command will be executed when I press a key, for example <Leader>c?
To see mappings use:
:verbose map <leader>c
Replace map by the corresponding imap, cmap, etc., as needed.
For Vim's built-in commands you'll need to use the help:
:help gq
See :help context for pointers.
Sometimes if map <keys> is not enough you may use one of the following:
:debug normal <keys><CR>: for normal, visual, select, operator-pending and insert/replace/virtual replace modes but not for ex/command-line mode. You will have to precede <keys> with something that enters target mode.
:set verbosefile=/tmp/verbose.log verbose=15<CR><keys>:set verbose=0<CR>: for all modes it will procude a log of all commands executed in file /tmp/verbose.log. You will see errors if there is a recursive structure somewhere.
Start vim with vim -s <(echo '<keys>') -D. It will enter debug mode immediately after vim starts, but you will have to skip all initializations manually.
These are all advanced debugging features and they are very time-consuming, but they may help where something more simple cannot.

vim command line completion when opening files

How to make VIM to always auto-complete filenames in command mode? It works fine when I type for example ":cd /ww[Tab]", but if I want to open a file and type ":o /ww[Tab]", it inserts "^I" character instead of completing.
Try:
:e /ww[Tab]
Use ":e" or ":split" or other edit commands instead of ":o".
Bonus fact: vim doesn't really support the ":o" command, at least not according to the docs. ":help :o" says this:
This command is in Vi, but Vim only simulates it:
*:o* *:op* *:open* :[range]o[pen]
Works like |:visual|: end Ex mode.
{Vi: start editing in open mode}
:[range]o[pen] /pattern/ As above, additionally move the cursor to the
column where "pattern" matches in the cursor
line.
Vim does not support open mode, since it's not really useful.
For those situations where ":open" would start open mode Vim will
leave Ex mode, which allows executing the same commands, but updates
the whole screen instead of only one line.

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