Walid Ber
Walid Ber

Reputation: 101

Bash place cursor beginning of line

How can I set the cursor to the beginning of line in a bash script. It should have the same behavior as pressing Ctrl-a. I tried something like this echo -e "\e[H" but it didn't work.

Here is what I'm trying to do. Let's say I have a command that I want to perform an action on it (doesn't matter what) before executing it. So I associated a Key (using bind -x ) to a function that will perform that action. However, before executing that action, I need to place the cursor to the beginning of that command (as if pressed Ctrl-a)

Upvotes: 9

Views: 14427

Answers (3)

Quintin B
Quintin B

Reputation: 5881

While Deanie's answer of

echo -ne "\r"

is correct, I found I had to ensure that my hash bang was correct:

#!/bin/bash

NOT

#!/bin/sh

Upvotes: 8

clt60
clt60

Reputation: 63952

Let say want bind the /some/path to shift-alt-W and want move to the beginning of the line:

bind '"\eW":"/some/path\C-a"'

Pressing the shift-alt-w will enter the /some/path into the terminal, and the \C-A cause to move to the beginning of the line so you can type cd before the /some/path.

Upvotes: 0

Deanie
Deanie

Reputation: 2394

Wouldn't it just be

echo -ne "\r"

Sorry, forgot to suppress the newline.

Upvotes: 3

Related Questions