14 votes
Accepted

Is there a command to enter Visual Block mode?

There is no built in command to start visual block mode in vim, but you can define one yourself: command! Vb normal! <C-v> Here is a breakdown of how it works: command! Vb - This creates a ...
13 votes
Accepted

Passing visual range to a :command as its argument

See this answer on stackoverflow. The example given is: function! PrintGivenRange() range echo "firstline ".a:firstline." lastline ".a:lastline " Do some more things endfunction ...
  • 46.8k
10 votes

Is there a command to enter Visual Block mode?

As far as I can find, there is no built-in command to start visual mode. However, you can easily add these commands to Vim: :command! Visual normal! v :command! VisualLine normal! V :command! ...
8 votes
Accepted

What is the purpose of user `command` in Vi/Vim?

Let's get this confusion out of the way first: Are these ex commands or Vimscript? Vim scripts are made of ex commands. From :help script: Your first experience with Vim scripts is the vimrc file....
  • 1,377
7 votes
Accepted

Better "bro ol"

This should work: command! Bro :enew | setl buftype=nofile | 0put =v:oldfiles \| nnoremap <buffer> <CR> gf | 1 :enew doesn't support +cmd, unlike :vnew, so just split the :setl out to ...
  • 24k
6 votes
Accepted

Filtering text out to new tab with custom command

You can create a new empty buffer in a new tab and then populate it with the result of a grep search on the previous file: :tabnew|0r!grep -n pattern # In a mapping: nnoremap <key> :tabnew\|...
  • 33.3k
5 votes
Accepted

How Can I Rename a Command but Still Use the Original Command

If you want to keep your habit typing tabclose you could use: cnoreabbrev tabclose TabClose Or as mentioned by @Rich to avoid that the abbreviation triggers in search or as part of other commands: ...
5 votes

Filtering text out to new tab with custom command

First clear an register: qaq second, append all matching lines using the :g command: :g/pattern/y A Third, put all matches on a new page :tabnew +$put\ A If you want to be able to make changes on ...
5 votes

How to define a custom user defined command to filter a range?

Ranges are not forwarded automatically, you need to use <line1> and <line2> for that: command! -range Ex <line1>,<line2>!expand -t4 You should read the manuals of :help :...
5 votes

Using arguments in a .vimrc command

If you are using the :%s command, you are already using some very simple VimScript. So much for not learning it :) If you want to create your custom command, you can use e.g. com! -nargs=1 Replace :%...
4 votes
Accepted

How to edit a file in a new tab?

You probably want to read :h user-commands and put the following in your vimrc: command! -nargs=1 -complete=file E tabnew <args> You can't override :e but you can create the command :E which ...
  • 46.8k
4 votes

How to create user command without a capital letter as first letter?

Why Commands (custom as well as built-in) are used in mappings, scripts, plugins. If you could override them, it would be very easy to break functionality. There is a mechanism for customization ...
4 votes
Accepted

How to write a user-defined command in vim?

It looks like you are trying to use :command like :map to map a series of keys. But they are different. In a user-defined command, you don't need : to enter into command-line mode and you don't need &...
3 votes

How to create user command to delete current file and saveas to new file?

If plugins are an option, tpope's eunuch plugin offers :Move to anywhere and :Rename within the current directory commands, among many others.
  • 21.8k
3 votes

How to create user command to delete current file and saveas to new file?

Use <args> when referencing command arguments. <args> will expand to the arguments passed to the command. Also, while not strictly required, because you always want to pass one argument to ...
3 votes
Accepted

Why only part of a command is executed

Your problem is that :command does not (by default) accept the use of bars, as explaind in :h :bar. You may circumvent this by using the -bar option, see :h :command-bar: command! -bar Tags execute '!...
3 votes

Passing visual range to a :command as its argument

you can also adapt this one liner command -range TmuxSession execute "!tmux.session " . @* or if you do need multiline visual selection change @* to substitute(@*,"\n"," ","g") The @* register ...
  • 213
3 votes
Accepted

Is it possible to non-recursively remap a command?

I don't believe it's possible to redefine :Glog without losing access to the script-local function. However, bearing in mind your muscle memory is for glog, not Glog, I propose another approach: ...
  • 28.9k
3 votes
Accepted

How do I remap the :make command to run a docker container?

Using 'makeprg': set makeprg=docker\ run\ --rm\ -v\ $(pwd):/src\ container\ make Unfortunately, the \ are necessary for set to keep the spaces; single quoting won't work here. Now you can run :make ...
  • 21.8k
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'
  • 596
2 votes

How to edit a file in a new tab?

Like everyone else, I recommend pretty strongly against doing this, but if you're dead set on the idea (and for future readers who might be doing something slightly different) you could use something ...
  • 28.9k
2 votes
Accepted

Mapping using tpope/vim-surround only works with command

EDIT: Your mapping doesn't work because you are in a rare case where you want to use nmap instead of nnoremap: You want to use in a mapping an already defined mapping. Using nnoremap you said to Vim "...
  • 46.8k
2 votes
Accepted

Custom Arguments to User Command

You can use the -complete=... flag while defining a user-command to program how tab completion should work when the command is used. See :help :command-completion for more details about that flag. The ...
  • 26.5k
2 votes

How to make an user-defined command that works in normal and visual mode?

Alternately, keep your original Fill definition, and always type (or enter from a mapping) :call Fill() When in normal mode, this works fine (as you’ve observed). From visual mode, with a mapping or ...
  • 21.8k
2 votes
Accepted

How to make an user-defined command that works in normal and visual mode?

You are almost there. I would do it the following: Define the command as taking an explicit range: command -range Fill :<line1>,<line2>call Fill() This will call the function with ...
2 votes
Accepted

How do I call a function from a user command? (Getting a "trailing character" error, E488)

First thing you need to do is get the syntax of :command right. This is what it's supposed to be... :com[mand][!] [{attr}...] {cmd} {rep} {attr} means attributes. That includes the -nargs value. ...
  • 18.5k
1 vote
Accepted

How can I execute commands or move to other tabs without first closing the terminal executed by `:term`?

In Neovim there's only one key combination not passed to the underlying terminal process. From :h terminal-input To send input, enter Terminal-mode using any command that would enter "insert mode" ...
  • 17.9k
1 vote

Passing visual range to a :command as its argument

You can also copy the part you want to use and use <C-r>" to paste it. For example, if you want to run myCommand like feh, firefox etc on a the line :map mapping Vy:!myCommand <C-r>"<Cr&...

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