Skip to main content
Share Your Experience: Take the 2024 Developer Survey
8 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
  • 20.8k
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
  • 390
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
  • 20.8k
6 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,466
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
  • 28.9k
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
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
  • 26.4k
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,111
5 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 ...
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,268
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
  • 32.1k
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
  • 20.8k
4 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
  • 28.9k
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
  • 26.4k
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
  • 26.4k
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
  • 26.4k
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.9k
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,141
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
  • 28.9k
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
  • 20.8k
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
  • 41k
3 votes
Accepted

Is there any difference in mapping commands with <Cmd> or with :?

Let's go through the basics first… A mapping is a way of telling Vim "when I press these keys, I want you to pretend that I pressed those keys instead". In the example below, I press a but ...
romainl's user avatar
  • 41k
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
  • 666
2 votes
Accepted

Are there any performance difference between :edit and :buffer on existing unchanged buffer?

It's faster as :buf won't reload the file from disk, whereas :edit will, which is different behaviour. This is why it's slightly slower in some cases. For most purposes, this is not a huge issue, ...
Martin Tournoij's user avatar
2 votes

How to tell command-t to ignore a specific directory

What's missing is the leading * in the wildignore value. command-t's help docs have an initially inscrutable, but actually helpful matrix of 'pattern'->'match' patterns, but the sort of buried lede ...
Marcos's user avatar
  • 121
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
  • 28.9k
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
  • 26.4k
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
  • 28.9k

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