ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
I frequently enter commands on the Bash command line which extend over a couple of lines eg.
Code:
for i in files-*.log
if [ -e ${i/log/tmp} ]
then
cp $i ${i/log/out}
fi
done
Manytimes, I build up such a script, where I initially just write an `echo' command instead of `cp' to prevent misfortune. However, each time I use key-up to get the previous entry from the command line, the whole command is written on one line and it becomes difficult to edit. Can Bash be configured such that multi line commands from the history are "rebuilt" that way?
in vi mode you can simply press key up to look for the command and ESC and v to edit that command
in emacs mode there can be something similar, but I'm not really familiar with that mode
Can Bash be configured such that multi line commands from the history are "rebuilt" that way?
Not sure if that could be done in bash, and sometimes the commands are built the way they should, but sometimes just creating and using a temporary script for it, is not really a bad thing and even safer. That's how I do things when I find that I'm already creating a large multi-line command and that I'm no longer really able to handle it.
in vi mode you can simply press key up to look for the command and ESC and v to edit that command
in emacs mode there can be something similar, but I'm not really familiar with that mode
where vi mode of bash is illustrated and this guy gives an example where he enters a command on the Bash command line, presses ESC to enter command mode (leaving insert mode) and 'v' (no quotes) to open a temporary vi session with the command currently being edited. Here it's fairly easy to edit the multi-line command, but what I dont get is how then to correctly execute the modified command?
A guy in the comments says you need :wq (no quotes), and that seems to execute the command, however my prompt gets stuck on a line with a '>' in front of it and i need to use Ctrl-C to finish it. I guess i'm missing how to correctly execute the temporary command from the vi session. How do I do that?
in such cases the command is copied into a temp file and that temp file will be edited by vi. You need to save that file before exiting vi (that is :wq) and finally the content of that temp file will be executed as a command (and also the temp file will be removed). If you got prompt '>' it means the command was not complete, could not be executed (for example a done, esac, fi or similar is missing from the end).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.