Hot answers tagged

18

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


14

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


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

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


4

According to :h <LocalLeader> <LocalLeader> is just like <Leader>, except that it uses "maplocalleader" instead of "mapleader". So you need let maplocalleader="]"


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

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


4

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


3

In VimScript all variables have a scope. Or, better to say, they are organized into :h Dictionaries with reserved names. So echo g: is a perfectly valid command. There are the following scopes (dictionaries) in no particular order: g: Global s: Script-local (like "static" in C) l: Function-local a: Function argument v: Vim internal b: Buffer local w: ...


3

Based on your other questions, it looks like you are familiar with Python. You can refer to the following gist which compares the various Python programming constructs with the corresponding VimScript constructs: https://gist.github.com/yegappan/16d964a37ead0979b05e655aa036cad0


3

Classical issues, addressed in another Q/A I could copy paste a few feedbacks I gave in another Q/A: https://vi.stackexchange.com/a/11329/626 use guards (this used to be used to permit to inhibit a plugin from being loaded, this is less critical with package/plugin managers nowadays) don't forget the licence avoid global functions -- here everything but ...


3

Ralf’s answer is the correct one, but I’ll add some more generically useful commands: Prepend :vertical to any command which splits and it will use a vertical split Similarly, prepend :tab to any command which splits and it will use a tab These more general commands work for most things (so :tab help does what you thing), so they are good to have in your ...


3

The option is called set nostartofline. However, it's a global which affects many(!) other commands in Vim and I'd rather not suggest to use it.


3

A variable named &name is the Vim script representation of the option name. So &textwidth is the Vim script representation of the option textwidth. See :h :let-&. The let a just prints the value of the variable a. I would prefer :echo a here. Addon: You also might want to read :help internal-variables to understand the name spaces of variables.


3

While answering questions here , I often write little Vim script files to test something. To automatically source the file, I use the following command: command! VimTest autocmd BufWritePost <buffer> redraw! | source % | echo 'Sourced ' . fnamemodify(expand('%'), ':t') When I decide that the current script should be re-sourced on every save, I execute ...


3

In Vim line continuation is done with a backslash on the new line. Like: noremap <C-F12> :!bash -c ' \git rev-parse --show-toplevel \\| xargs -I {} \ ctags -R --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q {}' \<CR> Another point is the usage of | in the command line, as it is a command ...


3

It's possible indeed! As @Biggybi mentioned, you have to use foldtext to achieve that. You assign a function to foldtext, and within this function, you can access the v:foldstart and v:foldend variables. From there, you can access the indentation. Here's a simple example: function! NeatFoldText() let lines_count = v:foldend - v:foldstart + 1 ...


3

The last typed & executed command is stored in the "colon"-register (:h quote:). You can input its value while editing the command-line by pressing Ctrl-R: (:h c_CTRL-R). Or you can just re-run this command by typing :@: in the command-line.


3

but what do these two lines do? let c = nr2char(getchar(0)) return (c =~ a:pat) ? '' : c The first line consumes a character from the typeahead buffer; if the latter is empty, getchar() doesn't wait for a character to be written inside, thanks to the optional 0 argument. The second line spits back the consumed character, but only if it's not a whitespace. ...


2

Running without major issues for a week now so I'm adding this as an alternative answer. I tried all suggestions made here and wherever I could find and the only thing that effectively worked in skipping accessing my networked homefolder was using gvim --clean. Although the help states that --clean is equivalent to -u DEFAULTS -U NONE -i NONE, it is not. ...


2

This behaviour is also in the latest Vim. It seems comments in the form of c text and * text are not recognized at all, only ! text comments are. Looking at $VIMRUNTIME/syntax/fortran.vim it loos like it modifies the syntax highlighting behaviour to match various Fortran dialects. :help ft-fortran-syntax has some more details on this. Adding let g:...


2

Pressing K immediately shows help for the word under cursor (in fact, it execs :h 'keywordprg', but the standard ftplugin sets its value to :help for "vim" files). All :-commands have standardized structure: [range]command[!] [parameters]. Here set and colorscheme are commands, and so they both are followed by space. filetype=markdown is a parameter. Its ...


2

Don't directly define buffer restricted mappings, abbreviations, commands, variables... in the .vimrc. They'll only apply to the first buffer: the one which is active when the .vimrc is loaded. Filetype/syntax related events are triggered after the startup routine described in :h startup


2

Use :normal! for the parts that should be executed in Normal mode. If they include special key codes such as <C-o> or <Esc>, then use :execute so you can use a double quoted string with \<...> sequences for the :normal! command. Putting it all together: function! FormatInput() normal! "*pV']J s/ /,/g execute "normal! i(\<c-...


2

execute() has silent modifier by default, so normal output doesn't appear on the screen. It must be invoked as execute(cmd, '') to allow "echoing". execute() is a sort of :redir by itself (essentially it captures the output into a variable and returns it to the caller), and you cannot do two redirections at a time. It should become clear from reading :h :...


2

The other possibility is to use a logging framework like mine. When working on complex plugins for years it's really game changing. The main feature being: having the logs sent to the quickfix window with the file+line where the log has been done the possibility to choose which module is logging and which isn't (actually this is a consequence of how I use ...


2

As matt points our, just redir: redir > file "... redir END


2

printf() is a function, not a command. And in Vim everything must start with a command. So while calling a function either put it on rhs, or use :call command (when you don't care for return value). But note that printf() is analog to sprintf() in C, so :call printf(...) doesn't make any sense. So you must do :echo printf('Hello, %s', 'name')


2

Nowadays (since vim7), we don't need :source any more -- except for reloading plugins while maintaining them Distribute your scripts within {rtp}/plugin/ -- always loaded once after .vimrc {rtp}/ftplugin/ -- always loaded once per buffer with a matching filetype {rtp}/autoload/ -- lazily loaded, perfect for library plugin Every thing is automatic. See ...


2

I doubt your issue is in the internal termcap database, since you said your issue is specific to insert mode. If Up was ill-defined, one would expect your issue to affect all modes. Anyway, here's one way to reproduce your issue in xterm: vim -Nu NONE +"pu!=['foo', 'bar']" +'ino <esc>O <nop>' +'startinsert' " press: Up " result: 'A' is ...


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