Hot answers tagged

112

Reasons for swap files Swap files store changes you've made to the buffer. If Vim or your computer crashes, they allow you to recover those changes. Swap files also provide a way to avoid multiple instances of Vim from editing the same file. This can be useful on multi-user systems or just to know if you have another Vim already editing a file. ...


92

The first thing you want to do is to start Vim with the default settings: vim -u NONE -U NONE -N The -u NONE prevents Vim from loading your vimrc, -U NONE prevents Vim from loading your gvimrc, and -N tells Vim to use no-compatible mode (this isn't required, but most Vim users are not used to "compatible" mode). Note that the NONE is required to be in all-...


82

TL;DR You can use your existing ~/.vimrc, files, and plugins located within ~/.vim without having to symlink the files. And this is now documented in NeoVim manual, see Transitioning from Vim. For Linux and macOS, just add below lines to the top of your ~/.config/nvim/init.vim, or %LOCALAPPDATA%\nvim\init.vim for Windows. set runtimepath^=~/.vim ...


66

The :b command can also take a substring of the name of the file of the buffer which you want to travel to, which is very convenient. For example, if you have three buffers foo, bar, and baz, then :b bar will switch to the bar file. :b o will switch to the foo file. :b a will give you an error because it could mean either bar or baz, but you can fix the ...


48

Run: :source $MYVIMRC inside Vim to reload the vimrc file. Or, a shorter version: :so $MYVIMRC as mentioned in a comment by kenorb. You may also find it useful to map either of these forms to a key. For example: nnoremap <Leader>r :source $MYVIMRC<CR>


43

let assigns a value to a variable, and set assigns a value to one of Vim's internal options. For example, you would use :let mystring='Hello!' to declare a new variable, but you would use :set tabstop=4 or :set expandtab to set one of Vim's options. See also: :help let, :help set. g: simply signifies a global variable. There are several of these "...


36

There is -D Vim parameter specially for debugging which will go to debugging mode after executing the first command from a script. E.g. to run Vim in debug mode without any plugins, run as: vim --noplugin -D Type n/next to parse the next line and keep pressing Enter. And cont or q to go back to vim interface. If you're using a GUI version, put a gui ...


28

One has to realize that the feature implemented by airline are inspired by the powerline plugin. Furthermore, I was relying on the docs too much, not realizing that there is well-written airline help document shipped with itself: :h airline Reading its documentation helps understanding the functionality of this plugin. Font As mentioned in the powerline ...


27

Yes this is possible and useful and even considered best practice. They are called filetype plugins in Vim speech. And Vim even comes with many filetype plugins (as well as indent and syntax files) for several languages. You need to enable this in your .vimrc like this: filetype plugin on then put your specific filetype settings into a file (creating non-...


26

A "script" does nothing more than run a sequence of ex commands. An "ex command" is what you type when you use : in Vim. For example :wq, :set wrap, :e file, etc. are all ex commands. The : is not part of the command; it is merely a keystroke to start the command-line mode; you don't always need to include the :, for example when you chain multiple commands ...


25

I have the following modelines at the bottom of my vimrc which I copied from godlygeek, the author of tabular: "" vim:fdm=expr:fdl=0 "" vim:fde=getline(v\:lnum)=~'^""'?'>'.(matchend(getline(v\:lnum),'""*')-2)\:'=' This will make any line starting with 2+ "'s a fold. The more "'s the deeper the fold. This ...


24

You don't need to store plugins in your VCS; you can also use a Vim package manager. Since yesterday, I use vim-plug: You can define plugins in your vimrc like so: call plug#begin('~/.vim/plugged') Plug 'embear/vim-localvimrc' Plug 'kchmck/vim-coffee-script' " ... etc call plug#end() Then restart Vim, and then install plugins with: :PlugInstall Or, ...


24

This is what I use: nnoremap <Leader>b :ls<CR>:b<Space> Now pressing \b will list the available buffers and prepare :b for you. Then you can just type the buffer number, and hit Enter. Or you can type part of the filename, and hit Enter. (However I usually hit Tab before hitting Enter, to check I got the right buffer. If not, I ...


23

There are a few lightweight ways to do this. Check for a file of given name and source it if filereadable(".vimscript_file") so .vimscript_file endif The file is hidden in the example but that's optional. Local .vimrc files (not the same as the plugin) set exrc This is similar to 1. but the file will be called ".vimrc". A common suggestion to ...


23

Plugins are sourced after your vimrc so there's no way to override a plugin mapping in your vimrc if the plugin doesn't provide a way to do so. Placing your custom mapping in ~/.vim/after/plugin/mystuff.vim (the name of the file doesn't matter) should allow you to override the plugin mapping.


23

As mentioned in other answers, the plugins are sourced after the vimrc is done. If you want to keep your overrides in your vimrc instead of doing an after plugin, you can use this "trick" anywhere in your vimrc file: autocmd VimEnter * noremap <leader>cc echo "my purpose" From :help VimEnter: VimEnter: After doing all the startup stuff, including ...


23

You can include another file using the source command. Simply put this at the top of your vimrc: source($MYVIMRC . ".private") Assuming your vimrc is ~/.vimrc, that will expand to ~/.vimrc.private. From now on, you can define variables in that file like this: let my_db_password = "bacon" Then you can simply refer to them by name in your main vimrc. ...


23

As the softtabstop documention mentions, it's useful if you want to keep the default tab stop size of 8, but edit a file as if the tab stop size was some other value. For example, if you wanted an indentation level of 4 while editing code, but some comments had tab-indented text such as a table that depended on a tab stop of 8, you could set sts to 4. One ...


21

If you just want to reload the file once in a while :source $MYVIMRC as Paul wrote is correct. If you end up changing your vimrc often, you could add something like this to your vimrc file: autocmd BufWritePost .vimrc,_vimrc source $MYVIMRC This will reload the file when you write it (from within that vim session)


21

Basic info Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim. Experiment 1 You can do this small ...


21

You could just do: %s//REPLACEMENT/ From the docs: If the {pattern} for the substitute command is empty, the command uses the pattern from the last substitute or :global command. If there is none, but there is a previous search pattern, that one is used. A example can be found with :h :s_r and then a few lines down, beginning with "For :s with an ...


20

There you go : autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call system("tmux rename-window 'vim | " . expand("%:t") . "'") Decomposing : autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call On buffer read, file read or buffer new file event (see :help autocmd-events) execute the next command : call system() Call a system function ...


20

The Unix section of the wiki page on configuration files, leads to the wiki page on run commands, which is where the rc comes from. Quoting from the run commands page: In the context of Unix-like systems, the term rc stands for the phrase "run commands". It is used for any file that contains startup information for a command. It is believed to have ...


19

According to :help cursorline, the highlight label is CursorLine. So you can try: :highlight CursorLine ctermbg=LightBlue See :help highlight-cterm for more options. Experiment with the colours to see which suits you.


19

Can somebody tell me how to avoid the very annoying vim's slowdown? Hopefully in a user friendly non-geeky way? I run Vim on a company-provided 2013 15" Retina MacBook with a 2.3 GHz i7 processor, 16 GB of RAM, and a measly GT 750M with 2 GB dedicated memory. That machine is very far from a "gaming PC" and I've never experienced any slow down. Ever. Even on ...


19

Usually, C-[ is also a way to get out of insert mode. Some people like to map jj in insert mode as a way to exit and return to normal mode. I personally prefer jh as my fingers 'slide' better with this and I don't strain my middle finger much. From somewhere in my neovim settings: inoremap jh <Esc> This remaps jh to escape in insert mode. Is this ...


18

How to deal with repositories-within-repositories has been an ongoing question with git. Git's submodules are one way of addressing the situation, at the expense of adding a little more complexity to keep track of. The git site has an introduction to submodules. The basic idea is to keep a reference to another git repository associated with a path on your ...


18

However after adding these lines into the .vimrc, it didn't work. The reason for this is that Vim clears existing syntax items when setting the 'syntax' option. This is done because keeping the old syntax items would lead to some strange situations; if you have a buffer which has syntax=foo and use set syntax=bar then you'll end up with a buffer which has ...


18

The best way to set an option for a particular filetype is to use autocommands. Here you could add something like that in your .vimrc: autocmd FileType markdown setlocal spell This line will trigger the command setlocal spell when the filetype of a buffer is set as markdown. you can also use the autocommand based on the extension of the file you edit with ...


Only top voted, non community-wiki answers of a minimum length are eligible