Hot answers tagged

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


16

In command-line, you can use ctrl-r (see :h c_ctrl-r) to start inserting the content of a register. In your case, you could paste the content of the last search with: <c-r>/ Another way would be to use :h incsearch to show the matches to the pattern as you type (in a search, or a :s command, and so on), so you wouldn't have to "check" for your regex ...


10

set nocp is not actually needed if your vimrc is located at ~/.vimrc or ~/.vim/vimrc. Many of your commands can be abbreviated even further sy on colo torte se rnu nu nowrap hi=1000 cb=unnamed si ts=4 sts=4 sw=4 sr is hls nn <CR> :noh<CR><CR> ino {<CR> {<CR>}<C-o>O


8

Is there any advantages or side-effects of an explicit set nocompatible that I'm perhaps missing? Actually, there are many side-effects. Every time compatible is set or reset Vim rescans all options (except "terminal") and switches defaults when necessary. After that it rebuilds quite a few internal tables for iskeyword, spelling, vartabs etc. (see ...


7

The first line really should be runtime defaults.vim. This is a standard file and it must be present in all Vim installations. Among other things, it saves from typing syntax on filetype plugin indent on set hi=200 set is Also set cb=unnamed is evil (just prepend "+ before normal mode command when needed) and set hls IMO is more harmful (every time ...


6

One reason you might want to include a guarded set nocompatible in your .vimrc is that compatible will not be automatically unset if you specify the vimrc with the -u flag: Using the "-u" argument with another argument than DEFAULTS has the side effect that the 'compatible' option will be on by default. It's for this reason that I have this version* in ...


6

You can control that with the 'iskeyword' option. To include dashes, you can use this command: set iskeyword+=- You'll probably want to use setlocal and limit that to the specific filetype where you want dashes to be recognized as identifiers. You can create a file ~/.vim/after/ftplugin/<your-filetype>.vim with that setlocal command to ensure - is ...


6

There are several approaches: Open the file As others have explained, unless the author doesn't follow conventions, the function scope1#scope2#...#scopen#funcname() is supposed to be defined in {rtp}/autoload/scope1/scope2/.../scopen.vim file. Where {rtp} designates any directory in 'runtimepath' list option. This option is usually automatically handled ...


6

The command is called finish. So it's pretty trivial if !has('nvim') " Stop sourcing here if regular vim is sourcing this file finish endif


5

Old one, but this has been added as per patch 8.1.0729 Problem: There is a SourcePre autocommand event but not a SourcePost. Solution: Add the SourcePost autocommand event. (closes #3739)


5

Variables are not expanded in the context of a set command. So use let: let &titlestring = $TTY . ' %-25.55F %a%r%m' set titlelen=17


5

Regarding the two variables: $term is a variable from the environment. When you are on the terminal (without opening Vim), echo $term should echo the same as :echo $term from inside Vim. (I'm a little bit surprised that it is not upper case, like $TERM.) &term represents the option term (see :h 'term'). So the output from :echo &term is the same as ...


5

The :syntax command is global. Probably you're looking for the "local to buffer" setting 'syntax' (abbreviation: 'syn'). To disable for current file: set syn=OFF per :h 'syn' though set syn= and set syn& seem to work, too. You can use this with a filetype auto command like so: autocmd FileType python set syn=OFF Each time you open a Python file it ...


5

Use append(), it's silent and it doesn't move the cursor -- As I said in your review question, I found using :normal & co convoluted to alter buffer content. Vim functions have less side effects. :nnoremap <silent> T :<c-u>call append('.', '')<cr> It can even be used to insert multiple lines: https://vi.stackexchange.com/a/9720/626 :...


5

reset option to its default value (without having to exit/re-enter vim) There is :set-&vim, but it will really reset to default value, not to the value that could have been set from a standard indent plugin. :set foldtext&vim 1.view what the default value is for a option? I'm not aware of any simple way, except :h '{optionname}'. You can always ...


5

Yes, using :h :windo: nnoremap <C-a> :windo set relativenumber!<CR>


5

Count must precede object (i} in this case). Hence, the right command is y3i}


5

You can either set 'textwidth' to 0 and then gq{movement} will line break at screen width or you can set it to desired width and disable autowrapping by changing 'formatoptions' (alias 'fo') like so... set fo-=tc Per :h fo-table: With 't' and 'c' you can specify when Vim performs auto-wrapping: value action "" no automatic formatting (you ...


5

Just like this let @+ = &statusline Or like this call setreg("+", getbufvar("%", "&statusline")) Or even like this... if has("nvim") call provider#clipboard#Call("set", [[matchstr(execute("set statusline"), "=\@=.*"], "v", "+"]) endif


4

Just stumbled across this question/answer, and I think it's a terrific idea, with an ingenious solution! However, while I think the existing answer is very clever, something about it doesn't sit well with me. I don't like the fact that, while in one sense the disabling of 'virtualedit' is automatic, it's not fully automated: it relies on the user using one ...


4

Ftplugin As mentioned do put settings pertaining to a certain file type in ftplugin. Separate into Files Break up code into different files. For example where my vimrc file is, there is a sub init/ dir which contains other files I wish to load at startup. To load them I use the below code. <sfile> evaulates to the current file (init.vim or .vimrc in ...


4

" tell which cinoption take effect for current line com! WhichCinoption call s:which_cinoption() function s:which_cinoption() abort " test if cindent take effect if !empty(&indentexpr) echo "'indentexpr' exists, it overrides 'cindent'." return endif if !&cindent echo "'cindent' is currently disabled." return endif " ...


4

You could use the plugin called vim-closetag which does exactly this. The big advantage over the default omni-completion is you don't need to type as much. The matching closing tag will be automatically inserted after your cursor. Just type > by default to insert a new line between those new tags, pretty convenient.


4

In vim, you can complete open html tags and close tags using omni-completion, invoked via ctrl-xctrl-o. To do this though, you must first type < or </ depending on which tag you want. For close, the tag corresponding to the nearest unclosed open tag will be chosen, for example: <div> <p>asdf</p> </[] <-- PRESS ctrl-x ctrl-...


4

The opposite of ; is , to find the previous occurrence. It works with both t and f searches. Of course, this will be inverted with T and F. Thus, to go backwards, you could choose to only use t and f together with , : this is as many keystrokes as using capital letters and keeps cycling consistent in every case.


4

The easiest is probably to select the word to be replaced in visual mode instead of deleting it: viwp If you don’t like that, an alternative is to delete without clobbering your yanked text by deleting into the black hole register: "_diwp Another alternative is to delete normally, but then paste from the yank register, which isn’t overwritten by your ...


4

You can and add this to your init.vim: augroup RestoreCursorShapeOnExit autocmd! autocmd VimLeave * set guicursor=a:hor20 augroup END See :h guicursor for details. The reason it isn't done automatically is there's no way to query the terminal cursor shape


4

For this particular case where $TTY is constant and not likely to change, @Ralf's answer of using let &titlestring to compose the setting string value is recommended. Another possible approach is to use a %{...} group inside the setting value, which will evaluate it every time the setting is used. In this case, this would work: set titlestring=%{$TTY}\...


4

Taking a word or otherwise long argument (longer than a single char) from a mapping usually involves prompting the user, using the input() function. But that's often not very convenient, as you're somewhat slowed down by the prompting and it's also hard to invoke or reuse that mapping from a non-interactive context. You'll probably find that using a user-...


4

You may have an interfering plugin. To be sure, most plugins do not expect vim to start in insert mode (although many won't care either). By default, vim +startinsert does almost what you expect. It starts insert mode, but does not clear the intro message. To also prevent the intro message, use shortmess: vim +'set shm+=I' +startinsert Sometimes, a ...


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