Mass
  • Member for 4 years, 5 months
  • Last seen this week
How to find and replace in Vim without having to type the original word?
23 votes

The c_CTRL-R family of maps can improve your workflow quite a bit: You need not type variable names when using :s/, just use CTRL-R CTRL-W to insert the word under the cursor into the command line (...

View answer
Is there a simple equivalent of :g/PATTERN/m0 that doesn't reverse the matched lines?
Accepted answer
21 votes

Instead of moving the target lines up (and reversing them), move all the non-target lines down. Since lines are processed top-to-bottom, the order of non-target lines will be preserved. Also, the ...

View answer
Remove new line from result of system()
Accepted answer
19 votes

If you don't want to use systemlist for whatever reason, you can explicitly remove the newline and/or whitespace. Since vim 8.0.1630 (very recent), there is a trim() function which removes whitespace,...

View answer
How to quickly type a C expression where the assigned-to variable is repeated?
12 votes

You can repeat the last inserted text in insert mode using CTRL-R . or CTRL-A But you need to leave insert mode to complete the change. Enter insert mode and type table[row][col] Then leave insert ...

View answer
Repeat last yank
Accepted answer
11 votes

If you include the flag y in the cpoptions option; set cpoptions+=y then yanks may be repeated using .. See :help cpo-y: *cpo-y* y A yank command can be redone ...

View answer
How to test if certain event is supported?
Accepted answer
11 votes

There are at least three ways to tell if a particular autocmd is supported: Use exists('##CmdlineEnter'). This should be exactly what you need. Check for the patch it was introduced in. For example, ...

View answer
Why Vim sets max text width to 79 instead of 80?
11 votes

It is important to realize that this "default" only applies to the gq and gw commands and auto-format as described in that section. The default textwidth is 0. Furthermore, :right and :center ...

View answer
Ctrl-A and Ctrl-X doing the opposite of what they should
Accepted answer
10 votes

Just use set nrformats+=unsigned. Then vim will treat all numbers as positive and ignore the hyphen.

View answer
Competitive Programming Vimrc is too long
10 votes

set nocp is not actually needed if your vimrc is located at ~/.vimrc or ~/.vim/vimrc. Many of your commands can be abbreviated even further sy on colo torte se rnu nu nowrap hi=1000 cb=unnamed si ts=...

View answer
vim, vimdiff, ex, view, rvim and rview: How do they start in a different way when they are symlinks to the same binary?
Accepted answer
10 votes

Inside vim's source code, there is a main function roughly like int main(int argc, char **argv) { if (!strcmp(argv[0], "ex")) do_ex_setup(); main_loop(); return 0; } This ...

View answer
Delay before running vim command?
Accepted answer
10 votes

In recent vim versions, you can use timer_start to delay an operation. For example, to execute winpos 200 milliseconds after vim starts, autocmd VimEnter * call timer_start(200, { tid -> execute('...

View answer
How to test for :terminal support?
9 votes

You can use if exists(':terminal') to check for existence of the terminal command. This works in both vim and neovim.

View answer
What's the difference between the '\zs' and '\@<=' atoms in Vim regex?
9 votes

Christian has completely addressed the question of buggy behavior of gn, but there still are fundamental differences between \zs and \@<=. The biggest being \@<= modifies a preceding atom, ...

View answer
Trouble using <cword> in mapping
Accepted answer
9 votes

<cword> looks like a key code such as <c-r>, but it is not. As such it is not expanded by nnoremap, but is expanded by :grep and a few other commands which take shell-type arguments. ...

View answer
Is there a way to preview the line where the corresponding open bracket is for a close bracket?
8 votes

Yes- the plugin match-up has this feature: https://github.com/andymass/vim-matchup Using the option let g:matchup_matchparen_offscreen = { 'method': 'popup' } There is also the ability to show the ...

View answer
move forward by a certain number of chars or bytes?
Accepted answer
8 votes

To go to the Nth byte in the file: use :go N, :Ngo, or Ngo (normal mode). To move N bytes ahead, you could use something like this :exe 'go' line2byte(line("."))+col(".")-1+N Unless you have ...

View answer
Why does "thge" correct to "Chge" using spellcheck?
Accepted answer
7 votes

chge is an abbreviation of "change." If you do not like this word, just type zw to mark it as "wrong." vim's spell word lists are obtained from free sources and are sometimes not ...

View answer
Delete entire block, with the prefixes before the opening {
Accepted answer
7 votes

With the cursor on the first line, the proper operation is dV%. The d% means delete from the next { to the matching }. The V forces the d operator to work linewise, rather than characterwise. See (:...

View answer
Registers ” vs @
Accepted answer
7 votes

Note: all of the following information (and much more) can be found using :help @, :help ", :help let-@, and :help expr-register "ap,"ad, &c.: use register a in a put from or ...

View answer
Yank both sides of equation
7 votes

This can be done with either regex+substitute or macros Substitute. This is the same as your regex except the important parts are surrounded by \( . \) to create capture groups. These are referred ...

View answer
Having two modelines in one file, is that possible?
Accepted answer
7 votes

According to the second modeline format (the one with set) listed in :help modeline, you can put any text after the second :. # vim: set autoindent smartindent ts=4 sw=4 sts=4 noet : {# vim: set ft=...

View answer
Unable to set iskeyword for colon ( : ) character
Accepted answer
7 votes

You are looking for set iskeyword-=: That is, making : explicitly not a keyword character. If : is in iskeyword it will be included in ciw.

View answer
How do you make the command mode case insensitive?
Accepted answer
7 votes

You can dynamically toggle smartcase using autocmds, so when in a : command line, it is off and when in a / command line it is on: " assumes set ignorecase smartcase augroup dynamic_smartcase ...

View answer
How to get all/global match results for a string?
Accepted answer
7 votes

An alternative to repeatedly calling match( is to use substitute( with a \= expression: function MatchStrAll(str, pat) let l:res = [] call substitute(a:str, a:pat, '\=add(l:res, submatch(0))',...

View answer
make :terminal use the current pane
Accepted answer
7 votes

You can replace the current window with a terminal using :terminal ++curwin :ter ++curwin " shorter form You can create a mapping or command for convenience nnoremap \t :terminal ++curwin<cr&...

View answer
How to map Alt-Enter
Accepted answer
7 votes

On many terminals (specifically the ones that send alt-modified keys as escape then key), you can map alt+enter in the same way as many other alt combinations, set <a-cr>=^[^M This is two ...

View answer
:exe moves cursor to first line of buffer
Accepted answer
7 votes

In vim, a function which does not explicitly return a value actually returns 0. So, your code is equivalent to exe 0, which is like :0 meaning move the cursor to the first line.

View answer
How to resolve E303 (Unable to open swap file) on Linux machine?
Accepted answer
7 votes

Vim does not interpolate the ~ when using the let & syntax. You must do this manually using expand(). se nobackup writebackup let s:vim_cstmztn_files_dir=expand('~/tools/Vim_Files/') let &...

View answer
How to use 'w' to navigate in CamelCase words?
7 votes

This is not how w and iskeyword works. Consider this example: !his!s!!hort!entence ^^ ^^^ ^ ^^ Pressing w repeatedly, you will see the cursor stops where sequences of !s meet sequences of alpha ...

View answer
How do I replace only the current instance of the search pattern without losing the pattern?
Accepted answer
7 votes

To change the current match (or next match, if the cursor is not on a match) use cgn or gnc. Alone, gn visually selects the match.

View answer
1
2 3 4 5
9