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,085
39 votes
Accepted

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

Bill Joy and Mark Horton wrote in their original vi manual: If you want to see more of the file below where you are, you can hit ^E to expose one more line at the bottom of the screen, leaving the ...
Martin Tournoij's user avatar
36 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
  • 461
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.4k
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.4k
34 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
  • 761
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
22 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.4k
21 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
  • 421
21 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
  • 704
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.4k
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,097
19 votes

How I apply a command to all lines?

You are looking for the :> command and a range of 1,$ (% for short) for the whole file. :%> For more help see: :h :> :h :range
Peter Rincker'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
17 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
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,640
15 votes
Accepted

How do I flip characters or words like emacs' transpose?

For characters, it's fairly simple: xp to swap the letter under the cursor with the following letter, and Xp to swap the letter under the cursor with the previous letter. The x command deletes the ...
Wildcard's user avatar
  • 4,364
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
  • 39.3k
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,706
15 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,898
14 votes
Accepted

Is there a way to insert a single character and then exit insert mode?

If there isn't (I haven't looked), you can use this mapping in your .vimrc: nnoremap <C-I> i <ESC>r It inserts, places a space, ESCapes, and starts a single letter replace. This gets ...
Qix - MONICA WAS MISTREATED's user avatar
14 votes

How to replace text inside () with previously yanked text?

You can use visual selection to select the text to replace, and then paste over it as follows: vi(p vi( selects Baz (because it is enclosed in parenthesis), p pastes Fnord over it. However, this ...
Alex's user avatar
  • 1,293
14 votes
Accepted

How to delete till the end of the previous line (i.e. to end of line above)?

Try d0kJx Deletes backwards to the beginning of the line, moves up, then joins the two lines and then removes the space in between the two joined lines. You don't have to leave normal mode with this....
Bryan Glazer'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

How to delete till the end of the previous line (i.e. to end of line above)?

Well, you can combine the "backward search" motion and the delete operator: d?$<Enter>
VanLaser's user avatar
  • 9,620
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,706
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.4k
13 votes
Accepted

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
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

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