Hot answers tagged

5

The reason why the approach using user commands (with :command!) does not work is that user commands are quite restricted in how they are allowed. They must always start with an uppercase letter (so they don't clash with Vim native commands) and their names may not include special characters (such as <.) Instead of user commands, you can use :cabbrev to ...


5

Can it be written? Yup. command! -nargs=* -complete=command KeepCursor \ let [s:view, s:win] = [winsaveview(), win_getid()] | \ try | \ execute <q-args> | \ finally | \ if win_id2win(s:win) | \ call win_gotoid(s:win) | \ endif | \ keepjumps call winrestview(s:view) | \ endtry Can it ...


5

Does something like this exist? In Vim this is called an autocommand. The relevant help topic can be found by typing :h autocommand (sic!) An example code to put into vimrc: augroup typescript_save | au! autocmd BufWritePost *.ts !tsc <afile>:p:S augroup end


5

Vim keeps what you type in memory, even when it seems to have no effect on the screen, this allows you to apply modifiers to commands. To see what modifiers you typed, and will be applied to the next command, you can use :set showcmd. They will appear at the bottom right of the screen. Another good example of that effect is how G will take you to the end of ...


5

This is the description of c in the help file: ["x]c{motion} Delete {motion} text [into register x] and start insert. The register is optional, but the motion is mandatory. A lone c makes no sense. In other words, we say that c triggers the operator-pending mode. Therefore the distinction between a normal mode i and an operator-pending mode i is ...


4

You can use :h <mods> to do this, it expands to modifier or nothing. It's added at patch 7.4.1898 command Foo call FooSplit(<q-mods>) function FooSplit(mods) exe a:mods 'new' endfunction Above example use <q-mods> instead of <mods>, as you need it as a string.


4

The problem is that, in the body of your EchoMessage() function, text does not refer to the value passed in, but instead to a different local variable. Use a: to refer to parameters passed in: echom a:text See :help internal-variables (and specifically :help function-argument) for more details.


4

it doesn't seem to pass in the argument Because you haven't passed it. The right syntax is: command! -nargs=1 Vres vertical resize <args> Simply read :help :command until the very end to know all the options.


3

Using the -range flag when defining a user-command usually allows the user to provide a range to the command: command -range Mine echo <range> <line1> ',' <line2> %Mine 1,/bar/Mine " the next one is equivalent to .Mine by default Mine However, sometimes you want to be able to do 0Mine Or else use the number provided as a <count>: ...


3

So I went digging for this one and as far as I can tell this behavior has been present ever since the user-defined command feature was introduced in Vim 5.2. Initially I thought this was present because a user comand takes its arguments from <...> special sequences, such as <args> or <bang>, which makes it so that it takes <lt> to ...


3

& in a replacement is a bit special: magic nomagic action & \& replaced with the whole matched pattern *s/\&* But you can escape it with \: %substitute/\Vif(cond1)/if(cond1\&\&cond2)/g


3

Based on :help v:count: *v:count* *count-variable* v:count The count given for the last Normal mode command. Can be used to get the count before a mapping. Read-only. Example: > :map _x :<C-U>echo "the count is " . v:count<CR> < Note: The <C-U> is required to remove the line range ...


3

Can it be written? Possibly, but it might be a lot harder than expected. Because, what do you do with commands that close the current window? Or commands, that add lines above the current cursor position? One problem you are running into is described unter :h function-search-undo. The last used search pattern and the redo command "." will not be changed ...


3

I’d recommend an <expr> mapping—make the following changes: nnoremap <expr> <Leader>b BuildSystemTask() And then in your function: if inProject() return ':make! ' else return '' An alternative would be to use the feedkeys() function


3

You must add -nargs=1 to your command: :command -nargs=1 FooCommand :call Foo(<q-args>) This tells Vim not to treat white space as separator. Only white space between the command name and the first non-white space character is removed from the argument passed to Foo(). All other spaces and tabs, even trailing ones, are included in the argument.


2

Can simply use exec and . to build the command: let PathToPluginsFolder = ~/.vim/plugins command! PS silent! exec ':w | PlugSnapshot! ' . PathToPluginsFolder . '/plugins.lock'


2

Am I missing something here? Yes, as written in :h 'isfname': If the character is '@', all characters where isalpha() returns TRUE are included. Normally these are the characters a to z and A to Z, plus accented characters. To include '@' itself use "@-@". So @ in isfname, iskeyword etc. actually means "all alphas", not literal "@". And you still ...


2

To change the behavior of <Del> so that it only deletes the character under the cursor and doesn't behave like backspace when at the end of the line, you can use the following mapping: cnoremap <expr> <Del> getcmdpos() <= strlen(getcmdline()) ? "\<Del>" : "" See @D.BenKnoble's answer for a solution to deleting until the end of ...


2

You can use the following function: function KillEnd() let l:cmd = getcmdline() let l:pos = getcmdpos() let l:newcmd = strpart(l:cmd, 0, l:pos - 1) return l:newcmd endfunction To use it on the command line, you need to do (I choose <C-x> arbitrarily since it wasn't used for anything else, and reminds me of X in normal mode): cnoremap <C-x&...


2

You're missing a final <CR> at the end of your normal command. If at the end of normal you have an incomplete command, it will cancel it as if <Esc> or <C-c> was pressed. So this should fix it: execute "normal! :'<,'>retab\<CR>" (Using \r inside the double quotes is also possible.) But in this case, there's probably no need ...


2

The + isn't preceding the setlocal command, so much as following the :new command: it's running the setlocal command in the newly created window. See :help :new and :help +cmd for more details.


2

For this particular task it's much much easier to do simply :filter /txt$/ oldfiles The error is due to -bar argument: the quote is parsed like a comment sign, and so the rest of the command is lost (see :h :command-bar, :h :quote). Either always escape all quotes (and bars) with backslashes, or remove -bar argument from the command definition.


2

If you're using the VS Code extension that I think you are then my understanding is that it supports at least some plugins and one of those is vim-surround. If that's the case and you're open to using plugins just jump to the last paragraph. Otherwise, read on. From Normal mode with the cursor on the first character of the word you could do... i"<C-O&...


2

You can give a try to vim-surround plugin. To put a word into quotes you will go ysiw'. The plugin gives you the power to: add (ysiw") word -> "word" change (csiw"') 'word' delete (ds') word the surroundings not only of sigle words but of all kinds of text objects: surround words inside parentheses (ysi)") def func(some ...


2

The video shows Vim, not Neovim. In Neovim the output of "bang" is not colorized. If you like long stories you can read this and this and so on. TL;DR they think it's not a big deal, but fixing "bang" may be too hard, so just use :terminal instead.


2

While I'm not entirely sure deleting this command is a great idea you can do it using an autocommand like this in your vimrc: augroup deletePluginCommand autocmd! autocmd VimEnter * delcommand Windows augroup END If you are not familiar with autocommands, they are a way to trigger some code on an event (see :h autocmd) and here we use the event ...


2

No, you have to write such "option parser" yourself. But quite probably there's no need to do this. Just re-write your function to accept dictionary argument. For example, function! SubmitJob(opts) abort let l:opts = extend(copy(a:opts), #{hours: 1, cpus: 1}, 'keep') echo l:opts.hours l:opts.cpus endfunction :call SubmitJob(#{cpus: 2}) &...


1

By keys I mean like being Normal mode and pressing e (takes you to end of [next] word), by commands I mean like being in Normal mode and pressing Shift+; and typing :delete. Yes. From Select mode, you can use CTRL-O to switch to Visual mode for the duration of a single command. So you can use <C-o>e to select until the end of the word, or <C-o>:...


1

If you want the user to be able to enter a sequence such as r\<cr> or r\r and have Vim interpret that the same way as it interprets sequences in a double quoted string, the easiest way to do so is to actually assemble it into a double quoted string and use eval() to have Vim interpret it. In your particular case: execute "normal! " . eval('&...


1

The official response from Junegunn is to put the following in your vimrc: command! -nargs=* W w This will make :W behave like :w, while still allowing you to use :Wi (or any other shortening of :Windows) if you want to.


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