48 votes
Accepted

Change an entire line in Vim

In the same way that yy copies the current line and dd deletes it, you can use cc to change it (i. e. yank it, delete it and enter insert mode). S is a synonym for cc.
Tae's user avatar
  • 2,125
39 votes

How to insert a newline without leaving normal mode

If you don't want to add mappings or a package dependency, you can just yank yy an empty line and then pasted p for below cursor and P for above the cursor.
redeemefy's user avatar
  • 491
38 votes
Accepted

Math calculations in Vim?

For this to work, you need to use the expression register. Which allows you to execute arbitrary vimscript and do something about the result of the evaluation. To use the expression register on insert ...
rbernabe's user avatar
  • 801
35 votes
Accepted

How to use a variable in the expression of a 'normal' command?

The execute function takes a string as argument, it expands the string and execute it a a regular ex command. Thus you can do: function! MyFunction(someArg) execute "normal! ". a:someArg. "l" ...
statox's user avatar
  • 49.8k
35 votes
Accepted

Simplest way to delete a "composite" word?

What you are calling a composite word is actually a WORD (by opposition to a word). Reading :h word and :h WORD should be helpful: *word* A word consists of a sequence of ...
statox's user avatar
  • 49.8k
31 votes

Splitting a line into two

For your specific case, when you have a space, that you also like to get rid of, I usually use rEnter
Christian Brabandt's user avatar
24 votes
Accepted

What is the meaning of 'g'?

g is a little bit of a weird key in vim. A lot of other keys have a specific word to describe their category: h, j, k, l, }, {, w, e, etc. are all motions. They tell your cursor to move. d, c, y, etc....
DJMcMayhem's user avatar
  • 17.6k
22 votes

What is the mnemonic for Ctrl-Y (in normal mode)?

I like to call <C-y> "Yester-line"™ like "Yesterday". Explaining it along with <C-e> rhymes, too. "Extra-line" and "Yester-line"™.
ddrscott's user avatar
  • 431
22 votes
Accepted

What is the purpose of `s`?

I don't think there's a more specific use mode than to delete the character under the cursor and enter insert mode. By using a number modifier you can delete several characters before entering insert ...
pfnuesel's user avatar
  • 724
20 votes
Accepted

Delete to EOL without entering insert mode

Yes. You want to use the "delete" operator instead of the "Change" operator. The delete operator is d<motion> and it deletes everything that <motion> moves over. In this case, the ...
DJMcMayhem's user avatar
  • 17.6k
20 votes

Delete n lines skip 1 line script

Very simple approach: Move to the first line you want to delete. Record a macro: qa3ddjq Repeat it with a high number: 1000@a Step three will repeat the macro a thousand times or until an error is ...
Ralf's user avatar
  • 9,197
18 votes
Accepted

How can I delete whole word in command insert mode?

If you press Ctrl-U all characters to the left of the cursor until the beginning of the commandline will be deleted. This is also explained in the help at :h c_CTRL-U To delete a single word, you ...
Christian Brabandt's user avatar
18 votes

Delete n lines skip 1 line script

The easiest solution to me would be: :%norm j3dd That is: %: for every line norm: run the following keys as if in normal mode j3dd: go down one line then delete 3 lines So from the first line, go ...
Marth's user avatar
  • 356
16 votes

What is the purpose of `s`?

s is short for cl which is mentioned in help s: ["x]s Delete [count] characters [into register x] and start insert (s stands for Substitute). Synonym for "...
laktak's user avatar
  • 2,943
16 votes
Accepted

How to delete entire function definition (C style)

Motion ][: next } in the first column V][d does what you want. More details can be found under :h ][ and :h section: *section* [...] The "]" and "[" ...
Hotschke's user avatar
  • 4,750
15 votes
Accepted

How does p command work in vim?

p and P always work the same way: p puts text after the cursor, P puts text before the cursor. But what you put with p and P can be "character-wise", "line-wise", or "block-wise". This means that ...
romainl's user avatar
  • 40.6k
15 votes
Accepted

Which text object defines text enclosed in space?

I believe you are looking for the WORD text object. From :h WORD: A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a ...
Tumbler41's user avatar
  • 7,746
14 votes
Accepted

What is the use case for gs?

Short answer: None. That is, I believe it is a waste to map the easily accessible keys gs to sleep. With that said, I often use the the :sleep command when I do testing and debugging of vimscript ...
Karl Yngve Lervåg's user avatar
14 votes

Change an entire line in Vim

cc does what you want, I think? From :help cc: ["x]cc Delete [count] lines [into register x] and start insert |linewise|. If 'autoindent' is on, preserve the indent of ...
Martin Tournoij's user avatar
13 votes
Accepted

Continue an ex mode command after "norm"?

If you have a sequence of keystrokes that you want to execute in normal mode from the command line, you can use the :normal command. However, by default the :normal command can't be followed by ...
saginaw's user avatar
  • 6,756
13 votes
Accepted

Set line numbers only while in normal mode

You can do this with autocommands. au InsertEnter * set nonumber au InsertLeave * set number Not much explanation is needed. This does exactly what you asked for. It ties "entering and exiting ...
DJMcMayhem's user avatar
  • 17.6k
13 votes
Accepted

How to replace a word with spaces

Move cursor anywhere on the word "test" and then viwr (the last one is a space). Saves at least two key strokes. v start visual selection iw text object "inner word" (see :help text-object, read ...
Ralf's user avatar
  • 9,197
12 votes
Accepted

Vim normal and insert mode cursor not changing in GNU screen

I think I figured it out, although I don't totally understand the fix. This appears to work for mintty, xterm, and rxvt: " Set up vertical vs block cursor for insert/normal mode if &term =~ "...
ZeroG's user avatar
  • 491
12 votes

What is the purpose of `s`?

In normal mode: s same as cl S same as cc In visual mode: s same as c S is remapped by vim-surround to a (very useful) surround operator. I don't see any clear advantage of having s for substitute ...
Miguel's user avatar
  • 221
12 votes
Accepted

How to copy lines from 4th till 11th and paste it from 15th till 22th line in ex mode?

I'm not sure if I understand you correctly but is this what you're looking for? :4,11t 15 This will copy lines from 4 to 11 and paste it on line 15. See :h co for more.
dkns's user avatar
  • 599
12 votes

How to execute a command on vim startup

In my opinion, the cleanest method of doing this is simply to add the command to a file in your .vim/vimfiles directory: ~/.vim/after/ftplugin/python.vim or for Windows: $HOME\vimfiles\after\...
Rich's user avatar
  • 31.9k
11 votes

Uncomment multiple lines

I do approve @mMontu comment suggesting to use a comment plugin (vim-commentary is an option but NERDCommenter has my preference over vim-commentary). But you could do it in other ways: First if all ...
statox's user avatar
  • 49.8k
11 votes

Simplest way to delete a "composite" word?

Another more general solution is to delete up to the whitespace character using dt (with a space after the t), which means "delete to [character]. This often is useful for things like dt: and similar ...
allo's user avatar
  • 231
11 votes
Accepted

Ctrl-A and Ctrl-X doing the opposite of what they should

Just use set nrformats+=unsigned. Then vim will treat all numbers as positive and ignore the hyphen.
Mass's user avatar
  • 14.1k
10 votes
Accepted

How to repeat last action with movement?

From the repeat documentation : . Repeat last change, with count replaced with [count]. Also repeat a yank command, when the 'y' flag is included in 'cpoptions'. ...
fruglemonkey's user avatar
  • 1,530

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