As of May 31, 2023, we have updated our Code of Conduct.

Hot answers tagged

7 votes
Accepted

How can I alias a command with arguments?

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 ...
Matt's user avatar
  • 19.5k
7 votes
Accepted

Keep specific parts of a :s command in nvim

Yes and no. Depends a lot on the complexity of the documents. Textile has a lot of various formats for links and Markdown has it's share. Keeping pattern When you do a substitute you can capture parts ...
Moba's user avatar
  • 380
6 votes
Accepted

Is it possible to have vim run a command every time save `:w` is invoked?

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 ...
Matt's user avatar
  • 19.5k
5 votes
Accepted

Define w< command

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 ...
filbranden's user avatar
  • 27.5k
5 votes
Accepted

creating a ":KeepCursor" command

Can it be written? Yup. command! -nargs=* -complete=command KeepCursor \ let [s:view, s:win] = [winsaveview(), win_getid()] | \ try | \ execute <q-args> | \ finally | ...
Peter Rincker's user avatar
5 votes

how does vim make the difference for "i" between the insert mode and the inner word

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 ...
Zorzi's user avatar
  • 1,101
5 votes
Accepted

how does vim make the difference for "i" between the insert mode and the inner word

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 ...
Quasímodo's user avatar
  • 2,386
4 votes
Accepted

What does the command option '-range=N' do?

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 "...
D. Ben Knoble's user avatar
  • 23.9k
4 votes
Accepted

How to add modifier to user defined command?

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' ...
dedowsdi's user avatar
  • 6,058
4 votes
Accepted

How to define a command if the function needs to take an argument from the script itself?

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 ...
Rich's user avatar
  • 30.4k
4 votes
Accepted

'@' in file names and 'gf' (go to file)

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 ...
Matt's user avatar
  • 19.5k
4 votes
Accepted

How do I quit vi/Vim/NeoVim?

In short, hit the Esc key to make sure you're in "Normal" mode, then type :q (which will appear on the last line of the screen) and press the Enter. If Vim complains that you have unsaved ...
3 votes
Accepted

Why does `<Bar>` behave like `|` in the `:command` command in Vim?

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 ...
filbranden's user avatar
  • 27.5k
3 votes
Accepted

text replace problem when target contains '&' symbol

& 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)/...
D. Ben Knoble's user avatar
  • 23.9k
3 votes
Accepted

Repeating command for running external script?

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. ...
D. Ben Knoble's user avatar
  • 23.9k
3 votes
Accepted

Print Command Without Hitting Enter

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 '...
D. Ben Knoble's user avatar
  • 23.9k
3 votes

creating a ":KeepCursor" command

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 ...
Christian Brabandt's user avatar
3 votes
Accepted

How to pass everything from a command into a function with escaped characters

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 ...
Jürgen Krämer's user avatar
3 votes
Accepted

set a default value to a command that acccepts two arguments?

Here's the general idea... func! MakeHLine(...) abort " Default delim char and width let l:ch = '#' let l:tw = 78 " Override defaults per given parameters if a:0 ...
B Layer's user avatar
  • 19.1k
3 votes
Accepted

Append inside brackets, quotes, double quotes

There's not a command that follows the ci... structure to do that. You can add a remap to do it (see below), or use core vim commands f and t (and their counterparts F and T) motions to get the cursor ...
mattb's user avatar
  • 1,071
3 votes

How can I make a command that checks iteratively through parent roots until it finds something or reaches C:/?

You can use the findfile() function, together with the file-searching features in order to try to find the path to a file up the directory tree. For example: :echo findfile('top-level.txt', '.;') ...
filbranden's user avatar
  • 27.5k
3 votes
Accepted

Neovim E276 - Command works in Vim but Not in Neovim

In VimScript the function composition can be written in two ways, either "traditional" g(f(x)), or "method" notation f(x)->g(). However, the latter one must be defined by ...
Matt's user avatar
  • 19.5k
3 votes
Accepted

Passing arguments to function in remapping

Those are mappings, not "remappings". You can use :help getcharstr() to, well… get one character from the user: xnoremap <expr> <key> Wrap("w", getcharstr()) nnoremap &...
romainl's user avatar
  • 36.9k
2 votes
Accepted

How to use variables in a user defined command?

Can simply use exec and . to build the command: let PathToPluginsFolder = ~/.vim/plugins command! PS silent! exec ':w | PlugSnapshot! ' . PathToPluginsFolder . '/plugins.lock'
ruohola's user avatar
  • 626
2 votes
Accepted

In command mode make delete not act like backspace

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 ...
filbranden's user avatar
  • 27.5k
2 votes

In command mode make delete not act like backspace

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 ...
D. Ben Knoble's user avatar
  • 23.9k
2 votes
Accepted

execute + colon character doesn't work

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 ...
filbranden's user avatar
  • 27.5k
2 votes
Accepted

What does + preceding command do in vim

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.
Rich's user avatar
  • 30.4k
2 votes
Accepted

Making custom command behave like ":echo" with respect to quotes

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 (...
Matt's user avatar
  • 19.5k
2 votes
Accepted

Is it possible to execute keys/commands in select mode?

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, ...
filbranden's user avatar
  • 27.5k

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