How to use vi to edit a command in terminal on Linux? - linux

When typing a very long command, I'd like to first edit the command in a text editor(e.g. vi) then execute in case of typos. Is there a way to edit the command directly in a terminal and run instead of invoke vi by typing vi then type the command?

If you're using bash, try the edit-and-execute-command command. By default, this is assigned to Ctrl-x Ctrl-e (type ctrl-x, then ctrl-e).
This should open whatever editor is specified in your environment. Whatever is in the buffer when you exit will execute in the shell - including multiple-line commands.

You can by setting vi editing mode. If you are using bash, you can enter the following or put it in your shell configuration files:
set -o vi
You can then, just like in vi use command mode and insert mode.
When a command is already on the line (called up by pressing the up arrow key, CTRL-R, fzf, etc.), a useful command is v when in normal mode on the shell, because it will start the default editor to edit the command.
A cheat sheet from this gist:
.---------------------------------------------------------------------------.
| |
| Readline VI Editing Mode |
| Default Keyboard Shortcuts for Bash |
| Cheat Sheet |
| |
'---------------------------------------------------------------------------'
| Peteris Krumins (peter#catonmat.net), 2008.01.08 |
| http://www.catonmat.net - good coders code, great reuse |
| |
| Released under the GNU Free Document License |
'---------------------------------------------------------------------------'
======================== Keyboard Shortcut Summary ========================
.--------------.------------------------------------------------------------.
| | |
| Shortcut | Description |
| | |
'--------------'------------------------------------------------------------'
| Switching to COMMAND Mode: |
'--------------.------------------------------------------------------------'
| ESC | Switch to command mode. |
'--------------'------------------------------------------------------------'
| Commands for Entering INPUT Mode: |
'--------------.------------------------------------------------------------'
| i | Insert before cursor. |
'--------------+------------------------------------------------------------'
| a | Insert after cursor. |
'--------------+------------------------------------------------------------'
| I | Insert at the beginning of line. |
'--------------+------------------------------------------------------------'
| A | Insert at the end of line. |
'--------------+------------------------------------------------------------'
| c<mov. comm> | Change text of a movement command <mov. comm> (see below). |
'--------------+------------------------------------------------------------'
| C | Change text to the end of line (equivalent to c$). |
'--------------+------------------------------------------------------------'
| cc or S | Change current line (equivalent to 0c$). |
'--------------+------------------------------------------------------------'
| s | Delete a single character under the cursor and enter input |
| | mode (equivalent to c[SPACE]). |
'--------------+------------------------------------------------------------'
| r | Replaces a single character under the cursor (without |
| | leaving command mode). |
'--------------+------------------------------------------------------------'
| R | Replaces characters under cursor. |
'--------------+------------------------------------------------------------'
| v | Edit (and execute) the current command in the text editor. |
| | (an editor defined in $VISUAL or $EDITOR variables, or vi |
'--------------'------------------------------------------------------------'
| Basic Movement Commands (in command mode): |
'--------------.------------------------------------------------------------'
| h | Move one character right. |
'--------------+------------------------------------------------------------'
| l | Move one character left. |
'--------------+------------------------------------------------------------'
| w | Move one word or token right. |
'--------------+------------------------------------------------------------'
| b | Move one word or token left. |
'--------------+------------------------------------------------------------'
| W | Move one non-blank word right. |
'--------------+------------------------------------------------------------'
| B | Move one non-blank word left. |
'--------------+------------------------------------------------------------'
| e | Move to the end of the current word. |
'--------------+------------------------------------------------------------'
| E | Move to the end of the current non-blank word. |
'--------------+------------------------------------------------------------'
| 0 | Move to the beginning of line |
'--------------+------------------------------------------------------------'
| ^ | Move to the first non-blank character of line. |
'--------------+------------------------------------------------------------'
| $ | Move to the end of line. |
'--------------+------------------------------------------------------------'
| % | Move to the corresponding opening/closing bracket. |
'--------------'------------------------------------------------------------'
| Character Finding Commands (these are also Movement Commands): |
'--------------.------------------------------------------------------------'
| fc | Move right to the next occurance of char c. |
'--------------+------------------------------------------------------------'
| Fc | Move left to the previous occurance of c. |
'--------------+------------------------------------------------------------'
| tc | Move right to the next occurance of c, then one char |
| | backward. |
'--------------+------------------------------------------------------------'
| Tc | Move left to the previous occurance of c, then one char |
| | forward. |
'--------------+------------------------------------------------------------'
| ; | Redo the last character finding command. |
'--------------+------------------------------------------------------------'
| , | Redo the last character finding command in opposite |
| | direction. |
'--------------+------------------------------------------------------------'
| | | Move to the n-th column (you may specify the argument n by |
| | typing it on number keys, for example, 20|) |
'--------------'------------------------------------------------------------'
| Deletion Commands: |
'--------------.------------------------------------------------------------'
| x | Delete a single character under the cursor. |
'--------------+------------------------------------------------------------'
| X | Delete a character before the cursor. |
'--------------+------------------------------------------------------------'
| d<mov. comm> | Delete text of a movement command <mov. comm> (see above). |
'--------------+------------------------------------------------------------'
| D | Delete to the end of the line (equivalent to d$). |
'--------------+------------------------------------------------------------'
| dd | Delete current line (equivalent to 0d$). |
'--------------+------------------------------------------------------------'
| CTRL-w | Delete the previous word. |
'--------------+------------------------------------------------------------'
| CTRL-u | Delete from the cursor to the beginning of line. |
'--------------'------------------------------------------------------------'
| Undo, Redo and Copy/Paste Commands: |
'--------------.------------------------------------------------------------'
| u | Undo previous text modification. |
'--------------+------------------------------------------------------------'
| U | Undo all previous text modifications. |
'--------------+------------------------------------------------------------'
| . | Redo the last text modification. |
'--------------+------------------------------------------------------------'
| y<mov. comm> | Yank a movement into buffer (copy). |
'--------------+------------------------------------------------------------'
| yy | Yank the whole line. |
'--------------+------------------------------------------------------------'
| p | Insert the yanked text at the cursor. |
'--------------+------------------------------------------------------------'
| P | Insert the yanked text before the cursor. |
'--------------'------------------------------------------------------------'
| Commands for Command History: |
'--------------.------------------------------------------------------------'
| k | Move backward one command in history. |
'--------------+------------------------------------------------------------'
| j | Move forward one command in history. |
'--------------+------------------------------------------------------------'
| G | Move to history line N (for example, 15G). |
'--------------+------------------------------------------------------------'
| /string or | Search history backward for a command matching string. |
| CTRL-r | |
'--------------+------------------------------------------------------------'
| ?string or | Search history forward for a command matching string. |
| CTRL-s | (Note that on most machines Ctrl-s STOPS the terminal |
| | output, change it with `stty' (Ctrl-q to resume)). |
'--------------+------------------------------------------------------------'
| n | Repeat search in the same direction as previous. |
'--------------+------------------------------------------------------------'
| N | Repeat search in the opposite direction as previous. |
'--------------'------------------------------------------------------------'
| Completion commands: |
'--------------.------------------------------------------------------------'
| TAB or = or | List all possible completions. |
| CTRL-i | |
'--------------+------------------------------------------------------------'
| * | Insert all possible completions. |
'--------------'------------------------------------------------------------'
| Miscellaneous commands: |
'--------------.------------------------------------------------------------'
| ~ | Invert case of the character under cursor and move a |
| | character right. |
'--------------+------------------------------------------------------------'
| # | Prepend '#' (comment character) to the line and send it to |
| | the history. |
'--------------+------------------------------------------------------------'
| _ | Inserts the n-th word of the previous command in the |
| | current line. |
'--------------+------------------------------------------------------------'
| 0, 1, 2, ... | Sets the numeric argument. |
'--------------+------------------------------------------------------------'
| CTRL-v | Insert a character literally (quoted insert). |
'--------------+------------------------------------------------------------'
| CTRL-r | Transpose (exchange) two characters. |
'--------------'------------------------------------------------------------'
===========================================================================
.---------------------------------------------------------------------------.
| Peteris Krumins (peter#catonmat.net), 2008.01.08. |
| http://www.catonmat.net - good coders code, great reuse |
| |
| Released under the GNU Free Document License |
'---------------------------------------------------------------------------'

If you are using zsh the shell-command is called edit-command-line. It is not bound by default, so add something like this to your configuration:
bindkey "^X^E" edit-command-line
Now Ctrl+xCtrl+e will work the same way as in bash except that the command is not executed before Return is struck.

Have you tried the fc ("fix command") shell built-in?1
By default, it opens the very last command in your editor, but you can discard that and replace it with whatever you wish, and it gets executed on exit. See help fc.
Idea suggested by Tom Ryder in his blog post, Vi mode in Bash.
(1) Available in fish, zsh, bash, dash, but probably in others as well.

If you are using the fish shell instead of bash you can open the current command in an editor with:
Alt-v (which uses the editor set in the VISUAL environment variable)
Alt-e (which uses the editor set in the EDITOR environment variable)

Related

Vim: How to open new vertical split for file under cursor

I know gf opens the file under cursor, and CTRL-w f opens the file under cursor in a new split window.
I'm probably being greedy but, how do you open it in a new vertical window?
From normal mode, you could type C-w f C-w L or C-w v gf, and from the command-line you could execute :wincmd f | wincmd L or :vert wincmd f.
They are not strictly equivalent, though.
With C-w f C-w L and :wincmd f | wincmd L, the height of your vertical viewport will be maximized no matter the current layout. So, for example, if you have a horizontal viewport below, its width should be decreased:
+-----------------+ +--------+-------+
| path/to/fileC | | | |
| | → | | |
| fileA | | fileA | fileC |
+-----------------+ +--------+ |
| | | | |
| fileB | | fileB | |
+-----------------+ +--------+-------+
With C-w v gf and :vert wincmd f, the height of your vertical viewport should be the same as the current one, and thus it shouldn't affect the horizontal viewport below:
+-----------------+ +--------+-------+
| path/to/fileC | | | |
| | → | | |
| fileA | | fileA | fileC |
+-----------------+ +--------+-------+
| | | |
| fileB | | fileB |
+-----------------+ +----------------+

vim: "extend" window into next window

Let's say I have this layout in vim:
+-----+-----+-----+
| | | |
| a | | |
| | | |
+-----+ c | d |
| | | |
| b | | |
| | | |
+-----+-----+-----+
is it possible to end up with this layout:
+-----+-----+-----+
| | | |
| a | c | |
| | | |
+-----+-----+ d |
| | |
| b | |
| | |
+-----+-----+-----+
like an "extend right" command ?
PS. Don't tell me to use ctrl-w J on b then ctrl-w L on d, my layout is actually more complex then this. I'm really looking for an "extend right" command if it exists.
This is hard to implement as a feature since there can be a lot of edge case scenarios to consider, also why vim doesn't do this natively.
If you don't like the option of using ctrl-w J on b then ctrl-w L on d then the simplest approach would be to close the c split and then open a new vertical split on a and open the buffer / file within it that you desired.
This is brittle, but probably better than nothing!
function! ExtendRight()
let l:start=winnr()
exe "normal \<c-w>l"
let l:shrink=bufnr('%')
close
exe "normal " . l:start . "\<c-w>w"
exe "normal \<c-w>k"
vsplit
exe "b " . l:shrink
endfunction
You can map it with nnoremap <c-w>e :call ExtendRight()<CR>.

is there a way to move vim windows in this way?

I have four windows like this:
| | | |
| A | | |
|_________| C | D |
| B | | |
| | | |
So, how can I change them into this way?
| | |
| A | C |
|_________|______|
| B | D |
| | |
the
ctrl+W/J/K/L
thing is always moving the window to the far other side, I can't use them to do this.
Thanks!
what I can think of is two steps:
move cursor to C, press C-W c to close the window
move cursor to D, press :sp #<enter>
The idea is, close one window(buffer), and reopen it in right place (by sp or vs).
glad to know if there is easier way.

How to move vertical split window to horizontal split, when there is an existing horizontal split?

In Vim I tend to open buffers in new vertical splits (with the occasional horizontal split). I keep my code to 80 chars wide, so this works pretty well on large monitors.
I often end up with this window arrangement:
---------------------
| | | | |
| | | | |
------ | A | B |
| | | | |
| | | | |
---------------------
At four or five columns wide, it can start getting a bit too narrow, so then I want to move the windows around so it looks like this:
----------------
| | | A |
| | | |
------ ------
| | | B |
| | | |
----------------
As far as I know, this is impossible to do by moving the windows in Vim.
The only way to get that window arrangement I've found, is to close window A, and then re-open A as a new horizontal split from window B.
Is that correct, or is there a way to move/re-arrange windows like that in Vim? Maybe a plugin?
I'm yet to find anything, so I thought I would ask because I find the opening/closing of windows anoying and breaks my flow.
FWIW, I find the Ctrl-W + J / Ctrl-W + K shortcuts useless, because they make the new horizontal split as wide as the whole screen, rather than splitting with the neighbouring window. I.e. Ctrl-W + J would give me this:
----------------
| | | |
| | | |
------ | A |
| | | |
| | | |
----------------
| |
| B |
----------------
Which is generally never what I want.
If anyone has some ideas, let me know!
There's a plugin that can do exactly what you want. Here's the link : https://github.com/fabi1cazenave/suckless.vim.

How to search for the Nth match in a line in Vim?

I am editing a wiki file and would like to add a new column in between of two existing columns.
| *No* | *Issue* | *File* | *Status* |
| 1 | blah | foo | open |
| 2 | blah1 | foo1 | close |
Say, I want to insert a new column between the 3rd and 4th columns above. If I could search for the fourth match of the | character in a given line, I could replace that with | |. But how one can do that in Vim?
The end result would look like so:
| *No* | *Issue* | *File* | | *Status* |
| 1 | blah | foo | | open |
| 2 | blah1 | foo1 | | close |
How about recording a macro into register q by entering qq3f|a|<ESC>q in command mode (ESC means pressing the Escape key). Now you can apply this macro to each line by :%norm#q.
Additional bonus:
With this pattern you can add more complex actions, for example replicate the first column as column 3 (if cursor is at first column):
qqf yf|;;;p0q
Oh, and the answer to your question: Search 4th occurrence of | on a line is done by 3f| (if the cursor is at position 0 and on a | character as in your example).
Consider the following substitution command.
:%s/\%(.\{-}|\)\{4}\zs/ |/
:%s/\(|[^|]*\)\{3\}/&| /
Which means: on each line (%), find three occurrences (\{3\}) of a string that starts with | followed by any number of non-| ([^|]*), and replace that with itself (&) followed by |.
You can call sed in vim as a filter:
:%!sed 's/|/| |/4'

Resources