Terminals that allow basic text editing of commands - macos

Is there a terminal application, particularly for Mac, that allows commands' text to be edited as in a word processor?
For example, in most terminals I know of, a user can't use ALT+BACKSPACE, can't remove more than one character at a time, can't cut, copy, or paste easily (if at all), can't use CMD+ARROW to go from one end of a long command to another quickly, etc., etc.
P.S. Is there a reason why editing text commands in a terminal is so (universally) cluncky?

In bash, you can
move to the beginning of the line with Ctrl-A
move to the end of the line with Ctrl-E
delete a whole word with Esc and then Backspace
These and other routines come from Emacs. If you learn the basics of how to use that, then the shell will become very intuitive.
As for copy and paste, these are handled by the terminal rather than the shell. In many environments, just highlight the text with your mouse to copy, and then paste with either the middle button or right button of your mouse. In the Mac terminal, you can use the common copy and paste routines.

Related

Combining the kill-ring and clipboard in Aquamacs on OSX

#lawlist asked for more details so I am re-writing the question:
My goal is to treat ^X and ^K as two different ways to cut text into a SINGLE
logical clipboard that can be pasted using ^V in emacs or in any other app.
Suppose there are two lines in the emacs buffer:
I am line one
I am line two
and I press ^K on the first line, then the kill ring AND the OS-level clipboard
BOTH now have 'I am line one' by pressing ^K two more time, then the OS-level clipboard will be updated again to have "I am line one\nI am line two"
Pressing ^V in emacs or in other applications will cause that text to be pasted into them. (^Y can continue to have its original behavior, or not, I don't care)
Right now, ^K is bound to org-kill-line or just kill-line depending on which edit mode I am in.
I don't think there is an existing emacs command to do this, but
if only I knew the functions to call, I bet it is trivial to simply
update the OS clipboard after every ^K.
Any help would be great.
thanks,
dan
I think you are looking for save-interprogram-paste-before-kill. Try adding this
(setq save-interprogram-paste-before-kill t)
to your init.el

Bash shell command inline correction

When typing a (long) command in the bash shell, if you were to make a mistake early in the line, is there a way to correct that mistake without having to navigate back to it? For example, lets say you have just entered something like this, but not yet pressed return:
git commit =m 'Some really long commit message, perhaps spanning multiple lines'
where you have accidentally typed = instead of -, would it be possible to append something to the end of the command before you press enter that would perform an in-line substitution to correct the mistake? This would be really handy to avoid having to do something annoying like
Pressing Ctrl+C and then rebuilding the command using a combination of copy and paste.
Pressing the left arrow a huge bunch of times so that it can be corrected before pressing enter.
One solution (in theory) would be to pipe the contents of the whole command through sed, however I am not sure how to capture the command as a string of text that could then be used in this manner.
What I would do : ctrl+a
then move the cursor after the =, then hit ctrl+w.
The latest delete the previous word.
Multiple shortcuts can help here:
Use ctrl+a to go to the beginning of the line
Use alt+b and alt+f to move forward and backward one word at a time
Use alt+e to open an editor ($EDITOR) containing your current command, edit it, then close your editor.

.vimrc to behave like Textmate?

I am making the switch to VIM from Textmate and I wanted to know what should be included in my .vimrc to get the similar behavior for:
auto-close on brackets, paranthesis, etc.
auto tabbing after a bracket
when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line
pressing <tab> on for will generate a common 'for' use
Thanks.
The first thing to be aware is that you can't turn Vim into TextMate. The second, is that you shouldn't even try.
Instead, focus your efforts on learning Vim, progressively, and grow/shrink your collection of settings and plugins as you need. Using Janus or some other "distro" will only occult Vim behind other people's arbitrary choices.
That said,
"auto-close on brackets, paranthesis, etc."
can be done in a "dumb" way by adding this line to your ~/.vimrc:
inoremap ( ()<Left>
inoremap is for mapping in insert mode (note the i)
( is the key you want to press, that's your trigger
()<Left> means input a pair of parenthesis then go back one character, between the parenthesis
If you need a "smarter" way, there are a many plugins to choose from.
You might want to try surround which was in and of itself a very compelling reason for me to switch from TextMate.
"when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line"
can be done in many ways as well, for example:
inoremap <C-Return> <CR><CR><C-o>k<Tab>
inoremap, again
<C-Return> means Ctrl+Return, that's your trigger
<CR><CR> means 2 carriage returns to push the closing bracket 2 lines below
<C-o> to leave insert mode only for one command
k to go up one line
<Tab> to put the insertion cursor at the right place
Mappings in Vim can be just that: shortcuts for sequences of keypresses or more serious scripts.
"pressing <Tab> on for will generate a common 'for' use"
can be done with a dedicated plugin like SnipMate. There are others.
auto-close on brackets, paranthesis, etc.
There are a number of plugins for this functionality: autoclose, closepairs, simplepairs
auto tabbing after a bracket
By this, I believe you mean automatic indentation. This is a default Vim behavior when you're working on a supported filetype. If you're just starting on a file and haven't saved it, you can manually set the filetype with :set ft=<whatever filetype you want> and you'll get indentation and syntax highlighting.
when i open a bracket and select enter, the open bracket is on 1st line, the cursor on the 2nd line indented, and the closing bracket on the 3rd line
This will be taken care of with the above auto-close plugins.
pressing tab on for will generate a common 'for' use
These are called "snippets" in TextMate. Snipmate is a commonly used Vim plugin for snippets. Other people prefer xptemplate.
If you're moving from TextMate to Vim, I would try installing Janus and thoroughly reading the documentation.
Janus is a great starter distribution of plugins and mappings for Vim, gVim, and MacVim. It features sane defaults and aims to provide a minimal working environment using the most popular plug-ins and the most common mappings. It was developed and is maintained by Carl Lerche and former TextMate user Yehuda Katz.
My own personal experience/advice is that you can really drive yourself crazy trying to mimic every feature of your favorite editor exactly as it was. Each feature you're trying to replicate is probably just a quirk you became used to through muscle memory. You'll be much more productive if you just start "accepting" Vim, learning it, and re-train your muscle memory for it.

How to use vim in the terminal?

How does one setup and start using vim in the terminal on OS X?
I want to start writing my C code using vim in the terminal rather than a separate text editor. How does one get started on this?
The basics like: opening, creating, saving files via terminal using vim and writing code using vim. Also, does one compile directly using vim in the terminal?
Get started quickly
You simply type vim into the terminal to open it and start a new file.
You can pass a filename as an option and it will open that file, e.g. vim main.c. You can open multiple files by passing multiple file arguments.
Vim has different modes, unlike most editors you have probably used. You begin in NORMAL mode, which is where you will spend most of your time once you become familiar with vim.
To return to NORMAL mode after changing to a different mode, press Esc. It's a good idea to map your Caps Lock key to Esc, as it's closer and nobody really uses the Caps Lock key.
The first mode to try is INSERT mode, which is entered with a for append after cursor, or i for insert before cursor.
To enter VISUAL mode, where you can select text, use v. There are many other variants of this mode, which you will discover as you learn more about vim.
To save your file, ensure you're in NORMAL mode and then enter the command :w. When you press :, you will see your command appear in the bottom status bar. To save and exit, use :x. To quit without saving, use :q. If you had made a change you wanted to discard, use :q!.
Configure vim to your liking
You can edit your ~/.vimrc file to configure vim to your liking. It's best to look at a few first (here's mine) and then decide which options suits your style.
This is how mine looks:
To get the file explorer on the left, use NERDTree. For the status bar, use vim-airline. Finally, the color scheme is solarized.
Further learning
You can use man vim for some help inside the terminal. Alternatively, run vimtutor which is a good hands-on starting point.
It's a good idea to print out a Vim Cheatsheet and keep it in front of you while you're learning vim.
Run vim from the terminal. For the basics, you're advised to run the command vimtutor.
# On your terminal command line:
$ vim
If you have a specific file to edit, pass it as an argument.
$ vim yourfile.cpp
Likewise, launch the tutorial
$ vimtutor
You can definetely build your code from Vim, that's what the :make command does.
However, you need to go through the basics first : type vimtutor in your terminal and follow the instructions to the end.
After you have completed it a few times, open an existing (non-important) text file and try out all the things you learned from vimtutor: entering/leaving insert mode, undoing changes, quitting/saving, yanking/putting, moving and so on.
For a while you won't be productive at all with Vim and will probably be tempted to go back to your previous IDE/editor. Do that, but keep up with Vim a little bit every day. You'll probably be stopped by very weird and unexpected things but it will happen less and less.
In a few months you'll find yourself hitting o, v and i all the time in every textfield everywhere.
Have fun!
if you want to open all your .cpp files with one command, and have the window split in as many tiles as opened files, you can use:
vim -o $(find name ".cpp")
if you want to include a template in the place you are, you can use:
:r ~/myHeaderTemplate
will import the file "myHeaderTemplate in the place the cursor was before starting the command.
you can conversely select visually some code and save it to a file
select visually,
add w ~/myPartialfile.txt
when you select visualy, after type ":" in order to enter a command, you'll see "'<,'>" appear after the ":"
'<,'>w ~/myfile $
^ if you add "~/myfile" to the command, the selected part of the file will be saved to myfile.
if you're editing a file an want to copy it :
:saveas newFileWithNewName
If you want to learn by reading yourself:
Open MacOS terminal app.
Write this and press enter -> vimtutor
For quit write this and click -> :q

Fastest way(s) to move the cursor on a terminal command line?

What is the best way to move around on a given very long command line in the terminal?
Say I used the arrow key or Ctrl-R to get this long command line:
./cmd --option1 --option2 --option3 --option4 --option5 --option6 --option7 --option8 --option9 --option10 --option11 --option12 --option13 --option14 --option15 --option16 --option17 --option18 --option19 --option20 --option21 --option22 --option23 --option24 --option25 --option26 --option27 --option28 --option29 --option30 --option31 --option32 --option33 --option34 --option35 --option36 --option37 --option38 --option39 --option40 --option41 --option42 --option43 --option44 --option45 --option46 --option47 --option48 --option49 --option50
Now I need to move (starting from the beginning or the end of the line) the cursor to --option25 to modify something there.
What is the fastest way to get there? What I usually do is Ctrl-A to get to the beginning and then repeatedly Alt-F to move forward, word by word (or Ctrl-E to go the end and Alt-B to then go backward). But on a long line that takes too much time. There must be a way to search and jump directly to the part I need to modify, e.g. option25?
To be clear, you don't want a "fast way to move the cursor on a terminal command line".
What you actually want is a fast way to navigate over command line in you shell program.
Bash is very common shell, for example.
It uses Readline library to implement command line input. And so to say, it is very convenient to know Readline bindings since it is used not only in bash. For example, gdb also uses Readline to process input.
In Readline documentation you can find all navigation related bindings (and more):
http://www.gnu.org/software/bash/manual/bash.html#Readline-Interaction
Short copy-paste if the link above goes down:
Bare Essentials
Ctrl-b Move back one character.
Ctrl-f Move forward one character.
[DEL] or [Backspace] Delete the character to the left of the cursor.
Ctrl-d Delete the character underneath the cursor.
Ctrl-_ or C-x C-u Undo the last editing command. You can undo all the way back to an empty line.
Movement
Ctrl-a Move to the start of the line.
Ctrl-e Move to the end of the line.
Meta-f Move forward a word, where a word is composed of letters and digits.
Meta-b Move backward a word.
Ctrl-l Clear the screen, reprinting the current line at the top.
Kill and yank
Ctrl-k Kill the text from the current cursor position to the end of the line.
M-d Kill from the cursor to the end of the current word, or, if between words, to the end of the next word. Word boundaries are the same as those used by M-f.
M-[DEL] Kill from the cursor the start of the current word, or, if between words, to the start of the previous word. Word boundaries are the same as those used by M-b.
Ctrl-w Kill from the cursor to the previous whitespace. This is different than M- because the word boundaries differ.
Ctrl-y Yank the most recently killed text back into the buffer at the cursor.
M-y Rotate the kill-ring, and yank the new top. You can only do this if the prior command is C-y or M-y.
M is Meta key.
For Max OS X Terminal you can enable "Use option as meta key" in Settings/Keyboard for that.
For Linux its more complicated.
Update
Also note, that Readline can operate in two modes:
emacs mode (which is the default)
vi mode
To switch Bash to use vi mode:
$ set -o vi
Personaly I prefer vi mode since I use vim for text editing.
Bonus
In macOS Terminal app (and in iTerm too) you can Option-Click to move the cursor (cursor will move to clicked position). This even works inside vim.
Since this hasn't been closed yet, here are a few more options.
Use Ctrl+x followed by Ctrl+e to open the current line in the editor specified by $FCEDIT or $EDITOR or emacs (tried in that order).
If you ran the command earlier, hit Ctrl+r for a reverse history search and type option25 (in this case). The line will be displayed. Hit Tab to start editing at this point.
Use history expansion with the s/// modifier. E.g. !-2:s/--option25/--newoption/ would rerun the second-to-last command, but replace option25. To modify the last ./cmd command, use the !string syntax: !./cmd:s/--option25/--newoption/
Any delimiter may be used in place of / in the substitution.
If editing the previous line, you can use quick substitution: ^--option25^--newoption
Character search. This was mentioned by Pax, and can be done in regular emacs-mode with Ctrl+] for forward search, and Ctrl+Alt+] for backward search.
I recommend the second option. Ctrl+r is really handy and fast, no mucking about with editors, and you see the results before the command is run (unlike the history expansions).
Hold down the Option key and click where you'd like the cursor to move, and Terminal rushes the cursor that precise spot.
I tend to prefer vi editing mode (since those keystrokes are embedded into my spinal cord now (the brain's not used at all), along with the CTRL-K, CTRL-X from WordStar 3.3 :-). You can use the command line set -o vi to activate it (and set -o emacs to revert).
In Vi, it would be (ESC-K to get the line up first of course) "f5;;B" (without the double quotes).
Of course, you have to understand what's on the line to get away with this. Basically, it's
f5 to find the first occurrence of "5" (in --option5).
; to find the next one (in --option15).
; to find the next one (in --option25).
B to back up to the start of the word.
Let's see if the emacs aficionados can come up with a better solution, less than 5 keystrokes (although I don't want to start a religious war).
Have you thought about whether you'd maybe like to put this horrendously long command into a script? :-)
Actually, I can go one better than that: "3f5B" to find the third occurrence of "5" then back up to the start of the word.
Use Meta-b / Meta-f to move backward/forward by a word respectively.
In OSX, Meta translates as ESC, which sucks.
But alternatively, you can open terminal preferences -> settings -> profile -> keyboard and check "use option as meta key"
After running the command once, run fc
It will launch $EDITOR with the previous command, then you can use your regular editor to modify the command. When you save and exit, the file will be executed.
..but, as Pax said - the command line isn't particularly good for editing absurdly long lines - why not make the command into a script?
If you want to move forward a certain number of words, hit M-<n> (M- is for Meta and its usually the escape key) then hit a number. This sends a repeat argument to readline, so you can repeat whatever command you want - if you want to go forward then hit M-<n> M-f and the cursor will move forward <n> number of words.
E.g.
$|echo "two three four five six seven"
$ M-4
(arg: 4) echo "two three four five six seven"
$ M-f
$ echo "two three four| five six seven"
So for your example from the cursor at the beginning of the line you would hit, M-26 M-f and your cursor would be at --option25| -or- from the end of the line M-26 M-b would put your cursor at --|option25
Incremental history searching
in terminal enter:
gedit ~/.inputrc
then copy paste and save
"\e[A": history-search-backward
"\e[B": history-search-forward
"\e[C": forward-char
"\e[D": backward-char
all you need to do to find a previous command is to enter say the first 2 or 3 letters and upward arrow will take you there quickly say i want:
for f in *.mid ; do timidity "$f"; done
all i need to do is enter
fo
and hit upward arrow command will soon appear
It might not be the fastest, but this need to be here, some reading about ANSI cursor movements
ANSI escape sequences allow you to move the cursor around the screen at will. This is more useful for full screen user interfaces generated by shell scripts, but can also be used in prompts. The movement escape sequences are as follows:
- Position the Cursor:
\033[<L>;<C>H
Or
\033[<L>;<C>f
puts the cursor at line L and column C.
- Move the cursor up N lines:
\033[<N>A
- Move the cursor down N lines:
\033[<N>B
- Move the cursor forward N columns:
\033[<N>C
- Move the cursor backward N columns:
\033[<N>D
- Clear the screen, move to (0,0):
\033[2J or \033c
- Erase to end of line:
\033[K
- Save cursor position:
\033[s
- Restore cursor position:
\033[u
(...)
Try putting in the following line of code at the prompt (it's a little clearer what it does if the prompt is several lines down the terminal when you put this in): echo -en "\033[7A\033[1;35m BASH \033[7B\033[6D" This should move the cursor seven lines up screen, print the word " BASH ", and then return to where it started to produce a normal prompt.
Examples:
Move the cursor back 7 lines:
echo -e "\033[7A"
Move the cursor to line 10, column 5:
echo -e "\033[10;5H"
Quickly echo colors codes, to colorize a program:
echo -e "\033[35;42m" ; ifconfig
using option key and using a click the place you want to place the cursor with mouse or touchpad is you are using Mac build-in terminal.
One option is to use M-x shell in emacs. That provides all editing facilities and keystrokes that emacs has, so C-s can be used to search the text option25, for example.
(But I'd still prefer to be in the real terminal shell instead if someone can point me to good search and edit facilities.)
Use the mouse
Sometimes, the easiest way to edit a commandline is using a mouse. Some previous answers give a command to open your current line in your $EDITOR. For me (zhs with grml config) that combination is Alt+e. If you enable mouse in your editor, you can make use of it.
To enable mouse in Vim, add this to your ~/.vimrc
set mouse=a
set ttymouse=xterm2
If you then want to do a text selection in terminal (instead of passing the mouseclick to vim), hold Shift when you click; this is terminal specific, of course.
Sysadmins should not be afraid of the mouse.
In Cygwin, you can activate such feature by right-clicking the window. In the pop-up window, select Options... -> Mouse -> activate Clicks place command line cursor -> Apply.
From now on, simply clicking the left mouse button at some position within the command line will place the cursor there.
first:
export EDITOR='nano -m'
then:
CTRL+X CTRL+E in sequence.
You current line will open in nano editor with mouse enable. You can click in any part of text and edit
then CTRL+X to exit and y to confirm saving.
I made a script to make the command line cursor move on mouse click :
Enable xterm mouse tracking reporting
Set readline bindings to consume the escape sequence generated by clicks
It can be found on github
More info on another post
Will work if echo -e "\e[?1000;1006;1015h" # Enable tracking print escape sequences on terminal when clicking with mouse

Resources