moving text to next line in vi - vim

There is a line in Shell script
f) SCREEN = TRUE
and I want to make it
f)
SCREEN = TRUE
I cannot do this very easily in vi. Obviously in a normal text editor enter command can shift the text after f) to the next line. What is the best way to do it in vi?

Press l to move the cursor right and j to move the cursor down to get the cursor to the S. Then press i to "insert" and hit enter to create a new line. Then hit esc to stop inserting and :wq to save and quit.
There are lots of basic vi command lists on the internet that might help.

I was trying to figure out if there was a way to do this in normal mode without pressing 3 buttons to insert, move the down line with enter and then escape to normal. I believe this is what OP may have been asking for. Not sure why there aren't any built in shortcuts for this.. Anyways I just ended up making a quick mapping in my .vimrc.
nmap <leader>j i<cr><esc>

With your cursor on the line, 0fSi<ENTER><ESC>
(0 Go to beginning of line, fS find 'S', i enter insert mode, <ENTER> insert some kind of newline, <ESC> exit insert mode)

Related

When I search in vim and then press “esc” cursor returns to previous position

Something has seemingly happened to my vim install, and I'm not exactly sure what. I'm a long time vim user (although I don't use it as a main editor).
When I search:
/foo
I want to edit the location that it found, so I press ESC (in preparation for getting into insert mode). vim now jumps BACK to where I started from in the file. E.g. if I was on line 0 of a 3000 line file, I search for a particular string, find it at line 1700, and want to edit it - ESC takes me back to line 0.
What's going on? Did I accidentally set some strange mode? Or did I forget a hotkey combination that I should know?
This is the expected behaviour with the incsearch option on:
Note that the match will be shown, but the cursor will return to its
original position when no match is found and when pressing <Esc>. You
still need to finish the search command with <Enter> to move the
cursor to the match.
If incsearch is not on then the cursor doesn't jump to the first match at all, it doesn't move until you press <Enter>.
you said
so I press ESC (in preparation for getting into insert mode).
you don't need to press ESC before you can get to insert mode, you need to press enter (known in vim as <CR> for carriage return).
so if you wanted to find foo and start inserting text, type
/foo<CR>i
remember that <CR> is a single pressing of the enter button.

How can I move the cursor to the end of the line in insert mode?

Is there an existing command or a configuration for my .vimrc that will make CTRL+right arrow go to the end of the line in insert mode instead of going to the next line? Like in most common editors, that shortcut jumps word by word, but in vim it goes to the next line when the cursor is at the last word in the line.
If there is a different fast way to jump to the end of the line in insert mode, that would also be useful.
In general, the best way to navigate in vim is in normal mode, and not in insert mode. I suggest that instead of finding ways to navigate in insert mode, you use the power of normal mode as intended. There are many ways to do this, but here are a couple of suggestions:
Use CTRL+o while in insert mode to temporarily enter normal mode for the next command, and then $ to go to the end of the line (after which you will be returned to insert mode)
Use ESC to return to normal mode, and then A which both moves the cursor to the end of the line and enters insert mode
If you want to navigate word by word (in normal mode), you can use w to move to the next word, or e to move to the end of the next word.
The Home and End keys are usually interpreted by vim in insert-mode as beginning and end of line (like 0 and $), respectively.
This is documented in the ins-special-special topic, e.g.,
:h ins-special-special

Switch automatically between normal and insert mode in vim

I am new to vim and have installed oh-my-vim and learnt these keybindings by googling:
Jump forward by word - w
Jump forward by word - b
Jump to end of sentence - A
Close current file without exiting - bd
Undo is u
Execute shell command with ! (bang)
of these typing Shift + a in Normal mode allows me to jump to end of sentence and also goes into insert mode soon as I start typing after it. I tried out Shift + w and Shift + b and
it also shows similar behaviour of going to insert mode as soon as I start typing after I use that keybinding.
What are the equivalents for these keybindings where I am not using a letter?
Jump to start of sentence - 0
Redo is Ctrl + R
Jump to previous line - ``
Hope I am clear in describing. Thanks!
I'm also a beginner and I am getting used to Vim recently.
Well, regarding your question, to switch from Normal to Insert, you could press:
i to insert (the cursor stays, effectively inserting to the left)
a to append (the cursor jumps after the character you were on)
I to insert from the beginning of the line. This is equivalent to pressing 0 in Normal mode, and then pressing i to enter Insert mode.
A to append to the end of the line. This is equivalent to pressing $ in Normal mode to get to the end of the line, and then pressing a to append in Insert Mode.
For Insert to Normal:
press <Esc> to get to Normal mode.
press <Ctrl-[> to get to Normal mode.
press <Ctrl-O> to temporarily enter Normal mode for one command. Upon execution it will automatically return to Insert mode.
Personally, to shift between Normal mode, I use <Ctrl-[> for consecutive commands in Normal mode and <Ctrl-O> if it's a one time deal. <Esc> is too far for my pinky :)
It's not entirely clear what you are asking. Shift+W and Shift+B do not automatically enter insert mode, instead they go back and forth on WORDS (vs words).
If you want to enter insert mode, just hit i. You could hit wi to insert after a word.
You mention 0, which goes to the beginning of a line. You can hit I (shift+I) to insert at the beginning of a line. Note that this inserts after the initial whitespace on the line, it is the same as hitting ^i. If you want to insert at the very beginning of the line, you'll need to use 0i.

Opposite of newline in vim

In vim, is there a command to delete the newline, and all empty space behind the cursor?
Say I stand in the middle of a text in insert mode and press Enter, what command would the reverse what I just did?
A) An example:
"some code{ in here }"
B) After pressing Enter:
"some code{
in here }"
Now pressing backspace will delete one space of the indentation. I would rather have it delete all indentation, and jump back to A.
Can this be done in a command or by doing some remapping to the backspace key?
It's tragic how unknown the J command is. It joins lines in normal mode.
In insert mode, you can press <C-U> twice; first, it'll delete the indent before the cursor, then it'll join with the previous line. Note that this requires
:set backspace=indent,eol,start
did you try J (uppercase) ? it will give exactly what you want.
"some code{ cursor on this line, pressJ
in here }"
You can do ᴇꜱᴄ, K, Shift+J.
K jumps up to the previous line and Shift+J joins the two lines.
However, with properly configured indentation and syntax, a backspace doesn’t just delete a space, it deletes the full previous indentation block.
One easy way is up one line, to end of that line and just delete. As long as you still are in insert mode it will do the same thing as J when deleting at the last position - like most other editors. For me that is the quickest alternative because I'm used to it from other editors.
That is: ↑, End, Delete (when still in insert mode)
One quick alternative (the VIM-way) is (when still in insert mode):
↑, Ctrl+o, J (when still in insert mode)
(Ctrl+o is used in insert mode to enter one normal mode command.)
It's also possible to use a remapping of the backspace key:
inoremap <expr> <bs> getline('.')[:col('.')-2]=~'^\s\+$' ? "<c-u><c-u>" : "<bs>"
Note that this mapping completely overrides the normal behavior the backspace key. This will only be useful when you don't intend to use its normal behavior. This is not recommended if you can easily access the other options (c-u or J)
However, (as far as I know) there's no way to distinguish between manually added leading white spaces and auto indent. If you use noexpandtab, you can edit the regex to only match tabs.
This also does not work in some modes of auto-indent (for example, in block comment in C, vim automatically start a new line starts with *)

How do I insert a linebreak where the cursor is without entering into insert mode in Vim?

Is possible to insert a line break where the cursor is in Vim without entering into insert mode? Here's an example ([x] means cursor is on x):
if (some_condition) {[ ]return; }
Occasionally, I might want to enter some more code. So I'd press i to get into insert mode, press Enter to insert the line break and then delete the extra space. Next, I'd enter normal mode and position the cursor before the closing brace and then do the same thing to get it on its own line.
I've been doing this a while, but there's surely a better way to do it?
For the example you've given, you could use rEnter to replace a single character (the space) with Enter. Then, fspace. to move forward to the next space and repeat the last command.
Depending on your autoindent settings, the above may or may not indent the return statement properly. If not, then use sEnterTabEsc instead to replace the space with a newline, indent the line, and exit insert mode. You would have to replace the second space with a different command so you couldn't use '.' in this case.
A simple mapping to break the line at the cursor by pressing Ctrl+Enter:
:nmap <c-cr> i<cr><Esc>
essentially enters 'insert' mode, inserts a line break and goes back to normal mode.
put it in your .vimrc file for future use.
Here's how to create a macro that inserts a newline at the cursor whenever you press 'g' while not in insert mode:
From within vim, type:
:map g i[Ctrl+V][Enter][Ctrl+V][Esc][Enter]
Where:
[Ctrl+V] means hold the Ctrl key and press 'v'
[Enter] means press the Enter key
[Esc] means press the Esc key
You'll see the following at the bottom of your vim window until you press the final Enter:
:map g i^M^[
Explanation:
[Ctrl+V] means "quote the following character" -- it allows you to embed the newline and escape characters in the command.
So you're mapping the 'g' key to the sequence: i [Enter] [Escape]
This is vim for insert a newline before the cursor, then exit insert mode.
Tweaks:
You can replace the 'g' with any character that's not already linked to a command you use.
Add more to the command, e.g. f}i^M^[O -- This will find the } and insert another newline, then escape from insert mode and Open an empty line for you to enter more code.
You can add the command to your .vimrc or .exrc file to make it permanent. Just omit the colon from the beginning, so the command starts with "map"
Enjoy!
If you're usually expanding a one line block to three lines, try substitution. Change the opening bracket into bracket/return, and the closing bracket into return/bracket.
The command for substituting bracket/return for bracket looks like this:
:s/{/{\r/
Since you want to use this often, you could map the full sequence to an unused keystroke like this:
:map <F7> :s/{/{\r/ ^M :s/}/\r}/ ^M
Where you see ^M in the sequence, type [Ctrl-V], then press enter.
Now with your cursor anywhere on your sample line, press the mapped key, and the carriage returns are added.
Check :help map-which-keys for advice on selecting unused keystrokes to map.
Assuming you're okay with mapping K to something else (choose a different key of your liking), and using marker ' as a temporary marker is okay why not do this?
:nmap K m'a<CR><Esc>`'
now pressing K in normal mode over the character after which you want the line break to occur will split the line and leave the cursor where it was.
Basically, when you split a line you either want to just insert a carriage return, or in the case that you're on a space, replace that with a carriage return. Well, why settle for one or the other? Here's my mapping for K:
"Have K split lines the way J joins lines
nnoremap <expr>K getline('.')[col('.')-1]==' ' ? "r<CR>" : "i<CR><Esc>"
I use the ternary operator to condense the two actions into one key map. Breaking it down, <expr> means the key map's output can dynamic and in this case hinges on the condition getline('.')[col('.')-1]==' ' which is the long winded way to ask vim if the character under the cursor is a space. Finally, the familiar ternary operator ? : either replaces the space with linebreak (r<CR>) or inserts a new one (i<CR><Esc>)
Now you have a lovely sister key map to the J command.
Vim will automatically kill any whitespace to the right of the cursor if you break a line in two while autoindent (or any other indentation aid) is enabled.
If you do not want to use any of those settings, use s instead of i in order to substitute your new text for the blank rather than just inserting. (If there are multiple blanks, put the cursor on the leftmost and use cw instead.)
In fact you need the following combined operations:
Press v to enter Visual Mode
Select the line you want to split
Press : to enter in Command Mode
s/\s/\r/g
Done
If you have the input:
aaa bbb ccc ddd
and want to output
aaa
bbb
ccc
ddd
You can use the command
f r<ENTER>;.;.
o ESC command will do it for you.
Set this key mapping in your vimrc
:map <C-m> i<CR><Esc>h
Then press Ctrl+m if you want to use it in your vim.
IMHO, the built-in mapping gs is not a useful mapping (put vim to sleep), one could use this for splitting:
nmap gs i<CR><ESC>
In Vrapper you can use gql which will split a line without entering insert mode, but may not always maintain indentation.
I found this to be the most faithful implementation of what I'd expect the opposite behaviour to J
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
It does the simplistic new line at cursor, takes care of any trailing whitespace on the previous line if there are any present and then returns the cursor to the correct position.
i <cr> <esc> - this is one of the most common solutions suggested, it doesn't delete non-whitespace characters under your cursor but it also leaves you with trailing whitespace
^mw - goto start of new line and create a mark under w
gk - go up one line
:silent! s/\v +$//<cr> - regex replace any whitespace at the end of the line
:noh<cr> - Clear any search highlighting that the regex might have turned on
`w - return the the mark under w
Essentially combines the best of both r<esc><cr> and i<cr><esc>
Note: I have this bound to S which potentially overwrites a useful key but it is a synonym for cc and since I don't use it as often as I do splits I am okay with overwriting it.
This mapping will break up any one-line function you have. Simply put your cursor on the line and hit 'g' in normal mode:
:map g ^f{malr<CR>`a%hr<CR>`a
This assumes that you have a space after the opening brace and a space before the closing brace. See if that works for you.

Resources